Java @RequestMapping注解中path和value属性的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50351590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:34:37  来源:igfitidea点击:

Difference between path and value attributes in @RequestMapping annotation

javaspringrestspring-bootspring-restcontroller

提问by Raj

What is the difference between below two attributes and which one to use when?

以下两个属性之间有什么区别以及何时使用哪一个?

@GetMapping(path = "/usr/{userId}")
public String findDBUserGetMapping(@PathVariable("userId") String userId) {
  return "Test User";
}

@RequestMapping(value = "/usr/{userId}", method = RequestMethod.GET)
public String findDBUserReqMapping(@PathVariable("userId") String userId) {
  return "Test User";
}

采纳答案by g00glen00b

As mentioned in the comments (and the documentation), valueis an alias to path. Spring often declares the valueelement as an alias to a commonly used element. In the case of @RequestMapping(and @GetMapping, ...) this is the pathproperty:

正如评论(和文档)中提到的,valuepath. Spring 经常将value元素声明为常用元素的别名。在@RequestMapping(and @GetMapping, ...)的情况下,这是path属性:

This is an alias for path(). For example @RequestMapping("/foo")is equivalent to @RequestMapping(path="/foo").

这是 的别名path()。例如@RequestMapping("/foo")相当于@RequestMapping(path="/foo").

The reasoning behind this is that the valueelement is the default when it comes to annotations, so it allows you to write code in a more concise way.

这背后的原因是该value元素是注释的默认元素,因此它允许您以更简洁的方式编写代码。

Other examples of this are:

这方面的其他例子是:

  • @RequestParam(valuename)
  • @PathVariable(valuename)
  • ...
  • @RequestParam( valuename)
  • @PathVariable( valuename)
  • ...

However, aliases aren't limited to annotation elements only, because as you demonstrated in your example, @GetMappingis an alias for @RequestMapping(method = RequestMethod.GET).

但是,别名不仅限于注释元素,因为正如您在示例中所展示的,@GetMapping是 ) 的别名@RequestMapping(method = RequestMethod.GET

Just looking for references of AliasForin their codeallows you to see that they do this quite often.

只需在他们的代码中查找 的引用,AliasFor您就可以看到他们经常这样做。

回答by user7294900

@GetMappingis an alias for @RequestMapping

@GetMapping是@RequestMapping 的别名

@GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).

@GetMapping 是一个组合注解,作为@RequestMapping(method = RequestMethod.GET) 的快捷方式。

valuemethod is an alias for path method.

value方法是 path 方法的别名。

This is an alias for path(). For example @RequestMapping("/foo") is equivalent to @RequestMapping(path="/foo").

这是 path() 的别名。例如@RequestMapping("/foo") 等价于@RequestMapping(path="/foo")。

So both methods are similar in that sense.

所以这两种方法在这个意义上是相似的。

回答by Juzer Ali

@GetMappingis a shorthand for @RequestMapping(method = RequestMethod.GET).

@GetMapping是 的简写@RequestMapping(method = RequestMethod.GET)

In your case. @GetMapping(path = "/usr/{userId}")is a shorthand for @RequestMapping(value = "/usr/{userId}", method = RequestMethod.GET).

在你的情况下。 @GetMapping(path = "/usr/{userId}")是 的简写@RequestMapping(value = "/usr/{userId}", method = RequestMethod.GET)

Both are equivalent. Prefer using shorthand @GetMappingover the more verbose alternative. One thing that you can do with @RequestMappingwhich you can't with @GetMappingis to provide multiple request methods.

两者是等价的。更喜欢使用速记@GetMapping而不是更冗长的替代方法。一件事,你可以做@RequestMapping,你不能用@GetMapping是提供多个请求的方法。

@RequestMapping(value = "/path", method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT)
public void handleRequet() {

}

Use @RequestMappingwhen you need to provide multiple Http verbs.

使用@RequestMapping时,您需要提供多个HTTP动词。

Another usage of @RequestMappingis when you need to provide a top level path for a controller. For e.g.

的另一种用法@RequestMapping是当您需要为控制器提供顶级路径时。例如

@RestController
@RequestMapping("/users")
public class UserController {

    @PostMapping
    public void createUser(Request request) {
        // POST /users
        // create a user
    }

    @GetMapping
    public Users getUsers(Request request) {
        // GET /users
        // get users
    }

    @GetMapping("/{id}")
    public Users getUserById(@PathVariable long id) {
        // GET /users/1
        // get user by id
    }
}