如何仅更改其余控制器的基本URL? [英] How to change base url only for rest controllers?

查看:44
本文介绍了如何仅更改其余控制器的基本URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何配置选项仅允许更改REST控制器的基本URL,例如,如果我的API的基本URL为www.example.com/user/{id}变为www.example.com/rest/user/{id}?

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} ?

我正在使用Spring Boot v1.3.2

I am using spring boot v1.3.2

我试图通过添加RequestMapping来创建自定义注释,从而扩展RestController.这是示例,但不起作用.

I tried to create custom annotation which extends RestController by adding RequestMapping. Here is the example, but it does not work.

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

推荐答案

选项1:自定义注释

创建一个声明基本URL的自定义注释,并使用它代替@RestController.

Option 1: Custom Annotation

Create a Custom Annotation that declares the base URL and use that in lieu of @RestController.

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

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

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";
    }
}

选项2:基本RestController

通过创建用作所有实际Controllers模板的Base Controller,您可以从一个位置有效地管理根URL.

Option 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.

BaseRestController.java

package com.example.stackoverflow.controller;

import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("/rest")
public class BaseRestController {}

然后,您只需为所有实际的Controller扩展此类即可.

Then you simply extend this class for all of your actual Controllers.

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

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";
    }
}

选项3:Spring Data REST

如果您的控制器正在从存储库中提供数据,则Spring Data REST会占用很多样板内容;解决您最初的问题.

Option 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.

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>

通过声明此依赖性,您的所有存储库都会自动启用REST.

By declaring this dependency, all of your Repositories automatically become REST enabled.

您可以通过使用属性文件来控制基本URL.

You can control the base URL by using a property file.

application.properties

spring.data.rest.basePath=/rest

这篇关于如何仅更改其余控制器的基本URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆