@GetMapping和@RequestMapping(method = RequestMethod.GET)有什么区别? 我在一些Spring Reactive的例子中看到过 使用@GetMapping代替@RequestMapping
当前回答
如图所示:
具体来说,@GetMapping是一个组合注释,充当 @RequestMapping(method = RequestMethod.GET)的快捷方式。 @GetMapping和@RequestMapping的区别 @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注释我们可以应用在类级和方法级
@GetMapping是@RequestMapping(method = RequestMethod.GET)的快捷方式 @RequestMapping是一个类级别 @GetMapping是一个方法级
4) @RequestMapping注释用于将web请求映射到特定的处理程序类和函数。这个注释的主要优点是它可以同时用于控制器类和方法。
5)通常建议在控制器方法上声明@RequestMapping时要明确,就像在大多数映射处理程序类中一样,@Getmapping不会被使用。
@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
推荐文章
- 我如何通过Java应用程序使用GMail,雅虎或Hotmail发送电子邮件?
- 注释@GetMapping和@RequestMapping(method = RequestMethod.GET)之间的区别
- lambda表达式中使用的变量应该是final或有效final
- 如何创建数组列表的数组?
- noclassdeffounderror:无法初始化类XXX
- 如何创建今天午夜和明天午夜的Java日期对象?
- ByteBuffer在Java中的用途是什么?
- 使Hibernate忽略未映射的实例变量
- 映射enum在JPA与固定的值?
- 如何找到可用的端口?
- Spring的GA、RC和M2版本之间有什么区别?
- 假唤醒在Java中真的发生了吗?
- 如何按字母顺序排序列表?
- 如何在java中格式化持续时间?(如格式H:MM:SS)
- urlencoder .encode(字符串)已弃用,我应该使用什么代替?