SpringBoot controller adds URL unified prefix

 Add a prefix to the project uniformly, this is basically known, set the context-path. If it is to add a unified prefix to the module, how to do it?

1. Add a unified prefix

yml inside settings:

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

2. Add a unified prefix to the module

How to use custom annotations:

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;

}

The method of using @ConfigurationProperties(prefix = "url.api.prefix") here is more convenient than the method of @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 configuration:

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

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

test:

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";
    }

}

result:

 

 

 Summarize:

        If you use a module plus a unified prefix, it is suitable to use annotations. It is more convenient to add corresponding annotations to the Controller that needs to be prefixed.

Guess you like

Origin blog.csdn.net/qq_35461948/article/details/129845102