简体   繁体   中英

@Path equivalent in spring-boot

Say I have controller with a method defined like this:

@RestController
@RequestMapping("/{user-id}/foo")
public class FooController {


    @RequestMapping("bar")
    public BarController someBarLogic() {
      //
    }
}

Is it possible to go further than {user-id}/foo/bar without specifying the whole root-path ? (Is there a way to relativize the path like @Path annotation in Jersey or an equivalent annotation in Spring-Boot ?)

It looks like you're looking for ant-style wildcards in path patterns. Have a look here:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-patterns

Accordingly you can define a RequestMapping like this

@RestController
@RequestMapping("/foo")
public class DemoController {
    @RequestMapping(value = "/bar/**", method = RequestMethod.GET)
    public String getDemo() {
        return "Hello world!";
    }
}

which will match /foo/bar/baz .

Ok, another example based on your comment below:

@RestController
public class DemoController {
    @RequestMapping(value = "/**/baz/**", method = RequestMethod.GET)
    public String getDemo() {
        return "Hello world!";
    }
}

This will match the same url as above, and also /foo/bar/baz/bar/foo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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