Em Ae
Em Ae

Reputation: 8704

Parsing Jersey inputstream to JacksonObject

I have a REST WS implemented using Jersey/Jackson. The method which has been implemented is PUT and it is working fine until i get an empty or null body.

After googling a bit, I realized that it is a known issue and there are couple of work arounds available. One of the work around that i found (and implemented) is using ContentRequestFilter to intercept calls, do basic checks and decide what to do.

But in that case, I have to check if the call is for that specific method. I don't like this since what if the method changes in future ?

What i want is to receive as InputStream instead of parsed JacksonObject (Its a custom POJO object created using Jackson Annotations) and parse the inputstream to do that. However, I am unable to find a reference to do that i.e., parsing a jackson object, out of input stream (based on input media type) and return the respective object.

Can someone direct me to some helpful resources or help me here ?

Upvotes: 0

Views: 336

Answers (1)

ohwhy
ohwhy

Reputation: 49

This is an easy way of getting the contents from a request handled by Resource. Just replace Map.class with your annotated POJO:

@POST
public void handle(String requestBody) throws IOException {
    ObjectMapper om = new ObjectMapper();
    Map result = om.readValue(requestBody, Map.class);
}

With this approach you are free to handle a null value in any way you find suitable.

Upvotes: 1

Related Questions