Option 1: Custom Annotation
Create a Custom Annotation that declares the base URL and use that in lieu of @RestController.
CustomRestControllerAnnotation.java
package com.example.stackoverflow.config;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@RestController
@RequestMapping("/rest")
public @interface CustomRestControllerAnnotation {}
FirstRestController.java
package com.example.stackoverflow.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.stackoverflow.config.CustomRestControllerAnnotation;
@CustomRestControllerAnnotation
public class FirstRestController {
@RequestMapping("/first")
public String firstMethod(){
return "First Controller";
}
}
SecondRestController.java
package com.example.stackoverflow.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.stackoverflow.config.CustomRestControllerAnnotation;
@CustomRestControllerAnnotation
public class SecondRestController {
@RequestMapping("/second")
public String secondMethod(){
return "Second Controller";
}
}
Option 2: Base RestController
By creating a Base Controller that serves as a template for all of your actual Controllers, you can effectively manage the root URL from a single location.
BaseRestController.java
package com.example.stackoverflow.controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/rest")
public class BaseRestController {}
Then you simply extend this class for all of your actual Controllers.
FirstRestController.java
package com.example.stackoverflow.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FirstRestController extends BaseRestController{
@RequestMapping("/first")
public String firstMethod(){
return "First Controller";
}
}
SecondRestController.java
package com.example.stackoverflow.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SecondRestController extends BaseRestController{
@RequestMapping("/second")
public String secondMethod(){
return "Second Controller";
}
}
Option 3: Spring Data REST
If your Controllers are serving Data from a Repository, then Spring Data REST can take out much of the boilerplate & solve your initial problem.
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
By declaring this dependency, all of your Repositories automatically become REST enabled.
You can control the base URL by using a property file.
application.properties
spring.data.rest.basePath=/rest