HTTP Status 415 – Unsupported Media Type

HTTP Status 415 – Unsupported Media Type

今天在测试springmvc的restful接口时候遇到了一个问题:通过body传参报错
HTTP Status 415 – Unsupported Media Type

简述restful接口传参方式

restful推荐的传参方式:

1.get/delete请求RequestParam ,请求的url类似于http://localhost:8080/springmvc/passValue/rest/param?name=root&password=123,
接收方法
@GetMapping("/param")
public String getByParam(@RequestParam String name, @RequestParam String password) (PS:@RequestParam是默认接收参数的方式,可以不写)
@DeleteMapping("/param")
public String deleteByParam(@RequestParam String name, @RequestParam String password)

2.get/delete请求PathVariable 请求的url类似于http://localhost:8080/springmvc/passValue/rest/root/123,接收方法
@GetMapping("/path/{name}/{password}")
public String getByPath(@PathVariable String name, @PathVariable String password)
@DeleteMapping("/path/{name}/{password}")
public String deleteByPath(@PathVariable String name, @PathVariable String password)

3.post/put请求body ,请求的url类似于http://localhost:8080/springmvc/passValue/rest/body,接收方法
@PostMapping("/body")
public String postByBody(@RequestBody User user)
@PutMapping("/body")
public String putByBody(@RequestBody User user)

post/put请求通过body体获取参数需要三步

①方法形参使用@RequestBody修饰(说明通过请求的body获取参数)
②请求增加http头字段Content-Type:application/json(说明body体里面的参数是json格式)
③引入jackson-annotations,jackson-core,jackson-databind包(解析body体的json参数)

你可能感兴趣的:(HTTP Status 415 – Unsupported Media Type)