How to specify prefix for all controllers in Spring Boot?

Murali :

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?

Tomasz Janek :

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Enable/Disable all controllers of spring boot app?

Add prefix to all Spring Boot actuator endpoints

Spring Boot: How to specify the PasswordEncoder?

How to configure a default @RestController URI prefix for all controllers?

how to intercept all requests in spring REST controllers?

How to find all controllers in Spring MVC?

How do I specify a BeanNamingStrategy with Spring Boot?

How to specify the Launcher in Spring Boot Gradle?

How to specify a fallback request mapping in Spring Boot

How can i reduce repetitive code in spring boot controllers

How to rewrite URLs with Spring (Boot) via REST Controllers?

How to check intended controllers are loaded in WebMvcTest in Spring Boot Unit Test

How can I resolve CORS error in Spring Boot for specific controllers?

Spring boot and controllers in imported modules

Spring Boot API with Multiple Controllers?

how can i set prefix path for thymeleaf in spring boot

Spring boot How to add prefix "/api" to certain urls, but not on "/" "/login"

How to count all classes with the @Controllers attribute in your spring project

Spring boot - How to Specify Java path in Process Builder

How to specify a profile when deploying a Spring boot war file to Tomcat?

How to specify external properties files in application.properties in Spring Boot?

Spring boot - How to specify different location for rotated tomcat log file

How to specify UTC timezone for Spring Boot JPA Timestamp

Liquibase with Spring Boot and multiple schemas, how to specify execution order

How to specify a newer version of Elasticsearch in spring-boot

How do I specify a default value for an identity variable in Spring Boot?

How to see specify class list in spring-boot?

How to update all libraries in Spring Boot project?

How to list all the managed @Component in Spring Boot?