
1. 参数放在请求体
以json串的格式设置在Http请求报文的请求体中。
@RequestMapping(value="/body",method=RequestMethod.POST)
public Result testPostByBody(@RequestBody User user) {
Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
logger.info(user.toString());
return new Result();
}
报文格式:
Hypertext Transfer Protocol
POST /single-app/test/body HTTP/1.1\r\n
Content-Type: application/json\r\n
cache-control: no-cache\r\n
Postman-Token: c4e08bb6-aa6c-4fb7-b8cb-8a278785c8cd\r\n
User-Agent: PostmanRuntime/7.1.1\r\n
Accept: */*\r\n
Host: 106.15.95.37:8089\r\n
accept-encoding: gzip, deflate\r\n
content-length: 45\r\n
Connection: keep-alive\r\n
\r\n
[Full request URI: http://106.15.95.37:8089/single-app/test/body]
[HTTP request 1/2]
[Response in frame: 17770]
File Data: 45 bytes
JavaScript Object Notation: application/json
Object
Member Key: id
Number value: 1
Key: id
Member Key: name
String value: sunpy
Key: name
Member Key: pwd
String value: 123456
Key: pwd
日志结果:

2. 参数放在请求头
以以键值对的形式,参数设置在HTTP请求报文的请求头中。
@RequestMapping(value="/header", method=RequestMethod.POST)
public Result testPostByHeader(@RequestHeader("name") String name) {
Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
logger.info(name);
return new Result();
}
报文格式:
Hypertext Transfer Protocol
POST /single-app/test/header HTTP/1.1\r\n
Content-Type: application/json\r\n
name: sunpy\r\n
cache-control: no-cache\r\n
Postman-Token: 098aabc8-4f60-424a-b94b-8fcce7ac8a38\r\n
User-Agent: PostmanRuntime/7.1.1\r\n
Accept: */*\r\n
Host: 106.15.95.37:8089\r\n
accept-encoding: gzip, deflate\r\n
content-length: 0\r\n
Connection: keep-alive\r\n
\r\n
[Full request URI: http://106.15.95.37:8089/single-app/test/header]
[HTTP request 1/1]
[Response in frame: 19405]
日志结果:

3. 路径传单个参数
将参数作为url的路径的一部分。
@RequestMapping(value="/path/{name}", method=RequestMethod.POST)
public Result testPostByPath(@PathVariable("name") String name) {
Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
logger.info(name);
return new Result();
}
报文格式:
Hypertext Transfer Protocol
POST /single-app/test/path/zhangsan HTTP/1.1\r\n
Content-Type: application/json\r\n
cache-control: no-cache\r\n
Postman-Token: fe659759-3ac0-422c-8b9a-486ca44fa89a\r\n
User-Agent: PostmanRuntime/7.1.1\r\n
Accept: */*\r\n
Host: 106.15.95.37:8089\r\n
accept-encoding: gzip, deflate\r\n
content-length: 0\r\n
Connection: keep-alive\r\n
\r\n
[Full request URI: http://106.15.95.37:8089/single-app/test/path/zhangsan]
[HTTP request 1/1]
[Response in frame: 22715]
日志结果:

4. 路径携带参数传参
@RequestMapping(value="/pathAttr",method=RequestMethod.POST)
public Result testPostByPathAttr(@RequestParam("name") String name) {
Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
logger.info(name);
return new Result();
}
报文格式:
Hypertext Transfer Protocol
POST /single-app/test/pathAttr?name=sunpy HTTP/1.1\r\n
Content-Type: application/json\r\n
cache-control: no-cache\r\n
Postman-Token: a80965b0-a1a7-4355-8384-17bec250c670\r\n
User-Agent: PostmanRuntime/7.1.1\r\n
Accept: */*\r\n
Host: 106.15.95.37:8089\r\n
accept-encoding: gzip, deflate\r\n
content-length: 0\r\n
Connection: keep-alive\r\n
\r\n
[Full request URI: http://106.15.95.37:8089/single-app/test/pathAttr?name=sunpy]
[HTTP request 1/1]
[Response in frame: 40775]
日志结果:

感受:postman这个工具太好用了,省得我写js发送HTTP请求,直接发请求!
网友评论