简体   繁体   English

Spring请求映射到特定路径变量值的不同方法

[英]Spring request mapping to a different method for a particular path variable value

@Controller
@RequestMapping("/authors")
public class AuthorController {
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Author getAuthor(
        final HttpServletRequest request,
        final HttpServletResponse response,
        @PathVariable final String id)
    {
        // Returns a single Author by id
        return null;
    }

    @RequestMapping(value = "/{id}/author-properties", method = RequestMethod.GET)
    public AuthorProperties getAuthorProperties(
        final HttpServletRequest request,
        final HttpServletResponse response,
        @PathVariable final String id)
    {
        // Returns a single Author's List of properties
        return null;
    }

    @RequestMapping // How to map /authors/*/author-properties to this method ????
    public List<AuthorProperties> listAuthorProperties(
        final HttpServletRequest request,
        final HttpServletResponse response)
    {
        // Returns a single Author's List of properties
        return null;
    }
}

class Author {
    String propertiesUri;
    // other fields
}

class AuthorProperties {
    String authorUri;
    // other fields
}

Basically I need: 基本上我需要:

  • /authors - listing all the authors /作者 - 列出所有作者
  • /authors/123 - fetching author by id 123 / authors / 123 - 通过id 123获取作者
  • /authors/123/author-properties - fetching the AuthorProperties object for a 123 author / authors / 123 / author-properties - 获取123作者的AuthorProperties对象
  • /authors/*/author-properties - fetching List of AuthorProperties for all authors / authors / * / author-properties - 获取所有作者的AuthorProperties列表

When I tried 当我尝试

@RequestMapping(value = "/*/author-properties", method = RequestMethod.GET)

It was still mapping /authors/*/author-properties to getAuthorProperties method with path variable value as "*". 它仍将map /authors/*/author-properties映射到getAuthorProperties方法,路径变量值为“*”。

看看这是否有效

@RequestMapping(value = "/{id:.*}/author-properties", method = RequestMethod.GET)

You can constraint the mapping for individual author using regular expressions, such as: 您可以使用正则表达式约束单个作者的映射,例如:

@RequestMapping("/{authorId:\\d+}/author-properties")
public String authorProperties(@PathVariable String authorId) {}

This will only match URLs where the author ID is numeric. 这仅匹配作者ID为数字的URL。

For the request for all author properties you can use: 对于所有作者属性的请求,您可以使用:

@RequestMapping("/*/author-properties")
public String allProperties() { }

Hovewer * has special meaning so it will match /foo/author-properties also. Hovewer *具有特殊含义,因此它也会匹配/foo/author-properties To work around it you can use something like: 要解决它,你可以使用类似的东西:

@RequestMapping("/{all:\\*}/author-properties")
public String allProperties() { }

If fetching List of AuthorProperties for all authors is common case, then I think that you should make an URI like this: "/author-properties". 如果为所有作者提取AuthorProperties列表是常见的情况,那么我认为你应该创建一个这样的URI:“/ author-properties”。 Otherwise answer given by Bohuslav is what you want. 否则Bohuslav给出的答案就是你想要的。

暂无
暂无

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

相关问题 在 Spring 的 GET 请求中将请求参数/路径变量映射到 Dto - Mapping request params/ path variable to Dto in GET request in Spring 使用Path变量为其他URL选择默认请求映射方法 - Default request mapping method picked up for other URLs with Path variable 路径变量未映射Spring MVC - Path Variable not mapping Spring MVC 具有路径变量的Spring MVC映射 - Spring MVC mapping with path variable Spring 中的分页通过路径变量引导 - Controller 请求映射注释中不存在请求的路径变量 - Pagination in Spring Boot via path variable - Requested path variable is not present in Controller request mapping annotations spring http请求映射单个uri用不同的请求参数到不同的方法是好的做法还是不好的做法 - spring http request mapping single uri with different request parameters to different method is good practice or bad practice Spring Web MVC:对请求参数和路径变量使用相同的请求映射 - Spring Web MVC: Use same request mapping for request parameter and path variable 这个 Spring Boot controller 方法注释有什么问题? (请求的路径在 class 级别定义,路径变量在方法中定义) - What is wrong in this Spring Boot controller method annotation? (the path of the request is defined at class level and the path variable at method) Spring 请求映射逻辑 map 是否应该基于 servletPath 的值到处理程序方法? - Shoud the Spring request mapping logic map to a handler method based upon the value of servletPath? 通过将子路径映射到其他控制器来拆分Spring Controller - Splitting up a Spring Controller by mapping a sub-path to a different controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM