dimanche 1 mars 2015

Jersey Jackson POJO serialization error when no contructor is available


Vote count:

0




I have a plain old Java class (POJO) that I want to serialize by Jersey Jackson:



public class MyClass {
private String name;
private Calendar time;
private ServiceType serviceType;

//getter and setter
......
}


My Jersey resource class will consume this class as JSON:



@POST
@Consumes("application/json")
public void foo(MyClass data) {
//...
}


But the problem is: When I sent a request containing the JSON data to the server, it returns error. Later I found that I need to add a non-arg constructor for all objects that I want to serialize by Jersey Jackson. That is, I need to add an empty non-arg constructor for the class ServiceType:



public ServiceType () {
}


This seems to be a new requirement for Jersey Jackson since I've used Jersey before and there wasn't such a requirement to add non-empty constructor. This may come from JAXB requirement.


This poses a problem that in case MyClass contains some fields that are from some third party library that don't have a non-arg constructor, then it will break. For example,



public class MyClass {
private String name;
private Calendar time;
private ThirdPartyBean other1;
private JDKBean other2

//getter and setter
......
}


In the above case, neither ThirdPartyBean nor JDKBean classes has no-arg constructor. Furthermore, nested class fields inside ThirdPartyBean do not have no-arg constructor either. So in this case, Jersey Jackson reported serialization/deserialization error due to lack of no-arg constructors.


I never encountered such a problem before when I used Jersey Jackson. But it seems to be a problem for Jersey Jackson 2.16. How can I configure the Jersey Jackson to use POJO mapping directly without using JAXB binding?



asked 1 min ago

tonga

2,330






Jersey Jackson POJO serialization error when no contructor is available

Aucun commentaire:

Enregistrer un commentaire