springboot 给controller路径增加前缀

springboot-web在访问的时,全局增加访问前缀,需要自定义配置。如果使用webmvc需要实现
WebMvcConfigurer的配置类并实现configurePathMatch方法,如果使用webfulx需要继承WebFluxConfigurer并实现configurePathMatching方式。

1.全局配置

1.1 webmvc下配置

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyWebConnfig implements WebMvcConfigurer {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.addPathPrefix("/api/v1", c -> true);
    }
}

2.2 webfulx下配置

import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.config.PathMatchConfigurer;
import org.springframework.web.reactive.config.WebFluxConfigurer;

@Configuration
public class MyWebConnfig implements WebFluxConfigurer {
    @Override
    public void configurePathMatching(PathMatchConfigurer configurer) {
        configurer.addPathPrefix("/api/v1", c -> true);
    }
}

2.局部配置(只修改部分)

2.1 自定义注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public @interface ApiController {
    @AliasFor(annotation = RequestMapping.class)
    String name() default "";

    @AliasFor(annotation = RequestMapping.class)
    String[] value() default {};

    @AliasFor(annotation = RequestMapping.class)
    String[] path() default {};
}

2.2controller上增加注解使用

@ApiController("hello")
public class HelloController {

    @GetMapping("info")
    public String helloSay() {
        return "hello say hello;";
    }
}

2.2修改WebMvcConfigurer配置

@Configuration
public class MyWebConnfig implements WebMvcConfigurer {
//    @Override
//    public void configurePathMatch(PathMatchConfigurer configurer) {
//        configurer.addPathPrefix("/api", c -> true);
//    }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.addPathPrefix("/api/v1", cls -> cls.isAnnotationPresent(ApiController.class));
    }
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个非常优秀的框架,通过它我们可以快速地构建Web应用程序,而且它还提供了许多便捷的功能,例如自动配置、启动器等。 在Spring Boot中,我们可以通过注解@RequestMapping来指定请求路径。默认情况下,请求路径是“/”,即访问应用程序的根目录。 但是,在实际开发中,我们可能需要指定请求路径,例如我们需要将所有的API请求路径前缀都加上“/api”,这时候我们就需要对@RequestMapping进行定制了。 在Spring Boot中,我们可以通过添加@Controller或@RestController注解来将一个类标记为控制器,然后在控制器的某个方法上添加@RequestMapping注解来指定请求路径。 例如,我们要将所有的API请求路径前缀都加上“/api”,我们可以在控制器的类级别上添加@RequestMapping注解,并指定value属性为“/api”。这样,控制器中所有方法的请求路径前缀都会变为“/api”。 @Controller @RequestMapping("/api") public class ApiController { // ... } 如果我们要进一步定制请求路径,例如指定某个方法的请求路径为“/api/user”,我们可以在方法级别上添加@RequestMapping注解,并指定value属性为“/user”。 @Controller @RequestMapping("/api") public class ApiController { @RequestMapping("/user") public String user() { return "Hello, User!"; } } 通过上述代码,我们就可以在浏览器中通过访问“http://localhost:8080/api/user”来访问user方法了。 通过对@RequestMapping的定制,我们可以很容易地对应用程序的请求路径进行管理和控制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值