Java 如何仅为休息控制器更改基本网址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38212691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change base url only for rest controllers?
提问by Adam Madoyan
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} ?
是否有任何配置选项允许更改仅用于休息控制器的基本网址,例如,如果我的 api 的基本网址是 www.example.com/user/{id} 变为 www.example.com/rest/user/{id} ?
I am using spring boot v1.3.2
我使用的是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.
我尝试创建自定义注释,通过添加 RequestMapping 来扩展 RestController。这是示例,但它不起作用。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@RestController
@RequestMapping(value = "/rest", path = "/rest")
public @interface MyRestController { }
回答by Matt
Typically you would define a servlet that handles all (or a particular set) of your restful requests. You would then tell that servlet to listen to a particular URL pattern like /rest
. The @RequestMapping
annotations of your controllers are unaware of that 'top level' pattern.
通常,您会定义一个 servlet 来处理所有(或一组特定的)您的 Restful 请求。然后,您将告诉该 servlet 侦听特定的 URL 模式,例如/rest
. @RequestMapping
您的控制器的注释不知道该“顶级”模式。
For instance, when bootstrapping your Spring Web Application, you could create that restful servlet manually and add a mapping. The whole setup is a little too large to be posted here, but find a snippet below to get a notion.
例如,在引导您的 Spring Web 应用程序时,您可以手动创建该 Restful servlet 并添加一个映射。整个设置有点太大,无法在此处发布,但请在下面找到一个片段以获得一个概念。
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletContext;
...
public class WebAppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
...
ServletRegistration.Dynamic restfulServlet = servletContext.addServlet("myServlet", new DispatcherServlet(rootContext));
restfulServlet.addMapping("/rest/*");
...
}
回答by Kyle Anderson
Option 1: Custom Annotation
选项 1:自定义注释
Create a Custom Annotation that declares the base URL and use that in lieu of @RestController.
创建一个自定义注释来声明基本 URL 并使用它代替 @RestController。
CustomRestControllerAnnotation.java
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
第一个RestController.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
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
选项 2:基础 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.
通过创建一个用作所有实际控制器模板的基本控制器,您可以从一个位置有效地管理根 URL。
BaseRestController.java
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
第一个RestController.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
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
选项 3:Spring 数据 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.
如果您的控制器从存储库提供数据,那么 Spring Data REST 可以去掉大部分样板文件并解决您最初的问题。
pom.xml
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.
通过声明此依赖项,您的所有存储库都会自动启用 REST。
You can control the base URL by using a property file.
您可以使用属性文件来控制基本 URL。
application.properties
应用程序属性
spring.data.rest.basePath=/rest
回答by Adam Madoyan
You should add server.servlet-path=/api
on your application.properties file and all requests you should send like domain/api/users/{id}
您应该添加server.servlet-path=/api
您的 application.properties 文件和您应该发送的所有请求,如 domain/api/users/{id}
回答by ankit.vishen
Updated config for Spring Boot v2.1.0
更新了 Spring Boot v2.1.0 的配置
In Spring Boot v2.1.0 you can configure base URL in application.properties
like
在 Spring Boot v2.1.0 中,您可以application.properties
像这样配置基本 URL
server.servlet.context-path = /baseApiName
server.servlet.context-path = /baseApiName