@RequestParam @PathVariable @RequestMapping

@RequestParam请求接口时,URL是:http://www.test.com/user/getUserById?userId=1

//@RequestParam用法,注意这里请求后面没有添加参数

@RequestMapping(value = "/test",method = RequestMethod.POST)

public Result test(@RequestParam(value="id",required=false,defaultValue="0")String id)

注意上面@RequestParam用法当中的参数。

<pre style="background:white">@PathVariable("xxx")</pre>

<pre style="margin-left:21.0pt;mso-para-margin-left:2.0gd;background:white">通过 @PathVariable 可以将URL中占位符参数{xxx}绑定到处理器类的方法形参中@PathVariable(“xxx“) </pre>

<pre style="margin-left:21.0pt;mso-para-margin-left:2.0gd;background:white">@RequestMapping(“user/{id}/{name}”)</pre>

<pre style="margin-left:21.0pt;mso-para-margin-left:2.0gd;background:white">请求路径:http://localhost:8080/user/1/james</pre>

@PathVariable主要用于接收http://host:port/path/{参数值}数据。@RequestParam主要用于接收http://host:port/path?参数名=参数值数据,这里后面也可以不跟参数值。

@RequestMapping("/xxx/{xxx}")请求

Post请求:@RequestMapping(value="/test",method = RequestMethod.POST)

RequestMapping中有六个属性分别是:

value: 指定请求的实际地址,指定的地址可以是URI Template 模式;

method: 指定请求的method类型, GET、POST、PUT、DELETE等;

consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;

produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;

params: 指定request中必须包含某些参数值是,才让该方法处理。

headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容