【问题标题】:Swagger 415 Unsupported media type: application/json instead of text/plain in POST requestSwagger 415 不支持的媒体类型:POST 请求中的应用程序/json 而不是 text/plain
【发布时间】:2015-03-30 11:42:34
【问题描述】:

我有两个 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,如果可能以及是否需要。

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

非常感谢您的帮助。

最好的问候。

信息如下:


确定:高级 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

【问题讨论】:

    标签: java json rest content-type swagger-ui


    【解决方案1】:

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

    在我的情况下,错误:

    {
      "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 {
    
     [...]
    
    }
    

    【讨论】:

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

    我也面临类似的问题。在我的情况下,我的 REST 客户端 API 调用正常,但通过 Swagger 的 API 调用失败。

    REST client -> content-type:application/json
    
    Swagger -> content-type:text/plain
    

    我不明白为什么我大摇大摆地将内容类型更改为 "text/plain" 以进行 post call。

    【讨论】:

      【解决方案3】:

      我自己找到了答案。我需要在 Rest 资源层的 @Post-annotation 中添加一个媒体类型,像这样

      @Post("json")
      

      【讨论】:

        【解决方案4】:

        在 Swagger Ui 上面临同样的问题,从 @RequestMapping 中删除“Consumes”也对我有用。

        【讨论】:

          【解决方案5】:

          这对我有用

          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”,仅限 [应用程序/json] 允许}

          【讨论】:

            【解决方案6】:

            对我来说,我的问题是 @RequestBody 的组合使用,即最初,我的 API 端点看起来像:

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

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

            【讨论】:

              猜你喜欢
              • 2015-06-17
              • 2016-11-21
              • 2018-07-12
              • 1970-01-01
              • 2019-01-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多