user1735826
user1735826

Reputation: 31

Posting JSON to Restlet using xstream and jettison results in "Unsupported Media Type"

I am using the jse edition of Restlet 2.1.0 with the xstream and jettison extensions that enable me to have a resource that is declared like this:

@Get("json")
public Customer retrieve();

This allows me to have a client that can choose whether to get a JSON string or get a Customer object, with xstream+jettison doing the serialization/deserialization.

I also have a resource method declared like this:

@Post
public Customer store(Customer customer);

Which I can post to using something like:

clientResource.post(customer, Customer.class);

This all seems to work nicely. However the one thing that doesn't work is the simplest scenario, of posting a JSON String to this Post Restlet resource.

When I try to post the following JSON String:

{"com.redprairie.task.common.Customer":{"firstName":"George","lastName":"Shaw","birthDate":"1856-07-26 07:00:00.0 UTC","address":"Bibbs Hall Lane, Ayot St. Lawrence, AL6 9BX  United Kingdom"}}

I get this stack trace:

<failure message="Unsupported Media Type (415) - Unsupported Media Type" type="org.restlet.resource.ResourceException">Unsupported Media Type (415) - Unsupported Media Type
    at org.restlet.resource.ClientResource.doError(ClientResource.java:612)
    at org.restlet.resource.ClientResource.handleInbound(ClientResource.java:1203)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1070)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1087)
    at org.restlet.resource.ClientResource.post(ClientResource.java:1438)
    at com.redprairie.task.common.rest.RestletResource.post(RestletResource.java:99)
    at com.redprairie.task.common.rest.ServerResourceTest.testPostMethodUsingJSON(ServerResourceTest.java:165)

I'm guessing there is something simple that I'm doing wrong, but I'm not sure what it is.

Upvotes: 1

Views: 902

Answers (2)

user1735826
user1735826

Reputation: 31

I was able to resolve my issue.

I was trying to post a JSON string using:

String jsonString = "{some valid json}";
clientResource.post(jsonString, MediaType.APPLICATION_JSON);

That didn't work. Not sure exactly why it didn't work, but doing it the following way works:

Representation rep = new StringRepresentation(jsonString, MediaType.APPLICATION_JSON);
clientResource.post(rep);

Upvotes: 1

avandecreme
avandecreme

Reputation: 979

Are you specifying the content-type in your POST request header?

Upvotes: 0

Related Questions