简体   繁体   English

如何仅为休息控制器更改基本URL?

[英]How to change base url only for rest controllers?

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} ? 是否有任何配置选项允许仅为其他控制器更改基本URL,例如,如果我的api的基本URL是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. 我尝试创建自定义注释,通过添加RequestMapping扩展RestController。 Here is the example, but it does not work. 这是一个例子,但它不起作用。

@Target(ElementType.TYPE) 
@Retention(RetentionPolicy.RUNTIME) 
@RestController 
@RequestMapping(value = "/rest", path = "/rest") 
public @interface MyRestController { }

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 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 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:Base 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 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 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 Data 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 application.properties

spring.data.rest.basePath=/rest

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

Complete property configuration list 完整的属性配置列表

Typically you would define a servlet that handles all (or a particular set) of your restful requests. 通常,您将定义一个servlet来处理所有(或特定集合)的restful请求。 You would then tell that servlet to listen to a particular URL pattern like /rest . 然后,您将告诉该servlet监听特定的URL模式,如/rest The @RequestMapping annotations of your controllers are unaware of that 'top level' pattern. 控制器的@RequestMapping注释不知道“顶级”模式。

For instance, when bootstrapping your Spring Web Application, you could create that restful servlet manually and add a mapping. 例如,在引导Spring Web Application时,您可以手动创建该restful servlet并添加映射。 The whole setup is a little too large to be posted here, but find a snippet below to get a notion. 整个设置有点太大,无法在此处发布,但在下方找到一个片段以获得概念。

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/*");
     ...

   }

您应该在application.properties文件中添加server.servlet-path=/api以及您应该发送的所有请求,如domain / api / users / {id}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM