springboot REST api 实例
所属分类 springboot
浏览量 954
@RestController
public class ApiController
@GetMapping("/")
public String hello()
@RequestMapping("/api")
public Map api(HttpServletRequest request)
@RequestMapping("/api2")
public Map api2(HttpServletRequest request)
@RequestMapping("/api3")
public Map api3(@RequestBody Map params)
测试工具
浏览器 curl命令 postman
http://127.0.0.1:8000/api?id=1&name=cat
http://127.0.0.1:8000/api2?id=1&name=cat
http://127.0.0.1:8000/api3?id=1&name=cat
This application has no explicit mapping for /error, so you are seeing this as a fallback.
There was an unexpected error (type=Bad Request, status=400).
@RequestMapping("/api3")
public Map api3(@RequestBody Map params) {
@RequestBody 注解 得用 post
否则 400 BAD_REQUEST
非法的json格式也会报 400 BAD_REQUEST
curl -XPOST http://127.0.0.1:8000/api3 -d 'name=tiger&id=1'
curl -XPOST http://127.0.0.1:8000/api3 -d '{"id":1,"name":"tiger"}'
{"timestamp":"2021-03-03T08:30:18.518+00:00","status":415,"error":"Unsupported Media Type","message":"","path":"/api3"}
curl -XPOST -H 'Content-Type:application/json' http://127.0.0.1:8000/api3 -d '{"id":1,"name":"tiger"}'
springboot @RequestBody 注解的接口 ,
使用post 且 设置 header Content-Type:application/json
postman post json 数据
http://127.0.0.1:8000/api3
raw
JSON(application/json)
{"id":1,"name":"tiger"}
https://gitee.com/dyyx/hellocode/raw/master/web/images/postman-post-json.png
完整代码
https://gitee.com/dyyx/demos/tree/master/springboot_api
curl实用技巧
postman中form-data x-www-form-urlencoded raw binary的区别
上一篇
下一篇
Dubbo Filter 顺序
dubbo 自定义 filter 实例
TBA聊考研
基于 HttpURLConnection 的 http 客户端
Spring5.2 @Configuration 属性 proxyBeanMethods 作用
aerospike 乐观锁实例