1

I have several REST endpoints in my Spring Boot application. We are currently using JAX-RS / Apache CXF (using javax.ws.rs.Path annotation in our Facade REST endpoint classes) and are currently migrating to to Spring REST (MVC). Due to big number of endpoint classes, we have structure like:

com.example.domain1.Facade1.java - @Path("facade1path)
com.example.domain1.Facade2.java - @Path("facade2path)
... (much more "Facade" classes in com.example.domain1 package)

com.example.domain2.Facade3.java - @Path("facade3path)
com.example.domain2.Facade4.java - @Path("facade4path)
... (much more "Facade" classes in com.example.domain2 package)

And we have the application configured in a way that paths are set to:

"/api/domain1/facade1path" -> Facade1.java
"/api/domain1/facade2path" -> Facade2.java
"/api/domain2/facade3path" -> Facade3.java
"/api/domain2/facade4path" -> Facade4.java

With old JAX-RS / CXF configuration, we were able to achieve this in quite elegant way, without having to repeat "/api/domain1/" or "/api/domain2/ in all @Path annotations. We have set up multiple "jaxrs-server" elements in specific CXF configuration in this way:

<jaxrs:server id="Domain1Server" address="/api/domain1" basePackages="com.example.domain1" />
<jaxrs:server id="Domain2Server" address="/api/domain2" basePackages="com.example.domain2" />

And it works :-)

Is there any way how to achieve something similar with Spring REST / MVC, without having to repeat "api/domain1" or "api/domain2" in annotations in multiple controller classes -

@RequestMapping("api/domain1/facade1path"), @RequestMapping("api/domain1/facade2path"), etc.?

Have not found any kind of resources indicating that this would be somehow possible with Spring so far, maybe it is not possible, but it would be nice if there is some hack how to do it :-)

Thanks in advance, anyone!

Michal Aron
  • 985
  • 1
  • 7
  • 8
  • https://stackoverflow.com/questions/38212691/how-to-change-base-url-only-for-rest-controllers I hope this link will help you. – Harshit May 28 '18 at 08:14
  • [I hope this link will help you.][1] [1]:https://stackoverflow.com/questions/38212691/how-to-change-base-url-only-for-rest-controllers – Harshit May 28 '18 at 08:18
  • [This link will help you about Base URL.](https://stackoverflow.com/questions/38212691/how-to-change-base-url-only-for-rest-controllers) – Harshit May 28 '18 at 08:20

1 Answers1

0

Create two Controller classes in Spring, and set baseUrl at the controller class level.

First will look like below:

@RestController
@RequestMapping("/api/domain1")
public class DomainOneController {

@RequestMapping("/facade1path"), 
@RequestMapping("/facade2path") ...
}

Second will look like:

@RestController
@RequestMapping("/api/domain2")
public class DomainTwoController {

@RequestMapping("/facade3path"), 
@RequestMapping("/facade4path") ...
}
Amit K Bist
  • 6,760
  • 1
  • 11
  • 26
  • Thanks, but that's not the point - the structure is following `package1:` `Controller1 @RequestMapping("/api/domain1/facade1path")` `Controller2 @RequestMapping("/api/domain1/facade2path")` `package2:` `Controller3 @RequestMapping("/api/domain2/facade3path")` `Controller4 @RequestMapping("/api/domain2/facade3path")` So there are MULTIPLE controller classes all having the "base url" (and we do not want to merge them into one class), they are just placed in the same package. What I want to avoid is to repeat "/api/domain1/" or "/api/domain2/" in all controller classes in this package. – Michal Aron May 28 '18 at 08:04