With the following classes, which make up the JSON message structure I transfer to the client side after a JAX-RS call (CXF), I receive
org.apache.cxf.jaxrs.client.ClientWebApplicationException: .Problem with reading the response message, class : class bg.vivacom.sel.dto.SELResponse, ContentType : application/json.
...
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not construct instance of bg.vivacom.sel.dto.SELDTO, problem: abstract types can only be instantiated with additional type information
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream#16edbe39; line: 1, column: 157] (through reference chain: bg.vivacom.sel.dto.SELResponse["dto"])
The returned object contains property dto which is an interface
#XmlRootElement(name = "SELResponse")
public class SELResponse implements Serializable{
#XmlElement(name = "corelationID")
private String corelationID;
#XmlElement(name = "responseTimeStamp")
private String responseTimeStamp;
#XmlElement(name = "respStatusCode")
private String respStatusCode;
#XmlElement(name = "respStatusMessage")
private String respStatusMessage;
#XmlElement(name = "processStatus")
private ProcessStatus processStatus;
#XmlElement(name = "dto")
private SELDTO dto;
The interface
public interface SELDTO extends Serializable {
is a way to include a number of different DTOs in my answer depending on the request, such as
#XmlRootElement(name = "customerProfile")
public class CustomerProfileDTO implements SELDTO {
#XmlElement(name = "customerCode")
private String customerCode;
...
and
#XmlRootElement(name = "sms")
public class SmsDTO implements SELDTO {
#XmlElement(name = "From")
private String from;
...
Any ideas how else I have to annotate the classes so that the response can be correctly set to the specific object type. I understand that it requires additional information as at the time it re-creates the dto object it doesn't know its type so I have tried to annotate the interface as follows:
#XmlSeeAlso({CustomerProfileDTO.class, SmsDTO .class})
public interface SELDTO extends Serializable {
but I still get the issue. Any thoughts would be appreciated it.
I would recommend using Jackson annotation '#JsonTypeInfo' for this case.
But if you must use JAXB annotations, use #XmlElements or #XmlElementRefs similar to how you'd use them with JAXB/XML:
#XmlElements({
#XmlElement(type=SubClass1.class, name="sub1"),
#XmlElement(type=SubClass2.class, name="sub2")
})
note that you must include specific mapping to possible subtypes here.
Related
Is is possible to exclude JsonProperties in the output of a Spring Boot Rest call based on a defined condition? (eg. the role of the user)
Example:
public class Employee{
#JsonProperty
private String name;
#JsonProperty
private String fieldForManagerOnly;
#JsonProperty
private String fieldForEmployeeOnly;
}
I want to have the fieldForManagerOnly only serialized in the JSON output when the user has the ROLE manager.
I've already tried the solution with the #JsonView (as described in Latest Jackson integration improvements in Spring) but that solution is very limited as the #JsonView is bound to one controler method and I want to have only one controller method.
I've solved the problem myself. I used the JsonView solution but instead of an annotation I select the JsonView from code.
First you need an interface for the Views.
public class JsonViews {
public interface EmployeeView {}
public interface ManagerView {}
}
Mark the fields in the Model class with the #JsonView annotations.
public class Employee{
#JsonProperty
private String name;
#JsonView(JsonViews.ManagerView.class)
private String fieldForManagerOnly;
#JsonView(JsonViews.EmployeeView.class)
private String fieldForEmployeeOnly;
}
In your controller set the JsonView to use based on the role (or some other condition):
#RequestMapping(value = "/{employeeId}", method = RequestMethod.GET)
public ResponseEntity<MappingJacksonValue> getEmployee(#PathVariable long employeeId) {
Employee employee = employeeService.getEmployee(employeeId);
MappingJacksonValue jacksonValue = new MappingJacksonValue(employeeResourceAssembler.toResource(employee));
if (getRole().equals("MANAGER")) {
jacksonValue.setSerializationView(JsonViews.ManagerView.class);
} else if (getRole().equals("EMPLOYEE")) {
jacksonValue.setSerializationView(JsonViews.EmployeeView.class);
}
return new ResponseEntity<>(jacksonValue, HttpStatus.OK);
}
Annotate the field with
#JsonInclude(JsonInclude.Include.NON_NULL)
and make sure to set the field fieldForManagerOnly to null if the current user is not a manager.
I am absolutly new in JSON and how Java handle convert into this format.
So I have the following problem into a Spring MVC application I have this controller method that correctly handle AJAX request toward the /provinceDiUnaRegione.json resource:
#RequestMapping(value = "/provinceDiUnaRegione.json")
#ResponseBody
public String getProvince(String codiceRegione) {
System.out.println("INTO getProvince()");
List<Twb1013Provincia> provinceDiUnaRegioneList = geograficaService.getListaProvinceDiUnaRegione(codiceRegione);
try {
String listaProvince = new ObjectMapper().writeValueAsString(provinceDiUnaRegioneList);
return listaProvince;
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
As you can see this method retrieve a list of Twb1013Provincia objects and then use this line to convert the retrieved list into a JSON String (correct me if it is wrong)
String listaProvince = new ObjectMapper().writeValueAsString(provinceDiUnaRegioneList);
The problem is that when this line is performed I obtain this exception:
com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafiche.Twb1012Regione["tpg1029Provnuoists"]->org.hibernate.collection.internal.PersistentBag[0]->it.myCompany.myProject.anagrafiche.Tpg1029Provnuoist["twb1012Regione"]->it.myCompany.myProject.anagrafic...
So doing some analysis it seems to me that the problem is that I am trying to convert a list of Twb1013Provincia objects that are something like this:
#Entity
#Table(name="anagrafiche.TWB1013_PROVINCIA")
#NamedQuery(name="Twb1013Provincia.findAll", query="SELECT t FROM Twb1013Provincia t")
public class Twb1013Provincia implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column(name="COD_PRV")
private String codPrv;
//bi-directional many-to-one association to Twb1012Regione
#ManyToOne
#JoinColumn(name="COD_REG")
private Twb1012Regione twb1012Regione;
.................................................
.................................................
OTHER FIELDS
.................................................
.................................................
}
The problems seems that, as you can see, this class contain a Twb1012Regione twb1012Regione; field and that this Twb1012Regione class contain in turn a reference to the original Twb1013Provincia (the object to convert), something like this:
#Entity
#Table(name="anagrafiche.TWB1012_REGIONE")
#NamedQuery(name="Twb1012Regione.findAll", query="SELECT t FROM Twb1012Regione t")
public class Twb1012Regione implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column(name="COD_REG")
private String codReg;
//bi-directional many-to-one association to Twb1013Provincia
#OneToMany(mappedBy="twb1012Regione")
private List<Twb1013Provincia> twb1013Provincias;
.................................................
.................................................
OTHER FIELDS
.................................................
.................................................
}
So the problems seems to be that when I try to convert a Twb1013Provincia object it find inside it a Twb1012Regione field that itself contain an Twb1013Provincia...so enter in an infinite loop and the conversion is impossible.
So how can I solve this issue? Exist a way to exclude the Twb1012Regione field from the Twb1013Provincia object conversion?
Or better can I specify the list of the Twb1013Provincia class fields that have to be converted in JSON format? (I need to convert only 2 fields of this class)
Tnx
You can exclude a field from serializing using #JsonIgnore annotation.
Try:
#JsonIgnore
private Twb1012Regione twb1012Regione;
I'm trying to have some inheritance in using bindy csv Dataformat (Camel 2.13.1)
I have an abstract class and two concrete classe, each one inherit the base class.
ex
#CsvRecord(separator=";",generateHeaderColumns=true,skipFirstLine=true)
public abstract class AbstractBaseFormat {
#DataField(columnName="FIELD1",pos=1)
protected String field1;
#DataField(columnName="FIELD2",pos=2)
protected String field2;
// getter/setter
}
public class Format1 extends AbstractBaseFormat {
#DataField(columnName="FIELD3",pos=3)
private String field3;
// getter / setter
}
public class Format2 extends AbstractBaseFormat {
#DataField(columnName="FIELD3",pos=3)
private Long field3;
// getter / setter
}
When i use concrete classe in my route , camel throw an error
java.lang.IllegalArgumentException: The separator has not been defined in the annotation #CsvRecord or not instantiated during initModel. must be specified
Ok, the #CsvRecord annotation is on the abstract class and is not recognized by the Bindy Factory.
But if i put this annotation on each concrete class , i get another exception as Bindy didn't find the first field (pos = 1).
Can i use this kind of model with Bindy csv Dataformat ?
I'm making a DB replication mechanism. I just want to send over an object once over the network. I have implemented a custom serializer that makes a reference to the object using an UUID instead of serializing the object itself. On the other side I plan to just look up the UUID in DB in order to create the none serialized object.
The problem I have is that Jackson expects a type id from my custom serializer. How do I create a type id?
I got the follwing class:
#JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="objectType")
public class Brand implements Serializable {
private String uuid;
#JsonSerialize(using = EntitySerializer.class)
private Producer producer = null;
// Omitting getters and setters
}
and
#JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="objectType")
public class Producer extends Entity implements Serializable {
private String uuid;
private String name;
//Omitting getters and setters.
}
To serialize Producer i got the following serializer:
public class EntitySerializer extends JsonSerializer<Entity> {
#Override
public void serialize(Entity value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
jgen.writeString(value.getUUID());
}
}
When I serialize I get the following error because I don't generate any #JsonTypeInfo type Id.
com.fasterxml.jackson.databind.JsonMappingException: Type id handling not implemented for type com.fodolist.model.Producer (through reference chain: com.fodolist.server.callprocessor.SyncContainter["one"]->com.fodolist.model.Brand["producer"])
Is there any better way to solve the problem?
Maybe have a look at #JsonIdentityInfo; one of id generators creates UUIDs to use as ids. By default the first instance will be serialized in its entirety (otherwise it is not possible to deserialize the data), but you can change that by using #JsonIdentityReference to indicate that all instances are to be serialized using just the id (generated, or accessed via property).
In case the uuid field is already set, you can use the PropertyGenerator as follow:
#JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid")
public class Producer extends Entity {
....
}
I'm facing problem with Jackson's ObjectMapper using JAXB annotations. To be concrete, I'm having collection with interface generic information and although I can deserialize input from XML, it is not possible with Jackson (using JAXB introspector). Maybe I'm just missing some configuration property or JAXB annotation? The problem is that "abstract types can only be instantiated with additional type information", I thought #XmlElementRef (or #XmlElement) with type information will handle this problem, but obviosly it does not.
Please note, that I want to stay only with JAXB annotations if possible.
E.g. using #JsonTypeInfo or #JsonDeserialize would be the last thing to do.
IEntry.java:
#XmlSeeAlso(Entry.class)
public interface IEntry {
String getValue();
}
Entry.java:
#XmlRootElement(name = "entry")
public class Entry implements IEntry {
#XmlElement(name = "value")
String value;
public Entry() {
}
public Entry(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
Aggregator.java:
#XmlRootElement(name = "aggregator")
public class Aggregator {
#XmlElementRef(type = Entry.class)
private Set<IEntry> entries;
public Aggregator() {
}
public Aggregator(Set<IEntry> entries) {
this.entries = entries;
}
public Set<IEntry> getEntries() {
return entries;
}
}
Test method:
#Test
public void testSerialization() throws Exception {
ObjectMapper om = new ObjectMapper();
AnnotationIntrospector intr = new JaxbAnnotationIntrospector();
om.getDeserializationConfig().withAnnotationIntrospector(intr);
String json = "{\"entries\":[{\"value\":\"X\"},{\"value\":\"Y\"},{\"value\":\"Z\"}]}\";\n}";
Aggregator agr = om.readValue(json, Aggregator.class);
}
Thanks for all response
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
I am not sure if Jackson supports this use case or not, but you appear to be using #XmlElementRef incorrectly. When you use #XmlElementRef the root element name associated with the class is used to determine the instance to be instantiated. If your example the node entries does not match the #XmlRootElement(name="entry") annotation.
You could try one of the following options (they all work with MOXy's JSON binding, see: http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html):
OPTION 1 - Change #XMLRootElement on Entry
#XmlRootElement(name = "entries")
public class Entry implements IEntry {
#XmlElement(name = "value")
String value;
public Entry() {
}
public Entry(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
OPTION #2 - Change the JSON Document
{"entry":[{"value":"X"},{"value":"Y"},{"value":"Z"}]}}
OPTION #3 - Use #XMLElement instead of #XMLElementRef
If you use the #XmlElement annotation you can specify on the field/property what the node name should be instead of relying on the #XmlRootElement annotation. Also if you annotate the fields you should specify #XmlAccessorType(XmlAccessType.FIELD) at the type level.
import java.util.Set;
import javax.xml.bind.annotation.*;
#XmlRootElement(name = "aggregator")
#XmlAccessorType(XmlAccessType.FIELD)
public class Aggregator {
#XmlElement(type = Entry.class)
private Set<IEntry> entries;
public Aggregator() {
}
public Aggregator(Set<IEntry> entries) {
this.entries = entries;
}
public Set<IEntry> getEntries() {
return entries;
}
}
For More Information
http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html
http://blog.bdoughan.com/2011/05/jaxb-and-interface-fronted-models.html
import org.codehaus.jackson.map.annotate.JsonDeserialize;
#JsonDeserialize(as = Entry.class)
public interface IEntry {
String getValue();
}