软件编程
位置:首页>> 软件编程>> java编程>> Java工具类之@RequestMapping注解

Java工具类之@RequestMapping注解

作者:猫吻鱼  发布时间:2023-11-16 03:00:54 

标签:Java,RequestMapping,注解

一、前言

问题阐述:在某一场景下,我们的代码在 Service 实现相同,但却在 Controller 层访问时却希望不同的前缀可以访问。如下 :/say/hello。我们这里希望在不借助任何外部服务的情况下 通过 /a/say/hello 和 /b/say/hello 都可以访问到该接口,同时不想在 Controller 中写两个方法。


@RestController
@RequestMapping("say")
public class SayController {

@Autowired
   private SayService sayService;

@RequestMapping("hello")
   public String hello() {
       return sayService.hello();
   }
}

二、代码实现

我们这里简单说明一下思路:

1.在 Spring 服务启动后, HandlerMapping 的实现类 RequestMappingHandlerMapping 会获取到被 @RequestMapping等请求注解修饰的方法,并封装成一个个 HandlerMethod 保存到 RequestMappingHandlerMapping#MappingRegistry 中(HandlerMapping 具有多个实现类,每个实现类具有不同规则)。

2.当 DispatcherServlet 接收到请求后会根据 url 获取 合适的 HandlerMapping 组成 HandlerExecutionChain(处理器执行链),随后通过 HandlerAdapter 来进行请求处理。而这里通过 HandlerMapping 会根据请求 URL 获取到匹配的 HandlerMethod 进行方法调用。

因此我们这里有了两种思路 :

1.在 Spring 加载 HandlerMethod 时设置当前 HandlerMethod 的匹配规则为 /a/say/hello/、/b/say/hello/,当 /a/say/hello/、/b/say/hello/ 请求访问时可以与之匹配。

2.在请求处理的时候,通过 * 将 /a/say/hello/、/b/say/hello/ 的访问路径匹配到 /say/hello 方法上。

本文选择第一种思路(不过话说怎么想都是第一种好吧)做一个简单demo示例,其实现如下:


// 自定义分发注解
@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();
           // 获取当前方法匹配的路径,随即进行添加处理。
           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中有多个实现,而 dispatcherServlet.setDetectAllHandlerMappings(false); 参数设置Spring 放弃加载多个 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/ 都可以访问

Java工具类之@RequestMapping注解
Java工具类之@RequestMapping注解

来源:https://blog.csdn.net/qq_36882793/article/details/115963109

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com