简体   繁体   English

如何忽略 Spring MVC 中的基本路径 url?

[英]How to ignore base path url in Spring MVC?

I want to make several controllers for such paths with a common prefix v1.0 :我想为具有公共前缀v1.0的此类路径制作多个控制器:

  • mysite.com/v1.0/users
  • mysite.com/v1.0/addresses
  • mysite.com/v1.0/phones

However, also I want to keep controllers paths without this prefix, for example:但是,我也想保留没有此前缀的控制器路径,例如:

  • mysite.com/monitoring/ping

The prefix v1.0 can be changed in the future.前缀v1.0将来可以更改 So, I don't want to duplicate this prefix for each controller not to change it everywhere iteratively whenever the prefix is changed.所以,我不想为每个 controller 复制这个前缀,而不是在前缀更改时在任何地方迭代地更改它。

I can write this property in application.properties to add a prefix:我可以在application.properties中写这个属性来添加一个前缀:

  • spring.data.rest.basePath=/api/v1

But in this case I will not be able to get requests on the addresses without this prefix.但是在这种情况下,如果没有这个前缀,我将无法获得对地址的请求。

The question is how to organize such a service?问题是如何组织这样的服务?

You could use你可以使用

@RestController
@RequestMapping("/v1")
public class SomeController {

on classlevel.在班级。 Then all @RequestMapping in this Class will be accesible with the prefix "/v1".然后这个 Class 中的所有@RequestMapping 都可以使用前缀“/v1”访问。 On a sidenote I would suggest not to use "."在旁注中,我建议不要使用“。” in your urls.在你的网址中。

If you don´t want to use the String in each Controller the only option that I can think of is the following.如果您不想在每个 Controller 中使用字符串,我能想到的唯一选项如下。 I tried it out and it seems to be working fine for me.我试过了,它似乎对我来说工作正常。

@RestController
@RequestMapping("/${your.custom.property.in.application.properties}")
public class SomeController { 

your.custom.property.in.application.properties = 1

--> The URL http://localhost:8080/1/test sends me a repsonse. --> URL http://localhost:8080/1/test 向我发送了一个回复。 The URL mapping was successful. URL 映射成功。 Debugger stops in breakpoints in this Controller调试器在此 Controller 中的断点处停止

I think, you can keep all controller methods with similar prefix like mysite.com/v1.0/.../... in same class and at class level you can use @RequestMapping(/v1.0) .我认为,您可以将所有 controller 方法都保留在同一个 class 中,并在 class 级别使用类似mysite.com/v1.0/.../...的前缀,您可以使用@RequestMapping(/v1.0) for example例如

@RequestMapping(/v1.0)
class Class1
{
    @GetMapping(/users)
    public String method1()
    {....}
    .
    .
    .

    @PostMapping(/addresses)
    public String method2()
    {....}

    @GetMapping(/phones)
    public String method3()
    {....}
}

and if you don't want to keep controller methods within the same prefix then you can create another class and don't annotate it at the class level.如果您不想将 controller 方法保留在同一前缀中,那么您可以创建另一个 class 并且不要在 class 级别对其进行注释。 Only annotate all controller methods with different prefixes.仅注释所有具有不同前缀的 controller 方法。

class Class2
{
    @GetMapping(/monitoring/ping)
    public String method1()
    {....}
    .
    .
    .
}

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

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