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加对应的注解,这样就比较方便。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
SpringBootController是一个Java类,用于处理HTTP请求并返回响应。它是一个典型的MVC架构中的控制器层,用于接收来自客户端的请求,调用服务层处理业务逻辑,并返回响应结果给客户端。 SpringBootController可以使用注解的方式定义,常用的注解包括: - @RestController:用于标记这个类是一个Controller,其所有方法都会返回JSON格式的数据。 - @RequestMapping:用于定义请求的URL地址,可以用于类级别和方法级别。 - @GetMapping、@PostMapping、@PutMapping、@DeleteMapping:用于定义HTTP请求的方法,分别对应HTTP的GET、POST、PUT和DELETE方法。 在Controller中,还可以使用一些注解来解析请求参数,如@PathVariable、@RequestParam、@RequestBody等。 示例代码: @RestController @RequestMapping("/api") public class UserController { @Autowired private UserService userService; @GetMapping("/users") public List<User> getAllUsers() { return userService.getAllUsers(); } @GetMapping("/users/{id}") public User getUserById(@PathVariable Long id) { return userService.getUserById(id); } @PostMapping("/users") public User createUser(@RequestBody User user) { return userService.createUser(user); } @PutMapping("/users/{id}") public User updateUser(@PathVariable Long id, @RequestBody User user) { return userService.updateUser(id, user); } @DeleteMapping("/users/{id}") public void deleteUser(@PathVariable Long id) { userService.deleteUser(id); } } 以上代码定义了一个UserController类,用来处理与用户相关的HTTP请求。其中,@RestController标记这个类是一个Controller,@RequestMapping定义了请求的URL前缀,@GetMapping、@PostMapping、@PutMapping和@DeleteMapping分别定义了HTTP请求的方法和URL地址。getUserById方法使用了@PathVariable来解析请求中的参数,createUser和updateUser方法使用了@RequestBody来接收JSON格式的请求体。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天狼1222

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值