Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson 3rd Party Class With No Default Constructor

Tags:

java

json

jackson

I'm trying to use Jackson to read/write my POJOs to/from Json. As of right now, I've got it configured and working for my classes, except for a 3rd party class. When trying to read in the Json I get the error:

org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type 

After a few quick google searches, it appears that my class needs either a default constructor or to override the default constructor with annotations. Unfortunately, the class in which this is failing is from a 3rd party library and that class does not have a default constructor and I obviously cannot over-write the code.

So my question is, is there anything I can do about this or am I just out of luck?

Thanks.

like image 813
Dan W Avatar asked Aug 07 '12 01:08

Dan W


People also ask

Does Jackson require default constructor?

Jackson uses default (no argument) constructor to create object and then sets value using setters. so you only need @NoArgsConstructor and @Setter.

Does Jackson require no args constructor?

Jackson won't use a constructor with arguments by default, you'd need to tell it to do so with the @JsonCreator annotation. By default it tries to use the no-args constructor which isn't present in your class.

Do all classes need a default constructor?

What is the default constructor? Java doesn't require a constructor when we create a class. However, it's important to know what happens under the hood when no constructors are explicitly defined. The compiler automatically provides a public no-argument constructor for any class without constructors.

Does every class has a default no parameter constructor?

The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass.

How do I ignore a property in Jackson?

There are two commonly used Jackson annotations to ignore properties, which are @JsonIgnore and @JsonIgnoreProperties. The former is directly applied to type members, telling Jackson to ignore the corresponding property when serializing or deserializing.

How does Jackson recreates data objects?

By default, Jackson recreates data objects by using no-arg constructors. This is inconvenient in some cases, such as when a class has non-default constructors and users have to write no-arg ones just to satisfy Jackson's requirements.

What is public all arguments constructor in Java?

Public Constructor Let's consider the Employee class structure. It has two required fields: id and name, thus we define a public all-arguments constructor that has a set of arguments that matches the set of object’s fields: This way, we’ll have all the object’s fields initialized at the moment of creation.

How to add a mixin to an object in Jackson 2?

For Jackson 2, you can add the mix-in directly to the objectMapper, objectMapper.addMixInAnnotations (target, mixinSource) ( source) He is right, I used the mixin approach to deserialized a field of the type android.location.Location, which by the way does not have a default constructor (and I have not control over the class/3rd party).


1 Answers

You could make use of Jackson's Mix-Ins feature, coupled with the Creator feature. The Mix-Ins feature alleviates the need to annotate the original third-party code, and the Creator feature provides a mechanism for custom instance creation.

For yet more customization, it's not too involved to write a custom deserializer.

like image 92
Programmer Bruce Avatar answered Oct 02 '22 11:10

Programmer Bruce