Swagger 415错误:POST请求中使用了application/json而非text/plain的不支持的媒体类型。

4
我有两个REST-GUI来测试我的REST应用程序。一个是使用Swagger创建的,另一个是在Chrome中运行的Advanced REST Client。
我向服务发送POST请求时,Swagger会出现错误415,而Advanced Rest Client则成功了。
两者之间的区别在于请求头中的Content-Type。OK版本具有Content-Type:text / plain,错误版本具有Content-Type:application / json。
对于其余部分,两者都非常相似。我切断了有效载荷,但在两种情况下它们也完全相同。
我不知道如何更改Swagger中的Content-Type,如果可以并且需要更改的话。
以下是您可能需要帮助我的信息。如果您需要更多信息,请告诉我。
非常感谢您的任何帮助。
最好的问候。
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

ERROR: 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
6个回答

9
如果这对未来的任何人有所帮助,
在我的情况下,错误是:
{
  "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

  [...]
}

I was putting in the @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 {

 [...]

}

3
当您使用"@RequestMapping"而不是"@PostMapping"或"@GetMapping"时,您必须指定HTTP请求方法(例如"method = RequestMethod.GET"或"method = RequestMethod.POST")。如果您正在使用GET,则为请求正文指定内容类型毫无意义,并且会创建一个错误。 - Tristan

3

我也遇到了类似的问题。在我的情况下,我的REST客户端API调用是正常的,但是通过Swagger进行的API调用失败了。

REST client -> content-type:application/json

Swagger -> content-type:text/plain

我不明白为什么我的swagger在进行post请求时会将content-type更改为"text/plain"。

1
我找到了答案。我需要在Rest资源层的@Post注释中添加一个媒体类型,就像这样:
@Post("json")

0

我在Swagger UI上遇到了同样的问题,从@RequestMapping中删除"Consumes"对我也起作用。


0
对我来说,我的问题是同时使用了 @RequestBody。原本,我的API端点看起来像这样:

public ResponseEntity<?> editDetails(@RequestBody @Valid EditDetailsRequestDto requestDto, @RequestPart @ApiParam(value="File") MultipartFile businessLicense) { ...

但当我将 @RequestBody 更改为一堆 @RequestParam 时,它修复了415错误。

0

这对我有用

headers: <String, String>{
        'Content-Type': 'application/json',
      },

最初,它是这样的:

    headers: <String, String>{
            'Content-Type': 'application/json; charset=UTF-8',
      },

后来那个GO swagger后端返回了这个JSON:

{code: 415, message: 不支持的媒体类型"text/plain",只允许使用[application/json]}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接