Reputation: 41759
I've got a Jax-rs endpoint that accepts JSON analogous to:
{
"a": 1,
"b": "some value",
"c": { <-some-arbitary-json-object-> }
}
In my DTO, a and b are no issue. What do I do with c? I need only to serialize it again (or, indeed, just read it as a String), I don't need to process it in any way. I do need to do things with a and b, so I can't just treat the entire body as a String.
What data type do I need to give it so that jax-rs/jersey can deserialize it?
I can't help but feel I'm missing something obvious.
Upvotes: 0
Views: 37
Reputation: 41759
I worked out one way, but I feel I'm probably re-inventing the wheel. I defined a custom deserializer to read in the arbitary JSON then serialise it again:
public class JsonAsStringDeserializer extends JsonDeserializer<String> {
private final ObjectMapper mapper = new ObjectMapper();
@Override
public String deserialize(JsonParser p, DeserializationContext ctx)
throws IOException {
TreeNode node = mapper.readTree(p);
return mapper.writeValueAsString(node);
}
}
And in the model POJO:
@JsonDeserialize(using = JsonAsStringDeserializer.class)
private String c = null;
Upvotes: 0