好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

聊聊@RequestMapping和@GetMapping @PostMapping的区别

@requestmapping和@getmapping @postmapping的区别

最近学习看一些代码,发现对于发送请求这件事,有的地方用@requestmapping,有的地方用@postmapping,为了搞清楚区别,特意查了下spring 源代码,现在特此记录下。

@getmapping 用于将http get请求映射到特定处理程序的方法注解 具体来说,@getmapping是一个组合注解,是@requestmapping(method = requestmethod.get)的缩写。 @postmapping 用于将http post请求映射到特定处理程序的方法注解 具体来说,@postmapping是一个组合注解,是@requestmapping(method = requestmethod.post)的缩写。

下面我们来看下@getmapping的源码

可以对上面的两句释义给予充分的支撑。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

/**

  * annotation for mapping http {@code get} requests onto specific handler

  * methods.

  *

  * <p>specifically, {@code @getmapping} is a <em>composed annotation</em> that

  * acts as a shortcut for {@code @requestmapping(method = requestmethod.get)}.

  *

  *

  * @author sam brannen

  * @since 4.3

  * @see postmapping

  * @see putmapping

  * @see deletemapping

  * @see patchmapping

  * @see requestmapping

  */

@target (elementtype.method)

@retention (retentionpolicy.runtime)

@documented

@requestmapping (method = requestmethod.get)

public @interface getmapping {

 

  /**

   * alias for {@link requestmapping#name}.

   */

  @aliasfor (annotation = requestmapping. class )

  string name() default "" ;

     ...

}

上面代码中,最关键的是

?

1

@requestmapping (method = requestmethod.get)

这行代码即说明@getmapping就是@requestmapping附加了请求方法。

同时,可以看到@getmapping这个注解 是spring4.3版本引入,同时引入的还有@postmapping、@putmapping、@deletemapping和@patchmapping,一共5个注解。

所以,一般情况下用

?

1

@requestmapping (method = requestmethod. xxxx)

即可。

springboot 中常用注解@pathvaribale/@requestparam/@getmapping介绍

介绍几种如何处理url中的参数的注解@pathvaribale/@requestparam/@getmapping。

其中,各注解的作用为:

@pathvaribale 获取url中的数据

@requestparam 获取请求参数的值

@getmapping 组合注解,是@requestmapping(method = requestmethod.get)的缩写

看一个例子,如果我们需要获取url=localhost:80/consumer/get/{id}中的返回的dept值,实现代码如下:

以上,通过@pathvariable注解来获取url中的时参数的前提条件是我们知道url的格式时怎么样的。

只有知道url的格式,我们才能在指定的方法上通过相同的格式获取相应位置的参数值。

一般情况下,url的格式为:localhost:80/consumer/get/{id},这种情况下该如何来获取其中的返回的dept值呢,

关于@requestparam来完成获取返回值代码如下

当输入:http://localhost/consumer/dept/get/1?id=1

看到返回了dept的结果:

但是当输入:http://localhost/consumer/dept/get/1 (即不输入id参数和参数值)

但是当输入:http://localhost/consumer/dept/get/1?id (不输入id参数值)

会报如下错误:

@requestparam注解给我们提供了这种解决方案,即允许用户不输入id时,使用默认值,具体代码如下:

此时输入:http://localhost/consumer/dept/get/1?id 就不在报错(使用了默认值)

输入:http://localhost/consumer/dept/get/1

@getmapping 组合注解

@getmapping是一个组合注解,是@requestmapping(method = requestmethod.get)的缩写。该注解将http get 映射到 特定的处理方法上。

即可以使用@getmapping(value = [/dept/get/{id}])来代替

?

1

@requestmapping (value=]/dept/get/{id}],method= requestmethod.get)

即可以让我们精简代码。

输入:http://localhost/consumer/dept/get/1?id

输入:http://localhost/consumer/dept/get/1

小结

本篇文章介绍了几种常用获取url中的参数哈,比较简单。以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/magi1201/article/details/82226289

查看更多关于聊聊@RequestMapping和@GetMapping @PostMapping的区别的详细内容...

  阅读:17次