SpringBoot controller添加URL统一前缀

 给项目统一加前缀,这个基本都是知道,设置context-path。 如果是给模块加统一前缀,要怎么弄呢?

1,添加统一前缀

yml里面设置:

server:
    port: 8081
    servlet:
        context-path: /api/v1

2,给模块添加统一前缀

使用自定义注解的方式:

ApiPrefix

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
//import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "url.api.prefix")
@ApiModel(value = "url的Api前缀")
public class ApiPrefix {
    @ApiModelProperty("流程模块的前缀")
    // @Value("${url.api.prefix.flow}")
    private String flow;

    @ApiModelProperty("通用模块的前缀")
    // @Value("${url.api.prefix.common}")
    private String common;

    @ApiModelProperty("用户模块的前缀")
    // @Value("${url.api.prefix.user}")
    private String user;

}

这边使用 @ConfigurationProperties(prefix = "url.api.prefix") 的方式,比 @Value的方式更为简便

ApiPrefixFlowRestController

import org.springframework.web.bind.annotation.RestController;

import java.lang.annotation.*;

@RestController
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiPrefixFlowRestController {

}

ApiPrefixCommonRestController

import org.springframework.web.bind.annotation.RestController;

import java.lang.annotation.*;

@RestController
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiPrefixCommonRestController {

}

ApiPrefixUserRestController 

import org.springframework.web.bind.annotation.RestController;

import java.lang.annotation.*;

@RestController
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiPrefixUserRestController {

}

WebMvcConfig

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    private ApiPrefix apiPrefix;

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.addPathPrefix(apiPrefix.getFlow(), c -> c.isAnnotationPresent(ApiPrefixFlowRestController.class));
        configurer.addPathPrefix(apiPrefix.getCommon(), c -> c.isAnnotationPresent(ApiPrefixCommonRestController.class));
        configurer.addPathPrefix(apiPrefix.getUser(), c -> c.isAnnotationPresent(ApiPrefixUserRestController.class));
    }

}

yml配置:

server:
    port: 8081
    servlet:
        context-path: /api/v1

url:
  api:
    prefix:
      flow: flow
      common: common
      user: user

测试:

TestApiPrefixFlowRestController

@ApiPrefixFlowRestController
@RequestMapping("/prefix")
public class TestApiPrefixFlowRestController {

    @GetMapping(value = "/getInfo")
    public String get() {
        return "flow prefix";
    }
}

TestApiPrefixCommonRestController

@ApiPrefixCommonRestController
@RequestMapping("/prefix")
public class TestApiPrefixCommonRestController {

    @GetMapping(value = "/getInfo")
    public String get() {
        return "common prefix";
    }
}

TestApiPrefixUserRestController 

@ApiPrefixUserRestController
@RequestMapping("/prefix")
public class TestApiPrefixUserRestController {

    @GetMapping(value = "/getInfo")
    public String get() {
        return "user prefix";
    }

}

结果:

 

 总结:

        如果是使用模块加统一前缀,适合使用注解的方式。 对于需要加前缀的Controller加对应的注解,这样就比较方便。

猜你喜欢

转载自blog.csdn.net/qq_35461948/article/details/129845102