工具類 :@RequestMapping 路由註解

專注的阿熊發表於2021-05-25

// 自定義分發註解

@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 的實現

  */

@Component("handlerMapping")

public class RouterRequestMappingHandlerMapping extends RequestMappingHandlerMapping {

// 在將 方法封裝成 HandlerMethod 時會呼叫此方法

     @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;

                     }

                 }

             }

         }

// 呼叫父類,生成 RequestMappingInfo

         RequestMappingInfo mappingForMethod = super.getMappingForMethod(method, handlerType);

         if (requestRouter != null) {

         // 如果 requestRouter 不為空,則進行路徑處理

             String[] requestRouterValue = requestRouter.value();

             PatternsRequestCondition condition = mappingForMethod.getPatternsCondition();

             // 獲取當前方法匹配的路徑,外匯跟單gendan5.com隨即進行新增處理。

             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;

     }

}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2773909/,如需轉載,請註明出處,否則將追究法律責任。

相關文章