Swagger 415 不支持的媒体类型:POST 请求中的 application/json 而不是 text/plain

Ber*_*ees 2 java rest json content-type swagger-ui

我有两个 REST-GUI 来测试我的 REST 应用程序。一个是使用 swagger 创建的,另一个是在 Chrome 中运行的 Advanced REST Client。

我发布了一个服务,Swagger 失败并显示错误 415,而 Advanced Rest Client 成功。

两者的区别在于请求头中的Content-Type。OK 版本具有 Content-Type:text/plain 错误版本具有:Content-Type:application/json

至于其余部分,两者完全相同。我切断了有效载荷,但在两种情况下它们也完全相同。

如果可能以及是否需要,我不知道如何在 Swagger 中更改 Content-Type。

以下是您可能需要帮助我解决此问题的信息。如果您需要更多信息,请需要。

非常感谢您的帮助。

此致。

信息如下:


OK:高级 REST 客户端

Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/oak-kernel-2.0/oak/archetype
Request Method:POST
Status Code:200 OK

POST /oak-kernel-2.0/oak/archetype HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 10642
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ....
Content-Type: text/plain
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,nl;q=0.6
Cookie: _ga=GA1.1.1597438054.1422608427
Run Code Online (Sandbox Code Playgroud)

有效载荷(片段):

{"archetype": "archetype (adl_version\u003d1.4)\n\top........
Run Code Online (Sandbox Code Playgroud)

回复:

Accept-Ranges:bytes
Content-Length:87
Content-Type:application/json;charset=UTF-8
Date:Fri, 30 Jan 2015 20:59:49 GMT
Server:Restlet-Framework/2.2.3
Vary:Accept-Charset, Accept-Encoding, Accept-Language, Accept
Run Code Online (Sandbox Code Playgroud)

错误:招摇

Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/oak-kernel-2.0/oak/archetype
Request Method:POST
Status Code:415 Unsupported Media Type

POST /oak-kernel-2.0/oak/archetype HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 10642
Accept: application/json
Origin: http://localhost:8080
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ....
Content-Type: application/json
Referer: http://localhost:8080/swagger/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,nl;q=0.6
Cookie: _ga=GA1.1.1597438054.1422608427
Run Code Online (Sandbox Code Playgroud)

有效载荷(片段):

{"archetype": "archetype (adl_version\u003d1.4)\n\top........
Run Code Online (Sandbox Code Playgroud)

回复

Accept-Ranges:bytes
Content-Length:554
Content-Type:text/html;charset=UTF-8
Date:Fri, 30 Jan 2015 21:28:12 GMT
Server:Restlet-Framework/2.2.3
Run Code Online (Sandbox Code Playgroud)

Pol*_*nta 6

如果这对未来的任何人有帮助,

在我的情况下,错误:

{
  "timestamp": "2019-01-22T18:23:48.989+0000",
  "status": 415,
  "error": "Unsupported Media Type",
  "message": "Content type '' not supported",
  "trace": "org.springframework.web.HttpMediaTypeNotSupportedException: Content type '' not supported

  [...]
}
Run Code Online (Sandbox Code Playgroud)

我正在输入@RequestMapping:

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@RequestMapping(value = "/products", 
                consumes= APPLICATION_JSON_VALUE, 
                produces = APPLICATION_JSON_VALUE)
public class ProductResource {

 [...]

}
Run Code Online (Sandbox Code Playgroud)

但我意识到产生这个错误的是'consumes',从@RequestMapping 中删除它,一切都在Swagger 界面ui 上工作。

对:

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@RequestMapping(value = "/products", 
                produces = APPLICATION_JSON_VALUE)
public class ProductResource {

 [...]

}
Run Code Online (Sandbox Code Playgroud)

  • 当您使用“@RequestMapping”而不是“@PostMapping”或“@GetMapping”时,您必须指定HTTP 请求方法(例如“method = RequestMethod.GET”或“method = RequestMethod.POST”)。如果您使用 GET,则为请求正文指定内容类型是没有意义的,并且会产生错误。 (2认同)