如何在 Spring Boot 中设置用于休息的基本 url? [英] How to set base url for rest in spring boot?

查看:31
本文介绍了如何在 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

这篇关于如何在 Spring Boot 中设置用于休息的基本 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆