简体   繁体   English

如何为Spring REST控制器设置多个基本路径(例如,一个或多个软件包的通用基本路径)

[英]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.

相关问题 Spring 4:多条基本路径在一个 controller - Spring 4: Multiple base path in one controller 如何在 spring 引导中将 base url 设置为 rest? - How to set base url for rest in spring boot? Spring Data JPA - 如何以编程方式设置 JpaRepository 基础包 - Spring Data JPA - How to programmatically set JpaRepository base packages 如何仅为休息控制器更改基本URL? - How to change base url only for rest controllers? 为spring数据mongo指定多个基本包 - Specify multiple base packages for spring data mongo 如何使用Spring AOP强制使用通用日志格式。 想要在每个记录器中附加一个字符串,例如服务名称 - How to Enforce a common log format using Spring AOP. Want to append a string e.g. service name in each logger 如何在一个代码库中拥有2个或更多的spring应用程序? - How can I have 2 or more spring applications in one code base? Java Path 如何转换为例如 InputStream - How is Java Path converted to e.g. InputStream Java中有没有一种方法可以基于java.lang基本类型(例如String)构建自定义类型? - Is there a way in Java to build custom types based on java.lang base Types (e.g. String)? 如何创建一个包含多个对象的JList? 例如价格,重量,颜色等 - How to create a JList that contains an object which has more than one value? e.g. Price, Weight, Colour etc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM