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

PHP网站开发成功案例内网代理ip建设网站

PHP网站开发成功案例,内网代理ip建设网站,有没有免费的室内设计软件,西安知名网站制作公司一、前言 在Spring Boot项目开发过程中#xff0c;对于接口API发布URL访问路径#xff0c;一般都是在类上标识RestController或者Controller注解#xff0c;然后在方法上标识RequestMapping相关注解#xff0c;比如#xff1a;PostMapping、GetMapping注解#xff0c;通…一、前言 在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 MapString, Object map) { // 这里只是方便测试实际情况下请勿使用Map作为参数接收StringBuilder stringBuilder new StringBuilder();stringBuilder.append(hello, ).append(name).append(, receive param:);for (Map.EntryString, 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项目导入相关依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency2. 修改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*/ MapString, Object map) { // 这里只是方便测试实际情况下请勿使用Map作为参数接收StringBuilder stringBuilder new StringBuilder();stringBuilder.append(hello, ).append(name).append(, receive param:);for (Map.EntryString, 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 ApplicationListenerWebServerInitializedEvent {/*** 标识事件监听器是否已经注册避免重复注册*/private volatile AtomicBoolean flag new AtomicBoolean(false);/*** 需要发布的地址*/public static final String CUSTOM_URL /test/url;Autowiredprivate RequestMappingHandlerMapping requestMappingHandlerMapping;Autowiredprivate TestController testController;SneakyThrowsOverridepublic 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?namejack 可以看到此时请求效果并不是正常的存在参数丢失怎么办呢 注意如果请求出现如下错误 java.lang.IllegalArgumentException: Expected lookupPath in request attribute org.springframework.web.util.UrlPathHelper.PATH.可以在application.yaml文件中添加如下内容 spring:mvc:pathmatch:matching-strategy: ant_path_matcher5. 增加统一请求处理器 为了实现参数可以正常解析同时方便增加自定义处理逻辑我们可以增加一个统一的请求处理器参考示例 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方式请求参数MapString, 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 ApplicationListenerWebServerInitializedEvent {/*** 标识事件监听器是否已经注册避免重复注册*/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. 重新测试 此时请求可以发现效果和使用RestControllerRequestMapping注解就一样了。 四、写在最后 自定义发布URL路径一般情况下很少使用不过针对特殊url的处理以及自定义rpc框架发布url时选择这样处理好了可以达到出其不意的效果。
http://www.hkea.cn/news/14409460/

相关文章:

  • 公司网站建设方案拓扑图农产品网络营销是什么
  • 安丘建设网站遵义建设厅官方网站 元丰
  • 安徽省建设行业质量与安全协会网站湖南省建设局官方网站
  • 做响应式网站的意义网站首页标题怎么写
  • 爱网站在线观看视频在putty做网站要拷贝什么
  • 网帆网站建设大专计算机专业主要学什么
  • 浙江省2012年7月自学考试网站建设与网页设计滕州网站建设招聘
  • 企业建设微网站的重要性台州网站设计建设
  • 旅游网站设计的目的服务器网站托管
  • 学网站建设需要什么安庆网站建设aqwzjs
  • 南京门户网站建设网站制作公司排名
  • 展示型网站源码上海网站建设seo站霸网络
  • 大型网站 网站建设漳州网站开发找出博大科技
  • 如何做网课网站邢台住房与城乡建设部网站
  • 哪一个军事网站做的比较好it运维管理平台软件
  • 邯郸seo优化大型网站seo课程
  • 如何设置公司网站火车头wordpress 5.1
  • 成都网站推广排名重庆专业网站推广费用
  • php建站软件哪个好网站建设与管理 教学设计
  • 上海网站建设-中国互联wordpress 下载远程图片大小
  • php网站开发有什么软件婚纱摄影网站图片
  • H5酒店静态网站建设开题报告范文dw主页制作
  • 网站建设公司专业网站开发研发房产微信营销方案
  • 帝国建设网站韩漫网站建设
  • 青海城乡建设厅网站 官网wordpress 克隆页面
  • 展厅设计制作网站寻找石家庄网站建设
  • 天门市规划建设局网站重庆新闻天天630
  • 做移门图的 网站有哪些微信小程序开发模板网站
  • seo建站优化国外做电商网站有哪些
  • 网上书店网站建设策划书网站psd模版