- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- Java工具類(lèi)之@RequestMapping注解
問(wèn)題闡述:在某一場(chǎng)景下,我們的代碼在 Service 實(shí)現相同,但卻在 Controller 層訪(fǎng)問(wèn)時(shí)卻希望不同的前綴可以訪(fǎng)問(wèn)。如下 :/say/hello。我們這里希望在不借助任何外部服務(wù)的情況下 通過(guò) /a/say/hello 和 /b/say/hello 都可以訪(fǎng)問(wèn)到該接口,同時(shí)不想在 Controller 中寫(xiě)兩個(gè)方法。
@RestController @RequestMapping("say") public class SayController { @Autowired private SayService sayService; @RequestMapping("hello") public String hello() { return sayService.hello(); } }
我們這里簡(jiǎn)單說(shuō)明一下思路:
1.在 Spring 服務(wù)啟動(dòng)后, HandlerMapping 的實(shí)現類(lèi) RequestMappingHandlerMapping
會(huì )獲取到被 @RequestMapping等請求注解修飾的方法,并封裝成一個(gè)個(gè) HandlerMethod 保存到 RequestMappingHandlerMapping#MappingRegistry
中(HandlerMapping 具有多個(gè)實(shí)現類(lèi),每個(gè)實(shí)現類(lèi)具有不同規則)。
2.當 DispatcherServlet 接收到請求后會(huì )根據 url 獲取 合適的 HandlerMapping 組成 HandlerExecutionChain(處理器執行鏈),隨后通過(guò) HandlerAdapter 來(lái)進(jìn)行請求處理。而這里通過(guò) HandlerMapping 會(huì )根據請求 URL 獲取到匹配的 HandlerMethod 進(jìn)行方法調用。
因此我們這里有了兩種思路 :
1.在 Spring 加載 HandlerMethod 時(shí)設置當前 HandlerMethod 的匹配規則為 /a/say/hello/、/b/say/hello/,當 /a/say/hello/、/b/say/hello/ 請求訪(fǎng)問(wèn)時(shí)可以與之匹配。
2.在請求處理的時(shí)候,通過(guò)攔截器將 /a/say/hello/、/b/say/hello/ 的訪(fǎng)問(wèn)路徑匹配到 /say/hello 方法上。
本文選擇第一種思路(不過(guò)話(huà)說(shuō)怎么想都是第一種好吧)做一個(gè)簡(jiǎn)單demo示例,其實(shí)現如下:
// 自定義分發(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í)現 */ @Component("handlerMapping") public class RouterRequestMappingHandlerMapping extends RequestMappingHandlerMapping { // 在將 方法封裝成 HandlerMethod 時(shí)會(huì )調用此方法 @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; } } } } // 調用父類(lèi),生成 RequestMappingInfo RequestMappingInfo mappingForMethod = super.getMappingForMethod(method, handlerType); if (requestRouter != null) { // 如果 requestRouter 不為空,則進(jìn)行路徑處理 String[] requestRouterValue = requestRouter.value(); PatternsRequestCondition condition = mappingForMethod.getPatternsCondition(); // 獲取當前方法匹配的路徑,隨即進(jìn)行添加處理。 Set<String> patterns = condition.getPatterns(); Set<String> routerPatterns = patterns.stream() // 拼接 請求路徑。這里可以自定義處理策略 .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í)現,而 dispatcherServlet.setDetectAllHandlerMappings(false);
參數設置Spring 放棄加載多個(gè) HandlerMapping,而只加載 beanName為 handlerMapping 的
2.HandlerMapping。RequestMappingInfo 包含 當前方法的諸多信息,其中就包含 什么樣請求路徑可以匹配到該方法,所以我們在這里獲取到 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/
都可以訪(fǎng)問(wèn)
到此這篇關(guān)于Java工具類(lèi)之@RequestMapping注解的文章就介紹到這了,更多相關(guān)Java RequestMapping內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站