@GetMapping和@RequestMapping(method = RequestMethod.GET)注释之间的区别

时间:2016-08-22 10:58:35

标签: java spring spring-mvc spring-4

@GetMapping@RequestMapping(method = RequestMethod.GET)之间的区别是什么?  我在一些Spring Reactive的例子中看到了这一点 使用@GetMapping代替@RequestMapping

4 个答案:

答案 0 :(得分:152)

@RequestMapping(method = RequestMethod.GET)是一个组合注释,充当@GetMapping的快捷方式。

SectionAuthorAdapter sectionAuthorAdapter; sectionAuthorAdapter = new SectionAuthorAdapter (mainActivity,authors_groupses); yourListView1.setAdapter(sectionAuthorAdapter ); sectionAuthorAdapter = new SectionAuthorAdapter (mainActivity,authors_groupses); yourListView2.setAdapter(sectionAuthorAdapter ); 是较新的注释。 它支持消费

消费选项是:

consumes =“text / plain”
 consumes = {“text / plain”,“application / *”}

详情请见: GetMapping Annotation

或阅读: request mapping variants

RequestMapping也支持消费

答案 1 :(得分:19)

正如您所见here

  

具体来说,@GetMapping是一个作为一个组成的注释   @RequestMapping(method = RequestMethod.GET)的快捷方式。

     

@GetMapping&之间的差异@RequestMapping

     

@GetMapping支持consumes属性   @RequestMapping

答案 2 :(得分:5)

@RequestMapping是课程级别

@GetMapping是方法级别

使用sprint Spring 4.3。一切都变了。现在,您可以在将处理http请求的方法上使用@GetMapping。使用(方法级别)@GetMapping注释完善了类级别的@RequestMapping规范

这里是一个例子:

@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)

克雷格·沃尔斯(Craig Walls)撰写的书的额外读物 Extra reading from a book authored by Craig Walls

答案 3 :(得分:0)

简短答案:

语义上没有区别。

  

具体来说,@ GetMapping是由组成的注释,用作   @RequestMapping的快捷方式(方法= 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

相关问题