简体   繁体   English

Spring Boot REST控制器仅与尾随“ /”匹配

[英]Spring Boot REST controller only matches with trailing “/”

I have a controller such as: 我有一个控制器,例如:

@RestController
@RequestMapping("/foo")
public class ServicesController {

    @RequestMapping(value="/", method=RequestMethod.GET)
    public Something doStuff() {
        ...
    }
}

For my domain www.bar.com , the request mapping will be called when I visit www.bar.com/foo/ , but not when I visit www.bar.com/foo . 对于我的域名www.bar.com ,当我访问www.bar.com/foo/ ,将调用请求映射,但是当我访问www.bar.com/foo时,将不调用请求映射。 How can I make Spring trigger the same method for both www.bar.com/foo/ and www.bar.com/foo ? 如何让Spring为www.bar.com/foo/www.bar.com/foo触发相同的方法?

Well, if you take a look at your own code, you're saying that to access your ServicesController , you must go to /foo , and that to access your doStuff , you must go to / . 好吧,如果您看一下自己的代码,就是说要访问ServicesController ,必须转到/foo ,要访问doStuff ,必须转到/

In short, you're saying that to call your doStuff method in your ServicesController , you must go to /foo/ . 简而言之,您是说要在ServicesController调用doStuff方法,必须转到/foo/

The right way in my opinion, to have a 'default' method for a class, you can do something like: 我认为使用正确的方法为类提供“默认”方法,您可以执行以下操作:

@RestController
@RequestMapping("/foo")
public class ServicesController {

    @RequestMapping(method=RequestMethod.GET)
    public Something doStuff() {
    }
}

You may add another value like, 您可以添加另一个值,例如

@RequestMapping(value={"","/"}, method=RequestMethod.GET)
public Something doStuff() {
    ...
}

this will trigger for both the www.bar.com/foo/ and www.bar.com/foo 这将同时触发www.bar.com/foo/www.bar.com/foo

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

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