0

I'm passing a Java object to a Web service that accepts json, using HttpURLConnection. Employing com.fasterxml.jackson to convert the object to json string and then writing it to the output stream, the service works fine for simple dummy POJOs, but the application breaks when I use a complex object that I'm originally intend to send, with the console filled with exceptions like StackOverflow and endless clutter of

at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690) at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)

Common solutions are like annotating the class with json annotation. Is there any other way to get around it, like using different accept:content-type for web service (other than json or xml as they require annotated objects) or like that?

RESTless
  • 1
  • 1
  • Send an object whose structure matches with the JSON structure expected by the service. If your original object doesn't match this structure, then either it can be annotated to match it, or you can copy the relevant state to an object whose structure does match, i.e. a DTO. – JB Nizet Oct 23 '16 at 16:17

1 Answers1

0

You can use mixin annotations to provide hints for Jackson on how to serialize your data, without polluting the POJO with unnecessary dependencies.

Here's the reference to documentation article, that has good example: http://wiki.fasterxml.com/JacksonMixInAnnotations

Ivan Gammel
  • 652
  • 7
  • 17