How to specify prefix for all controllers in Spring Boot?
I have controller mappings to /user
and /order
:
@RestController
@RequestMapping("/users")
public class UserController {
...
}
@RestController
@RequestMapping("/orders")
public class OrderController {
...
}
I want to access these by URL at http://localhost:8080/api/users
and http://localhost:8080/api/orders
, respectively.
How do I achieve this in Spring Boot?
You can provide a mapping to root context path of your spring boot application to /api/*
in your custom configuration.
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;
@Configuration
public class DispatcherServletCustomConfiguration {
@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(
dispatcherServlet(), "/api/");
registration.setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);
return registration;
}
}
or add this to your application.properties
in src\main\resources
folder
server.contextPath=/api/*
EDIT
As of Spring Boot 2.x the property has been deprecated and should be replaced with
server.servlet.contextPath=/api/*
More you find here Spring Boot Context Root and here Add servlet mapping to DispatcherServlet
If you want to add prefix just for some controllers I found two others solutions
Option 1 - Use spring SpEL to add a prefix variable for your controllers
@RestController
@RequestMapping(path = "${v1API}/users")
public class V1FruitsController {
@GetMapping(path = "")
@ResponseBody
public String list(){
return "[\"Joe\", \"Peter\"]";
}
}
application.properties
v1API=/api/v1
Option 2 - Create a custom controller annotation
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
@RequestMapping("/api/v1")
public @interface V1APIController {
@AliasFor(annotation = Component.class)
String value() default "";
}
@V1APIController
public class UserController {
@RequestMapping("/users")
@ReponseBody
public String index(){
return "[\"Joe\", \"Peter\"]";
}
}
then test it
curl -X GET localhost:8080/api/v1/users
If you are using spring boot 2 (spring framework 5), there is a replacement of the property in your application.properties
:
server.contextPath
for:
server.servlet.context-path=
Add your default path in the application.properties
as:
server.servlet.contextPath=/mainPath
Here /mainPath
will be the prefix for all the controller