• spring boot 根据目录结构自动生成路由前缀


    1.

    .新建一个类 继承 RequestMappingHandlerMapping

    重写 getMappingForMethod 方法

    package com.boot.missyou.core.hack;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
    import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
    
    import java.lang.reflect.Method;
    
    public class AutpPrefixMapping extends RequestMappingHandlerMapping {
        @Value("${missyou.api-package}")
        private String  packagePath;
    
    
        @Override
        protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
          RequestMappingInfo reinfo =  super.getMappingForMethod(method, handlerType);
          if (reinfo!=null) {
              String prefix = this.getPrefix(handlerType);
    // 替换并且合并为新路径 RequestMappingInfo newrequestMappingInfo
    =RequestMappingInfo.paths(prefix).build().combine(reinfo); return newrequestMappingInfo; } return reinfo; }
    // 获取前缀
    public String getPrefix(Class<?> handlerType) { String packName = handlerType.getPackage().getName(); String doPath = packName.replaceAll(this.packagePath, ""); return doPath.replace(".","/"); } }

    2.重新一个类让容器发现    AutpPrefixMapping 类

    采用  

    @Component  + 继承 WebMvcRegistrations

    package com.boot.missyou.core.congiguration;
    
    import com.boot.missyou.core.hack.AutpPrefixMapping;
    import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
    @Component
    public class AutoPrefixConfigration implements WebMvcRegistrations {
    
        @Override
        public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
            return  new AutpPrefixMapping();
        }
    }

    3. 添加配置  属性

     application.properties

    missyou.api-package = com.boot.missyou.api

    为了 可以在类中访问到 

    4.添加 controller  

    package com.boot.missyou.api.v1;
    import com.boot.missyou.exception.http.ForbiddenException;
    import com.boot.missyou.service.BannerService;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    @Api(value = "轮播图", tags = {"用于轮播图的相关接口"})
    @RestController
    @RequestMapping("/banner")
    public class BannerController {
        @Autowired
        private BannerService bannerService;
        @ApiOperation(value = "轮播图", notes = "轮播图", httpMethod = "GET")
        @GetMapping("/test")
        public String test() {
            throw new ForbiddenException(1001);
    //        return "hell cworld111333";
        }
    }

    这个controller位于 v1 的包下

    5.测试

  • 相关阅读:
    升讯威周报与工时统计系统 V3
    浅谈互联网时代的一万小时定律:方向与格局更重要
    GitHub开源:SQLite 增强组件 Sheng.SQLite.Plus
    GitHub开源:升讯威 SQLite 增强组件 Sheng.SQLite.Plus
    centos7 配置IPV6
    Vertica节点故障后的恢复经过
    windows下杀掉指定端口的应用
    解决QT Fault tolerant heap shim问题
    vertica生成查询计划失败:Request size too big
    IDEA配置aliyun的maven源
  • 原文地址:https://www.cnblogs.com/guangzhou11/p/12346208.html
Copyright © 2020-2023  润新知