@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)的快捷方式。
进一步阅读:
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` 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不会被使用。
@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)
额外阅读克雷格·沃尔斯所著的一本书
推荐文章
- 从枚举中选择一个随机值?
- 如何从URL获取参数与JSP
- 如何在Eclipse中生成Javadoc注释?
- 找到java类从哪里加载
- 从集合中随机选取一个元素
- 为什么x == (x = y)和(x = y) == x不一样?
- 什么Java 8流。收集等价物可在标准Kotlin库?
- 等待未来的名单
- 如何检查JSON键是否存在?
- 为什么MongoDB Java驱动在条件中使用随机数生成器?
- 即使从未抛出异常,使用try-catch块的代价是否昂贵?
- 什么时候我们应该使用观察者和可观察对象?
- Java中的split()方法对点(.)不起作用。
- Eclipse调试器总是阻塞在ThreadPoolExecutor上,没有任何明显的异常,为什么?
- Java生成两个给定值之间的随机数