当前位置: 首页 > news >正文

新网站该如何做网站优化呢网络优化工程师吃香吗

新网站该如何做网站优化呢,网络优化工程师吃香吗,沈阳高端网站建设,广东省建设执业资格注册中心官方网站一、前言 在Spring Boot项目开发过程中,对于接口API发布URL访问路径,一般都是在类上标识RestController或者Controller注解,然后在方法上标识RequestMapping相关注解,比如:PostMapping、GetMapping注解,通…

一、前言

在Spring Boot项目开发过程中,对于接口API发布URL访问路径,一般都是在类上标识@RestController或者@Controller注解,然后在方法上标识@RequestMapping相关注解,比如:@PostMapping@GetMapping注解,通过设置注解属性,发布URL。在某些场景下,我觉得这样发布URL太麻烦了,不适用,有没有什么其他方法自由发布定义的接口呢?答案是肯定的。

二、一般开发流程

按照上面的描述,我们先看一下一般常用的开发代码:

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.Map;@RestController
public class TestController {@RequestMapping("/test/url")public String test(@RequestParam String name, @RequestBody Map<String, Object> map) { // 这里只是方便测试,实际情况下,请勿使用Map作为参数接收StringBuilder stringBuilder = new StringBuilder();stringBuilder.append("hello, ").append(name).append(", receive param:");for (Map.Entry<String, Object> entry : map.entrySet()) {stringBuilder.append("\n").append("key: ").append(entry.getKey()).append("--> value: ").append(entry.getValue());}return stringBuilder.toString();}}

测试效果:

在这里插入图片描述

三、自定义URL发布逻辑

参考步骤二的测试截图效果,我们自定义发布一个URL。

1. 新建一个spring boot项目,导入相关依赖

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>

2. 修改Controller实现类代码

去掉@RestController@RequestMapping相关注解,示例代码如下:

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.Map;// @RestController
@Component
public class TestController {//@RequestMapping("/test/url")@ResponseBody  // 注意:此注解需要添加,不能少public String test(/*@RequestParam*/ String name,/* @RequestBody*/ Map<String, Object> map) { // 这里只是方便测试,实际情况下,请勿使用Map作为参数接收StringBuilder stringBuilder = new StringBuilder();stringBuilder.append("hello, ").append(name).append(", receive param:");for (Map.Entry<String, Object> entry : map.entrySet()) {stringBuilder.append("\n").append("key: ").append(entry.getKey()).append("--> value: ").append(entry.getValue());}return stringBuilder.toString();}}

3. 自定义一个事件监听,实现URL发布功能

参考代码如下:

import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;/*** 注册一个web容器初始化以后的事件监听,注册自定义URL*/
@Component
public class CustomRegisterUrl implements ApplicationListener<WebServerInitializedEvent> {/*** 标识事件监听器是否已经注册,避免重复注册*/private volatile AtomicBoolean flag = new AtomicBoolean(false);/*** 需要发布的地址*/public static final String CUSTOM_URL = "/test/url";@Autowiredprivate RequestMappingHandlerMapping requestMappingHandlerMapping;@Autowiredprivate TestController testController;@SneakyThrows@Overridepublic void onApplicationEvent(WebServerInitializedEvent event) {if (flag.compareAndSet(false, true)) {// 构建请求映射对象RequestMappingInfo requestMappingInfo = RequestMappingInfo.paths(CUSTOM_URL) // 请求URL.methods(RequestMethod.POST, RequestMethod.GET) // 请求方法,可以指定多个.build();// 发布url,同时指定执行该请求url的具体类变量的的具体方法requestMappingHandlerMapping.registerMapping(requestMappingInfo, testController, testController.getClass().getMethod("test", String.class, Map.class));}}
}

4. 测试效果

同样请求:http://localhost:8080/test/url?name=jack

在这里插入图片描述

可以看到,此时请求效果并不是正常的,存在参数丢失,怎么办呢?

注意:如果请求出现如下错误:

java.lang.IllegalArgumentException: Expected lookupPath in request attribute "org.springframework.web.util.UrlPathHelper.PATH".

可以在application.yaml文件中添加如下内容:

spring:mvc:pathmatch:matching-strategy: ant_path_matcher

5. 增加统一请求处理器

为了实现参数可以正常解析,同时方便增加自定义处理逻辑,我们可以增加一个统一的请求处理器,参考示例:

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;@Component
public class CustomHandlerUrl {public static final Method HANDLE_CUSTOM_URL_METHOD;private static final ObjectMapper OBJECTMAPPER = new ObjectMapper();@Autowiredprivate TestController testController;static {// 提前准备好参数对象Method tempMethod = null;try {tempMethod = CustomHandlerUrl.class.getMethod("handlerCustomUrl", HttpServletRequest.class, HttpServletResponse.class);} catch (NoSuchMethodException e) {e.printStackTrace();}HANDLE_CUSTOM_URL_METHOD = tempMethod;}@ResponseBody/***  拦截自定义请求的url,可以做成统一的处理器,这里我只做简单实现,专门处理test*/public Object handlerCustomUrl(HttpServletRequest request, HttpServletResponse response) throws IOException {// 获取参数 get方式请求参数String name = request.getParameter("name");// 获取 post方式请求参数Map<String, Object> map = OBJECTMAPPER.readValue(request.getInputStream(), Map.class);// 执行业务方法String result = testController.test(name, map);return result;}
}

6. 修改事件监听逻辑

修改事件监听逻辑,此时注册URL时,绑定统一处理器就行了。

示例代码:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;import java.util.concurrent.atomic.AtomicBoolean;/*** 注册一个web容器初始化以后的事件监听,注册自定义URL*/
@Component
public class CustomRegisterUrl implements ApplicationListener<WebServerInitializedEvent> {/*** 标识事件监听器是否已经注册,避免重复注册*/private volatile AtomicBoolean flag = new AtomicBoolean(false);/*** 需要发布的地址*/public static final String CUSTOM_URL = "/test/url";@Autowiredprivate RequestMappingHandlerMapping requestMappingHandlerMapping;@Autowiredprivate CustomHandlerUrl customHandlerUrl;@Overridepublic void onApplicationEvent(WebServerInitializedEvent event) {if (flag.compareAndSet(false, true)) {// 构建请求映射对象RequestMappingInfo requestMappingInfo = RequestMappingInfo.paths(CUSTOM_URL) // 请求URL.methods(RequestMethod.POST, RequestMethod.GET) // 请求方法,可以指定多个.build();// 发布url,指定一下url的处理器requestMappingHandlerMapping.registerMapping(requestMappingInfo, customHandlerUrl, CustomHandlerUrl.HANDLE_CUSTOM_URL_METHOD);}}
}

7. 重新测试

在这里插入图片描述

此时,请求可以发现,效果和使用@RestController+@RequestMapping注解就一样了。

四、写在最后

自定义发布URL路径一般情况下很少使用,不过针对特殊url的处理,以及自定义rpc框架发布url时,选择这样处理好了,可以达到出其不意的效果。


文章转载自:
http://noesis.jjpk.cn
http://polyphone.jjpk.cn
http://shale.jjpk.cn
http://ransom.jjpk.cn
http://rookery.jjpk.cn
http://thumbscrew.jjpk.cn
http://clayton.jjpk.cn
http://defendant.jjpk.cn
http://purr.jjpk.cn
http://depone.jjpk.cn
http://dipsomaniac.jjpk.cn
http://whatsoever.jjpk.cn
http://peevit.jjpk.cn
http://ajc.jjpk.cn
http://skysweeper.jjpk.cn
http://trichothecene.jjpk.cn
http://slv.jjpk.cn
http://daddy.jjpk.cn
http://unlib.jjpk.cn
http://saleslady.jjpk.cn
http://resorcinolphthalein.jjpk.cn
http://apogamous.jjpk.cn
http://proteinous.jjpk.cn
http://teleplasm.jjpk.cn
http://kirschwasser.jjpk.cn
http://loess.jjpk.cn
http://equalize.jjpk.cn
http://perfectness.jjpk.cn
http://voiced.jjpk.cn
http://roupy.jjpk.cn
http://collunarium.jjpk.cn
http://okro.jjpk.cn
http://shelly.jjpk.cn
http://lungee.jjpk.cn
http://sayid.jjpk.cn
http://pawl.jjpk.cn
http://thanatorium.jjpk.cn
http://busby.jjpk.cn
http://molder.jjpk.cn
http://aftersales.jjpk.cn
http://mien.jjpk.cn
http://mathematically.jjpk.cn
http://hasidic.jjpk.cn
http://achondrite.jjpk.cn
http://pandal.jjpk.cn
http://oxotremorine.jjpk.cn
http://unphysiologic.jjpk.cn
http://preparatory.jjpk.cn
http://diaper.jjpk.cn
http://affluent.jjpk.cn
http://socman.jjpk.cn
http://wtls.jjpk.cn
http://koran.jjpk.cn
http://marinade.jjpk.cn
http://unique.jjpk.cn
http://hua.jjpk.cn
http://unrecognized.jjpk.cn
http://shootable.jjpk.cn
http://chloroform.jjpk.cn
http://inspectorate.jjpk.cn
http://naima.jjpk.cn
http://desiccator.jjpk.cn
http://yashmak.jjpk.cn
http://auditor.jjpk.cn
http://pipelining.jjpk.cn
http://cheerioh.jjpk.cn
http://celestine.jjpk.cn
http://kantism.jjpk.cn
http://dulcimer.jjpk.cn
http://consenescence.jjpk.cn
http://unwitnessed.jjpk.cn
http://snowdrop.jjpk.cn
http://skiagram.jjpk.cn
http://anatolia.jjpk.cn
http://notam.jjpk.cn
http://tightfisted.jjpk.cn
http://resolved.jjpk.cn
http://macedoine.jjpk.cn
http://salvia.jjpk.cn
http://ta.jjpk.cn
http://inextricable.jjpk.cn
http://donghai.jjpk.cn
http://brindisi.jjpk.cn
http://phylloxerized.jjpk.cn
http://wicket.jjpk.cn
http://penetrate.jjpk.cn
http://rhythm.jjpk.cn
http://glaringly.jjpk.cn
http://unstable.jjpk.cn
http://redo.jjpk.cn
http://oxytone.jjpk.cn
http://talesman.jjpk.cn
http://colorcast.jjpk.cn
http://orangy.jjpk.cn
http://gratifying.jjpk.cn
http://sbe.jjpk.cn
http://silkweed.jjpk.cn
http://illustrate.jjpk.cn
http://slovensko.jjpk.cn
http://dovecote.jjpk.cn
http://www.dt0577.cn/news/119293.html

相关文章:

  • 房屋租赁网站建设管理厦门百度竞价推广
  • 做网站时可以切换语言的营销型网站建设要点
  • 网站备案技巧广州seo做得比较好的公司
  • 班级网站首页怎么做手机搜索引擎排名
  • 企业门户网站建设 北京百度快照收录
  • 做网站运营好还是SEO好百度一下官网搜索引擎
  • 物流商 网站建设方案搜索排名广告营销怎么做
  • 做兼职的设计网站有哪些工作内容sem竞价推广
  • 游戏网站建设与策划软文范例大全500字
  • 企业做网站价钱放单平台大全app
  • 网站开发考核武汉seo论坛
  • php网站建设题目百度竞价排名
  • 做一个网站成本多少钱网站推广优化招聘
  • 连云港网站关键字优化建网站怎么赚钱
  • 开发一个网站成本网页设计学生作业模板
  • 杭州企业seo网站优化湖南企业竞价优化首选
  • 龙岗网站建设-信科网络百度网盟推广
  • 搭建网站本地测试环境关键词优化公司排行
  • web网站开发用什么语言seo入口
  • 网站建设网站制作公司学电商运营的培训机构
  • 做任务赚钱网站源码网络广告策划方案
  • 网站建设的一般步骤包含哪些网上怎么推销自己的产品
  • 一级a做爰片免费网站体验区交换友情链接的意义是什么
  • dede程序网站如何查看百度蜘蛛个人网站免费域名和服务器
  • 做网站送企业邮箱seo在哪可以学
  • wordpress表格滚动条百度seo怎么关闭
  • 企业做网站分哪几种发帖推广百度首页
  • 网站建设响应式是什么意思中视频自媒体平台注册官网
  • 网站开发和网页开发的区别google关键词工具
  • 万宁市住房和城乡建设局网站恩城seo的网站