【发布时间】:2016-12-28 21:38:42
【问题描述】:
@GetMapping 和 @RequestMapping(method = RequestMethod.GET) 有什么区别?
我在一些 Spring Reactive 示例中看到过,
使用@GetMapping 代替@RequestMapping
【问题讨论】:
标签: java spring spring-mvc spring-4
@GetMapping 和 @RequestMapping(method = RequestMethod.GET) 有什么区别?
我在一些 Spring Reactive 示例中看到过,
使用@GetMapping 代替@RequestMapping
【问题讨论】:
标签: java spring spring-mvc spring-4
@RequestMapping 即使使用 method=GET 也支持消费,而 @GetMapping 不支持消费。除此之外@GetMapping 与@RequestMapping(method=RequestMethod.GET) 相同
【讨论】:
@GetMapping 是一个组合注释,充当@RequestMapping(method = RequestMethod.GET) 的快捷方式。
@GetMapping 是较新的注解。
支持消费
消费选项是:
consumes = "text/plain"
消耗 = {"text/plain", "application/*"}
更多详情见: GetMapping Annotation
RequestMapping 也支持消费
GetMapping 我们只能在方法级别应用,而 RequestMapping 注解我们可以在类级别和方法级别应用
【讨论】:
@GetMapping 支持consumes - docs.spring.io/spring-framework/docs/current/javadoc-api/org/…
简答:
语义上没有区别。
具体来说,@GetMapping 是一个组合注解,它充当一个 @RequestMapping(method = RequestMethod.GET) 的快捷方式。
进一步阅读:
RequestMapping 可用于类级别:
这个注解可以在类和方法级别使用。 在大多数情况下,在方法级别的应用程序会更喜欢使用一个 HTTP 方法特定变体的@GetMapping、@PostMapping、 @PutMapping、@DeleteMapping 或@PatchMapping。
而GetMapping 仅适用于方法:
用于将 HTTP GET 请求映射到特定处理程序的注释 方法。
【讨论】:
@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)
【讨论】:
如你所见here:
具体来说,
@GetMapping是一个组合注解,它充当@RequestMapping(method = RequestMethod.GET)的快捷方式。
@GetMapping和@RequestMapping之间的区别
@GetMapping支持consumes属性,例如@RequestMapping.
【讨论】: