小编典典

如何在spring boot中设置rest的基url?

spring

我试图将mvc和rest混合在一个单一的spring引导项目中。

我想在一个地方设置所有其余控制器(例如example.com/api)的基本路径(我不想用@RequestMapping('api/products'),而只是用注释每个控制器@RequestMapping('/products')

Mvc控制器应可通过example.com/whatever访问

可能吗?

(我不使用spring数据休息,只是spring mvc)


阅读 447

收藏
2020-04-13

共2个答案

小编典典

使用Spring Boot 1.2+(<2.0),只需在application.properties中使用一个属性即可:

spring.data.rest.basePath=/api

对于2.x,请使用

server.servlet.context-path=/api
2020-04-13
小编典典

我来到这里才得到答案,所以我在这里发布了。创建(如果还没有的话)一个application.properties并添加

server.contextPath=/api

因此,在前面的示例中,如果你具有RestController,@RequestMapping("/test")则可以像这样访问它localhost:8080/api/test/{your_rest_method}

2020-04-13