Swagger 415 Unsupported media type: application/json instead of text/plain in POST request
Solution 1
If this helps anyone in the future,
In my case the error:
{
"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 {
[...]
}
But I realized that what was generating this error was the 'consumes', removing it from @RequestMapping everything worked on the Swagger interface ui.
right:
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
@RequestMapping(value = "/products",
produces = APPLICATION_JSON_VALUE)
public class ProductResource {
[...]
}
Solution 2
I am also facing the similar issue. In my case my REST client API call is ok but API call through Swagger fails.
REST client -> content-type:application/json
Swagger -> content-type:text/plain
I don't understand why my swagger is changing the content-type to "text/plain"
for the post call.
Solution 3
I found the answer myself. I need to put a mediatype to the @Post-annotation in the Rest resource layer, like this
@Post("json")
Bert Verhees
Golang and Angular, much experience in domain healthcare related software. Years of experience in object oriented programming and service architectures. Contact me, I am, as always, available for new challenges. In employment relation or as independent developer.
Updated on June 05, 2022Comments
-
Bert Verhees almost 2 years
I have two REST-GUI's to test my REST-application. One is created with swagger, and the other is Advanced REST Client which runs in Chrome.
I do a post to a service, the Swagger fails with error 415, and the Advanced Rest Client succeeds.
The difference between the two is the Content-Type in the request header. The OK-version has Content-Type: text/plain The Error version has: Content-Type: application/json
For the rest, both are quite the same. I cut off the payload, but they are also exact the same in both cases.
I don't know how to change Content-Type in Swagger, if it is possible and if it is needed.
Below is information which you could need to help me in this issue. If you need more info, please need.
Thanks very much for any help.
Best regards.
The information look like:
OK: Advanced REST Client
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
Payload (fragment):
{"archetype": "archetype (adl_version\u003d1.4)\n\top........
Response:
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
Payload (fragment):
{"archetype": "archetype (adl_version\u003d1.4)\n\top........
Response
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