@GetMapping和@RequestMapping(method = RequestMethod.GET)有什么区别? 我在一些Spring Reactive的例子中看到过 使用@GetMapping代替@RequestMapping
如图所示:
具体来说,@GetMapping是一个组合注释,充当 @RequestMapping(method = RequestMethod.GET)的快捷方式。 @GetMapping和@RequestMapping的区别 @GetMapping支持像这样的消费属性 @RequestMapping。
@GetMapping是一个组合注释,作为@RequestMapping(method = RequestMethod.GET)的快捷方式。
@GetMapping是更新的注释。 它支持消费
消费选项有:
消费= "text/plain" 消费= {"text/plain", "application/*"}
详情见: GetMapping注释
或阅读: 请求映射变量
RequestMapping也支持消费
GetMapping我们只能应用在方法级,RequestMapping注释我们可以应用在类级和方法级
@RequestMapping是一个类级别
@GetMapping是一个方法级
使用sprint Spring 4.3。现在情况已经发生了变化。现在您可以在处理http请求的方法上使用@GetMapping。类级@RequestMapping规范使用(方法级)@GetMapping注释进行了细化
这里有一个例子:
@Slf4j
@Controller
@RequestMapping("/orders")/* The @Request-Mapping annotation, when applied
at the class level, specifies the kind of requests
that this controller handles*/
public class OrderController {
@GetMapping("/current")/*@GetMapping paired with the classlevel
@RequestMapping, specifies that when an
HTTP GET request is received for /order,
orderForm() will be called to handle the request..*/
public String orderForm(Model model) {
model.addAttribute("order", new Order());
return "orderForm";
}
}
在Spring 4.3之前,它是@RequestMapping(method=RequestMethod.GET)
额外阅读克雷格·沃尔斯所著的一本书
简短的回答:
语义上没有区别。
具体来说,@GetMapping是一个组合注释,充当 @RequestMapping(method = RequestMethod.GET)的快捷方式。
进一步阅读:
RequestMapping可以在类级别使用:
这个注释既可以在类级别使用,也可以在方法级别使用。 在大多数情况下,在方法级应用程序更倾向于使用一个 HTTP方法的特定变量@GetMapping, @PostMapping @PutMapping, @DeleteMapping或@PatchMapping。
而GetMapping只适用于方法:
将HTTP GET请求映射到特定处理程序的注释 方法。
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/GetMapping.html
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html
@RequestMapping即使使用方法=GET也支持消耗,而@GetMapping不支持消耗。 @RequestMapping是方法和类型级别的注释,而@GetMapping是方法级别的注释
除此之外,@GetMapping与@RequestMapping(method=RequestMethod.GET)相同
`@RequestMapping` since 2.5
可以处理所有HTTP方法
=>适用于类和方法
=>可以用来代替@Controller和@RestController,如果我们使用它 和@Component一起。
`@GetMapping` since 4.3
=>只能处理HTTP的GET方法
=>仅适用于方法
@GetMapping是@RequestMapping(method = RequestMethod.GET)的特定类型。两者都支持消费
@GetMapping是@RequestMapping(method = RequestMethod.GET)的快捷方式 @RequestMapping是一个类级别 @GetMapping是一个方法级
4) @RequestMapping注释用于将web请求映射到特定的处理程序类和函数。这个注释的主要优点是它可以同时用于控制器类和方法。
5)通常建议在控制器方法上声明@RequestMapping时要明确,就像在大多数映射处理程序类中一样,@Getmapping不会被使用。
推荐文章
- Java Regex捕获组
- Openssl不被视为内部或外部命令
- 如何添加自定义方法到Spring Data JPA
- 如何在Ubuntu中设置Java环境路径
- 无法执行dex:在Eclipse中超过GC开销限制
- 有人能解释一下JPA和Hibernate中的mappedBy吗?
- 是什么导致JNI调用变慢?
- Java中的&和&&有什么区别?
- 使用Java的Collections.singletonList()?
- Maven使用多个src目录编译
- 导入时无法解析符号
- 用谓词限制流
- 为特定的代码行禁用特定的Checkstyle规则?
- Java中生成UUID字符串的有效方法(UUID. randomuuid ().toString()不带破折号)
- malformedurlexception:没有协议