Is there any configuration option that allows to change base url only for rest controllers, for example if my api's base url is www.example.com/user/{id} becomes www.example.com/rest/user/{id} ?
I am using spring boot v1.3.2
I tried to create custom annotation which extends RestController by adding RequestMapping. Here is the example, but it does not work.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@RestController
@RequestMapping(value = "/rest", path = "/rest")
public @interface MyRestController { }
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";
}
}
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";
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With