java - Swagger 415 不支持的媒体类型 : application/json instead of text/plain in POST request

标签 java json rest content-type swagger-ui

我有两个 REST-GUI 来测试我的 REST 应用程序。 一种是使用 swagger 创建的,另一种是在 Chrome 中运行的高级 REST 客户端。

我向服务发布消息,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
<小时/>

错误:大摇大摆

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

最佳答案

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

就我而言,错误:

{
  "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 {

 [...]

}

但我意识到生成此错误的是“消耗”,将其从 @RequestMapping 中删除,在 Swagger 界面 ui 上运行的所有内容都有效。

右:

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

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

 [...]

}

关于java - Swagger 415 不支持的媒体类型 : application/json instead of text/plain in POST request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28245362/

相关文章:

Java 用异常处理覆盖 compareTo

java - Android - Viewmodel 变大

java - 将 Retrofit 与 Moshi 结合使用,在执行 POST、PUT 或 PATCH 时如何忽略特定对象属性

php - 如何在服务器端访问 PHP REST API PUT 数据?

java - JButton 更改文本字段的文本颜色

Java Apache HTTP 错误 : Receiving CRLF expected at end of chunk

php - json_decode 不适用于我的变量,但适用于它的内容

php - 数据类型 json 的 jQuery $.ajax 请求不会从 PHP 脚本中检索数据

java - 如何在 JAX-RS 中管理状态?

json - 从 react 组件进行 REST 调用