如何在 Spring Boot 中设置用于休息的基本 url?

问题描述:

我正在尝试在一个 Spring Boot 项目中混合使用 mvc 和 rest.

I'm trying to to mix mvc and rest in a single spring boot project.

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

I want to set base path for all rest controllers (eg. example.com/api) in a single place (I don't want annotate each controller with @RequestMapping('api/products'), instead, just @RequestMapping('/products').

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

Mvc controllers should be accessible by example.com/whatever

有可能吗?

(我不使用spring data rest,只使用spring mvc)

(I don't use spring data rest, just spring mvc)

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

With Spring Boot 1.2+ (<2.0) all it takes is a single property in application.properties:

spring.data.rest.basePath=/api

参考链接:https://docs.spring.io/spring-data/rest/docs/current/reference/html/#getting-started.changed-base-uri

对于 2.x,使用

server.servlet.context-path=/api