codegen - consume json and xml - produces json and xml - error 415 Unsupported Media Type

914 views
Skip to first unread message

Luca Toldo

unread,
Jul 18, 2017, 10:59:23 AM7/18/17
to Swagger
Dear Swaggers,
i need to have a jaxrs service accepting POST in JSON or XML and returning corresponding type (JSON for JSON, XML for XML).
For this reason I've implemented a yaml with the following sections both in the header side of the yaml and also in the paths section.

consumes:
- "application/json"
- "application/xml"
produces:
- "application/json"
- "text/xml"

codegen generate produces corresponding  code in io.swagger.api.mymethod.java

@Path("/mymethod")
@Consumes({ "application/json","application/xml" })
@Produces({ "application/json","text/xml" })

and also inside the class

@POST  
    @Consumes({ "application/json","application/xml" })
    @Produces({ "application/json","text/xml" })



Compilation proceeds fine, and calling with application/json content type works fine.
However when I curl with application/xml or text/xml then I get 415.

curl -X POST --header 'Content-Type: application/xml' --header 'Accept: text/xml' -d '<?xml version="1.0"?><cas:Sofa xmi:id="1" sofaNum="1" text="Aspirin"/>' http://localhost:8080/my/api/v2/doit/mymethod
<!doctype html><html lang="en"><head><title>HTTP Status 415 – Unsupported Media Type</title><style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 415 – Unsupported Media Type</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> Unsupported Media Type</p><p><b>Description</b> The origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource.</p><hr class="line" /><h3>Apache Tomcat/8.5.16</h3></body></html>

any advice is appreciated.
Thankyou


Luca Toldo

unread,
Jul 19, 2017, 6:46:26 AM7/19/17
to Swagger

Xin Meng

unread,
Jul 5, 2018, 10:44:44 AM7/5/18
to Swagger
I have the same issue, I have tried the solution to add HeaderParam in the API definition, but it still does not work, could you share your solution to figure out this 415 Error message issue? 

On Wednesday, 19 July 2017 11:46:26 UTC+1, Luca Toldo wrote:
Reply all
Reply to author
Forward
0 new messages