Error 415 Unsupported Media Type: POST not reaching REST if JSON, but it does if XML
The issue is in the deserialization of the bean Customer. Your programs knows how to do it in XML, with JAXB as Daniel is writing, but most likely doesn't know how to do it in JSON.
Here you have an example with Resteasy/Jackson
http://www.mkyong.com/webservices/jax-rs/integrate-jackson-with-resteasy/
The same with Jersey:
http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/
REST Webservice returning 415 - Unsupported Media Type
You need to send the request-header Content-Type: application/json
. Seems like REST-Client does not add this header automatically for you.
HTTP Error 415 Unsupported Media Type on REST service operation with an XML parameter (Jersey + Jetty )
There's a missconfiguration of the maven-shade plugin. Replacing it by
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin.version}</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
solves the problem
HTTP 415 unsupported media type error when calling Web API 2 endpoint
SOLVED
After banging my head on the wall for a couple days with this issue, it was looking like the problem had something to do with the content type negotiation between the client and server. I dug deeper into that using Fiddler to check the request details coming from the client app, here's a screenshot of the raw request as captured by fiddler:
What's obviously missing there is the Content-Type
header, even though I was setting it as seen in the code sample in my original post. I thought it was strange that the Content-Type
never came through even though I was setting it, so I had another look at my other (working) code calling a different Web API service, the only difference was that I happened to be setting the req.ContentType
property prior to writing to the request body in that case. I made that change to this new code and that did it, the Content-Type
was now showing up and I got the expected success response from the web service. The new code from my .NET client now looks like this:
req.Method = "POST"
req.ContentType = "application/json"
lstrPagingJSON = JsonSerializer(Of Paging)(lPaging)
bytData = Encoding.UTF8.GetBytes(lstrPagingJSON)
req.ContentLength = bytData.Length
reqStream = req.GetRequestStream()
reqStream.Write(bytData, 0, bytData.Length)
reqStream.Close()
'// Content-Type was being set here, causing the problem
'req.ContentType = "application/json"
That's all it was, the ContentType
property just needed to be set prior to writing to the request body
I believe this behavior is because once content is written to the body it is streamed to the service endpoint being called, any other attributes pertaining to the request need to be set prior to that. Please correct me if I'm wrong or if this needs more detail.
Java Jersey Consume Response is giving back 415 (unsupported media type)
The Waypoint.java produces an xml and @Path("/waypoint") seems to consume a JSON as well as produce a JSON. I think thats why you are getting unsupported media type error
Http 415 Unsupported Media type error with JSON
Not sure about the reason but Removing lines charset=utf8
from con.setRequestProperty("Content-Type", "application/json; charset=utf8")
resolved the issue.
Related Topics
N-Gram Generation from a Sentence
Checked VS Unchecked Exceptions in Java
Use Class Name as Root Key for JSON Jackson Serialization
Java.Sql.Sqlexception: No Suitable Driver Found for Jdbc:Mysql://Localhost:3306/Dbname
Convert Two Dimensional Array to List in Java
Creating Unicode Character from Its Number
Best Way to Create a Hashmap of Arraylist
Blank PDF Even with the Simplest Jasperreport Jrxml
Create Bar Chart in Excel with Apache Poi
Why Is Constructor of Super Class Invoked When We Declare the Object of Sub Class? (Java)
Steganography in Lossy Compression (Java)
Differencebetween the 'Shell' Channel and the 'Exec' Channel in Jsch
Using Javafx.Beans Properties in Model Classes
Why Should I Use Concurrent Characteristic in Parallel Stream with Collect
Information About _Java_Options
What Does "An Arbitrary Object of a Particular Type" Mean in Java 8