How to set base url for rest in spring boot?

Teimuraz picture Teimuraz · Oct 4, 2015 · Viewed 185.5k times · Source

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

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 controllers should be accessable by example.com/whatever

Is it possible?

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

Answer

Suroj picture Suroj · Mar 15, 2017

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

spring.data.rest.basePath=/api

ref link : https://docs.spring.io/spring-data/rest/docs/current/reference/html/#getting-started.changing-base-uri

For 2.x, use

server.servlet.context-path=/api