[英]How to set multiple base paths for Spring REST controllers (e.g. common base path for one or more packages)
I have several REST endpoints in my Spring Boot application. 我的Spring Boot应用程序中有几个REST端点。 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).
我们当前正在使用JAX-RS / Apache CXF(在Facade REST端点类中使用javax.ws.rs.Path批注),并且当前正在迁移到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: 使用旧的JAX-RS / CXF配置,我们能够以非常优雅的方式实现此目的,而不必在所有@Path注释中重复“ / api / domain1 /”或“ / api / domain2 /”。我们设置了多个“特定CXF配置中的jaxrs-server”元素以这种方式:
<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 - 有什么方法可以实现与Spring REST / MVC类似的功能,而不必在多个控制器类的注释中重复“ api / domain1”或“ api / domain2”-
@RequestMapping("api/domain1/facade1path"), @RequestMapping("api/domain1/facade2path"),
etc.? @RequestMapping("api/domain1/facade1path"), @RequestMapping("api/domain1/facade2path"),
等?
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 :-) 到目前为止,还没有发现任何资源表明这在某种程度上可以通过Spring实现,也许是不可能的,但是如果有一些技巧可以做到这一点,那就太好了:-)
Thanks in advance, anyone! 预先感谢任何人!
Create two Controller classes in Spring, and set baseUrl at the controller class level. 在Spring中创建两个Controller类,并在controller类级别设置baseUrl。
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") ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.