【问题标题】:How to solve error message "status": 415, "error": "Unsupported Media Type"," for DELETE request in Spring?如何解决 Spring 中 DELETE 请求的错误消息“status”:415,“error”:“Unsupported Media Type”?
【发布时间】:2018-08-05 12:34:05
【问题描述】:

我基于Service Components using this tutorial.创建了一个Spring Boot应用

我的删除请求是这样构造的:

   @RequestMapping(value = "/api/greetings/{id}", method = RequestMethod.DELETE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Greeting> deleteGreeting(@PathVariable("id") Long id, @RequestBody Greeting greeting) {


    greetingService.delete(id);

    return new ResponseEntity<Greeting>(HttpStatus.NO_CONTENT);
    }

所有其他请求最终都可以正常工作,但是如果我在 Postman 中发出 DELETE 请求,则会收到以下错误:

   {
"timestamp": 1519060345434,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'application/x-www-form-urlencoded' not supported",
"path": "/api/greetings/2"

}

我检查了以下问题,没有任何帮助(难怪,它们都没有发出 DELETE 请求:

415 Unsupported MediaType

415 Unsupported MediaType for POST request in spring application

415 Unsupported Media Type in RESTful webservice Ask

【问题讨论】:

  • 您可以使用@RestController,如果您制作一些api,并为您的删除功能使用@DeleteMapping注释。

标签: maven spring-boot request spring-data postman


【解决方案1】:

您的控制器期望 application/json 作为内容类型,但显示错误消息

"message": "Content type 'application/x-www-form-urlencoded' not supported",

你应该将 postman 中的 header 更改为 content-type application/json

【讨论】:

    猜你喜欢
    • 2012-04-19
    • 1970-01-01
    • 2021-07-16
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多