user798719
user798719

Reputation: 9869

troubleshooting JAXBUnmarshalException: Unexpected end of stream (RESTeasy)

I would appreciate if someone could help me formulate a strategy for fixing the following error, which occurs in no discernible pattern; most of the time there is no error, and I am successful about 90% of the time:

ERROR [org.jboss.resteasy.core.SynchronousDispatcher]  Failed executing POST /toys/customer25/insert
org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException: Unexpected end of stream

How would I log this error simply to the console? I'd like to capture the JSON that this method is receiving and figure out what is causing this error. I would be able to see it in the server logs.

What I am doing is POSTing an array of Toy objects to the server, in JSON format. That is, on the client I transform my Toy object into a Toy JSON. The string that I am sending is like this: [{"toyName":"buzzlightyear", "toyMaker":"mattel"}]

My understanding is that when I POST JSON to a jax-rs restful service, a jax-rs implementation (I'm using RESTeasy 1.2) will somehow automagically convert that JSON string into a java Toy object.

My code on the Java JAX-RS restful service for handling a POST which inserts a Toy object is this:

@POST
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.TEXT_PLAIN})
@Path("toy/{customer}/insert")
public String insertToys(@PathParam("customer") String customer, List<Toy> toys);

How do I log/capture what is wrong with the JSON string? It seems to me that the system is handling the conversion of the JSON to Toy object right when the insertToys() method is called. So it's hard form to figure out how to debug.

Can someone formulate a strategy for me? What is the likelihood that I am passing in malformed JSON, maybe from some unescaped character? Vs mapping entities wrong? Thanks!

Upvotes: 1

Views: 352

Answers (1)

Carlo Pellegrini
Carlo Pellegrini

Reputation: 5686

Your JSON is incorrect. The method accepts List, so you must send an array of Toy Objects:

[{"toyName":"buzzlightyear", "toyMaker":"mattel"}]

(note the braces).

For more objects:

[{"toyName":"buzzlightyear", "toyMaker":"mattel"},{"toyName":"rex", "toyMaker":"mattel"}]

Upvotes: 1

Related Questions