Java工具類之@RequestMapping注解
一、前言
問題闡述:在某一場(chǎng)景下,我們的代碼在 Service 實(shí)現(xiàn)相同,但卻在 Controller 層訪問時(shí)卻希望不同的前綴可以訪問。如下 :/say/hello。我們這里希望在不借助任何外部服務(wù)的情況下 通過 /a/say/hello 和 /b/say/hello 都可以訪問到該接口,同時(shí)不想在 Controller 中寫兩個(gè)方法。
@RestController @RequestMapping("say") public class SayController { @Autowired private SayService sayService; @RequestMapping("hello") public String hello() { return sayService.hello(); } }
二、代碼實(shí)現(xiàn)
我們這里簡(jiǎn)單說明一下思路:
1.在 Spring 服務(wù)啟動(dòng)后, HandlerMapping 的實(shí)現(xiàn)類 RequestMappingHandlerMapping
會(huì)獲取到被 @RequestMapping等請(qǐng)求注解修飾的方法,并封裝成一個(gè)個(gè) HandlerMethod 保存到 RequestMappingHandlerMapping#MappingRegistry
中(HandlerMapping 具有多個(gè)實(shí)現(xiàn)類,每個(gè)實(shí)現(xiàn)類具有不同規(guī)則)。
2.當(dāng) DispatcherServlet 接收到請(qǐng)求后會(huì)根據(jù) url 獲取 合適的 HandlerMapping 組成 HandlerExecutionChain(處理器執(zhí)行鏈),隨后通過 HandlerAdapter 來進(jìn)行請(qǐng)求處理。而這里通過 HandlerMapping 會(huì)根據(jù)請(qǐng)求 URL 獲取到匹配的 HandlerMethod 進(jìn)行方法調(diào)用。
因此我們這里有了兩種思路 :
1.在 Spring 加載 HandlerMethod 時(shí)設(shè)置當(dāng)前 HandlerMethod 的匹配規(guī)則為 /a/say/hello/、/b/say/hello/,當(dāng) /a/say/hello/、/b/say/hello/ 請(qǐng)求訪問時(shí)可以與之匹配。
2.在請(qǐng)求處理的時(shí)候,通過攔截器將 /a/say/hello/、/b/say/hello/ 的訪問路徑匹配到 /say/hello 方法上。
本文選擇第一種思路(不過話說怎么想都是第一種好吧)做一個(gè)簡(jiǎn)單demo示例,其實(shí)現(xiàn)如下:
// 自定義分發(fā)注解 @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface RequestRouter { String[] value() default ""; }
package com.kingfish.springjdbcdemo.config; import lombok.SneakyThrows; import org.springframework.stereotype.Component; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition; import org.springframework.web.servlet.mvc.method.RequestMappingInfo; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Arrays; import java.util.Set; import java.util.stream.Collectors; /** * @Author : kingfish * @Email : kingfishx@163.com * @Data : 2021/4/21 16:47 * @Desc : 路由 HandlerMapping 的實(shí)現(xiàn) */ @Component("handlerMapping") public class RouterRequestMappingHandlerMapping extends RequestMappingHandlerMapping { // 在將 方法封裝成 HandlerMethod 時(shí)會(huì)調(diào)用此方法 @SneakyThrows @Override protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) { // 獲取 RequestRouter 注解 RequestRouter requestRouter = method.getAnnotation(RequestRouter.class); if (requestRouter == null) { requestRouter = handlerType.getAnnotation(RequestRouter.class); if (requestRouter == null) { for (Class<?> handlerTypeInterface : handlerType.getInterfaces()) { if ((requestRouter = handlerTypeInterface.getAnnotation(RequestRouter.class)) != null) { break; } } } } // 調(diào)用父類,生成 RequestMappingInfo RequestMappingInfo mappingForMethod = super.getMappingForMethod(method, handlerType); if (requestRouter != null) { // 如果 requestRouter 不為空,則進(jìn)行路徑處理 String[] requestRouterValue = requestRouter.value(); PatternsRequestCondition condition = mappingForMethod.getPatternsCondition(); // 獲取當(dāng)前方法匹配的路徑,隨即進(jìn)行添加處理。 Set<String> patterns = condition.getPatterns(); Set<String> routerPatterns = patterns.stream() // 拼接 請(qǐng)求路徑。這里可以自定義處理策略 .flatMap(pattern -> Arrays.stream(requestRouterValue).map(val -> "/" + val + pattern)) .collect(Collectors.toSet()); // 將拼接后的路徑添加到 RequestMappingInfo 中 patterns.addAll(routerPatterns); } return mappingForMethod; } }
@Configuration public class SpringConfig { @Bean public DispatcherServlet dispatcherServlet(){ DispatcherServlet dispatcherServlet = new DispatcherServlet(); // 禁止加載所有的handlerMapper,而只加載beanName 為 handlerMapper 的bean dispatcherServlet.setDetectAllHandlerMappings(false); return dispatcherServlet; } }
這里需要注意 :
1.HandlerMapping 在 Spring中有多個(gè)實(shí)現(xiàn),而 dispatcherServlet.setDetectAllHandlerMappings(false);
參數(shù)設(shè)置Spring 放棄加載多個(gè) HandlerMapping,而只加載 beanName為 handlerMapping 的
2.HandlerMapping。RequestMappingInfo 包含 當(dāng)前方法的諸多信息,其中就包含 什么樣請(qǐng)求路徑可以匹配到該方法,所以我們?cè)谶@里獲取到 RequestRouter 的信息,并添加到匹配路徑上。
三、效果
在 方法上加上 @RequestRouter(value = {"a", "b"})
注解
@RestController @RequestMapping("say") public class SayController { @Autowired private SayService sayService; @RequestRouter(value = {"a", "b"}) @RequestMapping("hello") public String hello() { return sayService.hello(); } }
/a/say/hello/
、/b/say/hello/
以及 /say/hello/
都可以訪問
到此這篇關(guān)于Java工具類之@RequestMapping注解的文章就介紹到這了,更多相關(guān)Java RequestMapping內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JDK1.7中HashMap的死循環(huán)問題及解決方案
這篇文章主要為大家介紹了JDK1.7中HashMap的死循環(huán)問題及解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10基于Tomcat7、Java、WebSocket的服務(wù)器推送聊天室實(shí)例
HTML5 WebSocket實(shí)現(xiàn)了服務(wù)器與瀏覽器的雙向通訊,本篇文章主要介紹了基于Tomcat7、Java、WebSocket的服務(wù)器推送聊天室實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12Spring MVC請(qǐng)求參數(shù)的傳遞方式
Spring MVC是一種基于Model-View-Controller(MVC)設(shè)計(jì)模式的輕量級(jí)Web框架,用于Java應(yīng)用程序的開發(fā),在處理HTTP請(qǐng)求時(shí),Spring MVC會(huì)涉及到請(qǐng)求參數(shù)的傳遞,本文給大家介紹了Spring MVC請(qǐng)求參數(shù)的傳遞方式,需要的朋友可以參考下2024-10-10List轉(zhuǎn)換成Map工具類的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄狶ist轉(zhuǎn)換成Map工具類的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01java 壓縮和解壓縮Zip、Jar、Gzip文件實(shí)例代碼
本文主要介紹java壓縮和解壓縮Zip、Jar、Gzip文件的知識(shí),這里整理了相關(guān)資料,并附示例代碼有興趣的小伙伴可以參考下2016-09-09