Swagger 415 不支持的媒体类型:application/json 而不是 POST 请求中的 text/plain
Swagger 415 Unsupported media type: application/json instead of text/plain in POST request
我有两个 REST-GUI 来测试我的 REST 应用程序。
一个是用 swagger 创建的,另一个是在 Chrome.
中运行的 Advanced REST Client
我对服务执行 post,Swagger 失败并出现错误 415,Advanced Rest Client 成功。
两者的区别在于请求头中的Content-Type。 OK 版本具有 Content-Type:text/plain
错误版本有:内容类型:application/json
其余的,两者完全相同。我切断了有效负载,但它们在两种情况下也完全相同。
我不知道如何在 Swagger 中更改 Content-Type,如果可能并且需要的话。
以下是您可能需要帮助我解决此问题的信息。如果您需要更多信息,请需要。
非常感谢您的帮助。
此致。
信息如下:
确定:高级 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
有效负载(片段):
{"archetype": "archetype (adl_version\u003d1.4)\n\top........
回复:
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
错误:Swagger
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
有效负载(片段):
{"archetype": "archetype (adl_version\u003d1.4)\n\top........
回应
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
我自己找到了答案。我需要在 Rest 资源层中为 @Post-annotation 添加一个 mediatype,就像这样
@Post("json")
我也遇到了类似的问题。在我的例子中,我的 REST 客户端 API 调用正常,但 API 通过 Swagger 调用失败。
REST client -> content-type:application/json
Swagger -> content-type:text/plain
我不明白为什么我的大摇大摆将 post 调用的内容类型更改为 "text/plain"
。
如果这对以后的人有帮助,
在我的例子中错误:
{
"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
[...]
}
我输入了@RequestMapping:
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
@RequestMapping(value = "/products",
consumes= APPLICATION_JSON_VALUE,
produces = APPLICATION_JSON_VALUE)
public class ProductResource {
[...]
}
但我意识到产生此错误的是 'consumes',将其从 @RequestMapping 中删除,一切都在 Swagger 界面上运行 ui。
对:
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
@RequestMapping(value = "/products",
produces = APPLICATION_JSON_VALUE)
public class ProductResource {
[...]
}
在 Swagger Ui 上遇到同样的问题,从 @RequestMapping 中删除“Consumes”对我也有效。
这对我有用
headers: <String, String>{
'Content-Type': 'application/json',
},
最初是这样的:
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
而后一个从 GO swagger 后端返回了这个 json,
{code: 415, message: unsupported media type "text/plain", only
[application/json] are allowed}
对我来说,我的问题是 @RequestBody
的组合使用,即最初,我的 API 端点看起来像:
public ResponseEntity<?> editDetails(@RequestBody @Valid
EditDetailsRequestDto requestDto, @RequestPart @ApiParam(value="File")
MultipartFile businessLicense) { ...
但是当我将 @RequestBody
更改为一堆 @RequestParam
时,它修复了 415 错误。
我有两个 REST-GUI 来测试我的 REST 应用程序。 一个是用 swagger 创建的,另一个是在 Chrome.
中运行的 Advanced REST Client我对服务执行 post,Swagger 失败并出现错误 415,Advanced Rest Client 成功。
两者的区别在于请求头中的Content-Type。 OK 版本具有 Content-Type:text/plain 错误版本有:内容类型:application/json
其余的,两者完全相同。我切断了有效负载,但它们在两种情况下也完全相同。
我不知道如何在 Swagger 中更改 Content-Type,如果可能并且需要的话。
以下是您可能需要帮助我解决此问题的信息。如果您需要更多信息,请需要。
非常感谢您的帮助。
此致。
信息如下:
确定:高级 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
有效负载(片段):
{"archetype": "archetype (adl_version\u003d1.4)\n\top........
回复:
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
错误:Swagger
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
有效负载(片段):
{"archetype": "archetype (adl_version\u003d1.4)\n\top........
回应
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
我自己找到了答案。我需要在 Rest 资源层中为 @Post-annotation 添加一个 mediatype,就像这样
@Post("json")
我也遇到了类似的问题。在我的例子中,我的 REST 客户端 API 调用正常,但 API 通过 Swagger 调用失败。
REST client -> content-type:application/json
Swagger -> content-type:text/plain
我不明白为什么我的大摇大摆将 post 调用的内容类型更改为 "text/plain"
。
如果这对以后的人有帮助,
在我的例子中错误:
{
"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
[...]
}
我输入了@RequestMapping:
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
@RequestMapping(value = "/products",
consumes= APPLICATION_JSON_VALUE,
produces = APPLICATION_JSON_VALUE)
public class ProductResource {
[...]
}
但我意识到产生此错误的是 'consumes',将其从 @RequestMapping 中删除,一切都在 Swagger 界面上运行 ui。
对:
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
@RequestMapping(value = "/products",
produces = APPLICATION_JSON_VALUE)
public class ProductResource {
[...]
}
在 Swagger Ui 上遇到同样的问题,从 @RequestMapping 中删除“Consumes”对我也有效。
这对我有用
headers: <String, String>{
'Content-Type': 'application/json',
},
最初是这样的:
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
而后一个从 GO swagger 后端返回了这个 json,
{code: 415, message: unsupported media type "text/plain", only [application/json] are allowed}
对我来说,我的问题是 @RequestBody
的组合使用,即最初,我的 API 端点看起来像:
public ResponseEntity<?> editDetails(@RequestBody @Valid EditDetailsRequestDto requestDto, @RequestPart @ApiParam(value="File") MultipartFile businessLicense) { ...
但是当我将 @RequestBody
更改为一堆 @RequestParam
时,它修复了 415 错误。