I am using the Jersey implementation of JAX-RS. I would like to POST a JSON object to this service but I am getting an error code 415 Unsupported Media Type. What am I missing?
Here's my code:
@Path("/orders")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class OrderResource {
private static Map<Integer, Order> orders = new HashMap<Integer, Order>();
@POST
public void createOrder(Order order) {
orders.put(order.id, order);
}
@GET
@Path("/{id}")
public Order getOrder(@PathParam("id") int id) {
Order order = orders.get(id);
if (order == null) {
order = new Order(0, "Buy", "Unknown", 0);
}
return order;
}
}
Here's the Order object:
public class Order {
public int id;
public String side;
public String symbol;
public int quantity;
...
}
A GET request like this works perfectly and returns an order in JSON format:
GET http://localhost:8080/jaxrs-oms/rest/orders/123 HTTP/1.1
However a POST request like this returns a 415:
POST http://localhost:8080/jaxrs-oms/rest/orders HTTP/1.1
{
"id": "123",
"symbol": "AAPL",
"side": "Buy",
"quantity": "1000"
}
I faced the same
415
http error when sending objects, serialized into JSON, via PUT/PUSH requests to my JAX-rs services, in other words my server was not able to de-serialize the objects from JSON. In my case, the server was able to serialize successfully the same objects in JSON when sending them into its responses.As mentioned in the other responses I have correctly set the
Accept
andContent-Type
headers toapplication/json
, but it doesn't suffice.Solution
I simply forgot a default constructor with no parameters for my DTO objects. Yes this is the same reasoning behind @Entity objects, you need a constructor with no parameters for the ORM to instantiate objects and populate the fields later.
Adding the constructor with no parameters to my DTO objects solved my issue. Here follows an example that resembles my code:
Wrong
Right
I lost hours, I hope this'll save yours ;-)
Jersey makes the process very easy, my service class worked well with JSON, all I had to do is to add the dependencies in the pom.xml
And in the pom.xml
The answer was surprisingly simple. I had to add a
Content-Type
header in thePOST
request with a value ofapplication/json
. Without this header Jersey did not know what to do with the request body (in spite of the@Consumes(MediaType.APPLICATION_JSON)
annotation)!