Error 415 Unsupported Media Type: POST not reaching REST if JSON, but it does if XML
JavaJsonWeb ServicesRestPostJava Problem Overview
I am actually new to REST WS but really I don't get this 415 Unsupported Media Type
.
I am testing my REST with Poster on Firefox and the GET
works fine for me, also the POST
(when it's a application/xml
) but when I try application/json
it doesn't not reach the WS at all, the server rejects it.
This is my URL: http:// localhost:8081/RestDemo/services/customers/add
This is JSON
I'm sending: {"name": "test1", "address" :"test2"}
This is XML
I'm sending:
<customer>
<name>test1</name>
<address>test2</address>
</customer>
and this is my Resource class:
@Produces("application/xml")
@Path("customers")
@Singleton
@XmlRootElement(name = "customers")
public class CustomerResource {
private TreeMap<Integer, Customer> customerMap = new TreeMap<Integer, Customer>();
public CustomerResource() {
// hardcode a single customer into the database for demonstration
// purposes
Customer customer = new Customer();
customer.setName("Harold Abernathy");
customer.setAddress("Sheffield, UK");
addCustomer(customer);
}
@GET
@XmlElement(name = "customer")
public List<Customer> getCustomers() {
List<Customer> customers = new ArrayList<Customer>();
customers.addAll(customerMap.values());
return customers;
}
@GET
@Path("/{id}")
@Produces("application/json")
public String getCustomer(@PathParam("id") int cId) {
Customer customer = customerMap.get(cId);
return "{\"name\": \" " + customer.getName() + " \", \"address\": \"" + customer.getAddress() + "\"}";
}
@POST
@Path("/add")
@Produces(MediaType.APPLICATION_JSON)
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public String addCustomer(Customer customer) {
//insert
int id = customerMap.size();
customer.setId(id);
customerMap.put(id, customer);
//get inserted
Customer result = customerMap.get(id);
return "{\"id\": \" " + result.getId() + " \", \"name\": \" " + result.getName() + " \", \"address\": \"" + result.getAddress() + "\"}";
}
}
EDIT 1:
This is my Customer class:
@XmlRootElement
public class Customer implements Serializable {
private int id;
private String name;
private String address;
public Customer() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Java Solutions
Solution 1 - Java
Add Content-Type: application/json
and Accept: application/json
in REST Client header section
Solution 2 - Java
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/
Solution 3 - Java
Just in case this is helpful to others, here's my anecdote:
I found this thread as a result of a problem I encountered while I was using Postman to send test data to my RESTEasy server, where- after a significant code change- I was getting nothing but 415 Unsupported Media Type errors.
Long story short, I tore everything out, eventually I tried to run the trivial file upload example I knew worked; it didn't. That's when I realized that the problem was with my Postman request. I normally don't send any special headers, but in a previous test I had added a "Content-Type": "application/json" header. OF COURSE, I was trying to upload "multipart/form-data." Removing it solved my issue.
Moral: Check your headers before you blow up your world. ;)
Solution 4 - Java
I had this issue and found that the problem was that I had not registered the JacksonFeature class:
// Create JAX-RS application.
final Application application = new ResourceConfig()
...
.register(JacksonFeature.class);
Without doing this your application does not know how to convert the JSON to a java object.
https://jersey.java.net/documentation/latest/media.html#json.jackson
Solution 5 - Java
I had the same problem: > curl -v -H "Content-Type: application/json" -X PUT -d '{"name":"json","surname":"gson","married":true,"age":32,"salary":123,"hasCar":true,"childs":["serkan","volkan","aybars"]}' XXXXXX/ponyo/UserModel/json
* About to connect() to localhost port 8081 (#0)
* Trying ::1...
* Connection refused
* Trying 127.0.0.1...
* connected
* Connected to localhost (127.0.0.1) port 8081 (#0)
> PUT /ponyo/UserModel/json HTTP/1.1
> User-Agent: curl/7.28.1
> Host: localhost:8081
> Accept: */*
> Content-Type: application/json
> Content-Length: 121
>
* upload completely sent off: 121 out of 121 bytes
< HTTP/1.1 415 Unsupported Media Type
< Content-Type: text/html; charset=iso-8859-1
< Date: Wed, 09 Apr 2014 13:55:43 GMT
< Content-Length: 0
<
* Connection #0 to host localhost left intact
* Closing connection #0
I resolved it by adding the dependency to pom.xml as follows. Please try it.
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>0.98</version>
</dependency>
Solution 6 - Java
I get the same issue. In fact, the request didn't reach the jersey annoted method. I solved the problem with add the annotation: @Consumes(MediaType.APPLICATION_FORM_URLENCODED) The annotation @Consumes("/") don't work!
@Path("/"+PropertiesHabilitation.KEY_EstNouvelleGH)
@POST
//@Consumes("*/*")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void get__EstNouvelleGH( @Context HttpServletResponse response) {
...
}
Solution 7 - Java
Don't return Strings in your methods but Customer objects it self and let JAXB take care of the de/serialization.
Solution 8 - Java
I had the same issue, as it was giving error-415-unsupported-media-type while hitting post call using json while i already had the
@Consumes(MediaType.APPLICATION_JSON)
and request headers as in my restful web service using Jersey 2.0+
Accept:application/json
Content-Type:application/json
I resolved this issue by adding following dependencies to my project
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.25</version>
</dependency>
this will add following jars to your library :
- jersey-media-json-jackson-2.25.jar
- jersey-entity-filtering-2.25.jar
- jackson-module-jaxb-annotations-2.8.4.jar
- jackson-jaxrs-json-provider-2.8.4.jar
- jackson-jaxrs-base-2.8.4.jar
I hope it will works for others too as it did for me.
Solution 9 - Java
just change the content-type to application/json when you use JSON with POST/PUT, etc...
Solution 10 - Java
In case you are trying to use ARC app from google and post a XML
and you are getting this error, then try changing the body content type to application/xml
. Example here
Solution 11 - Java
I encountered the same issue in postman, i was selecting Accept in header section and providing the value as "application/json". So i unchecked and selected Content-Type and provided the value as "application/json". That worked!
Solution 12 - Java
If none of the solution worked and you are working with JAXB, then you need to annotate your class with @XmlRootElement
to avoid 415 unsupported media type
Solution 13 - Java
I had similar problem while using in postman. for POST request under header section add these as
key:valuepair
Content-Type:application/json Accept:application/json
i hope it will work.