Role base Json output in Spring Boot - json

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.

Related

Control JSON data with Spring Security

I have a class named Order shown below.
class Order{
private int id;
private String name;
private String amount;
//getters and setters
}
Using Spring security, I need to be able to control the data that is being returned as a response from Spring Controller. For say, Admin can view all the data of Order, but customer can see only name and amount. How can I filter the JSON data with Spring Security.So, final output for admin should be
[{id:1,name:order1,amount:100}, {id:2,name:order2,amount:200}]
and output for customer should be
[{name:order1,amount:100}, {name:order2,amount:200}].
Is there any way to do this
you could hack it a bit with Spring Data and Spring Security:
public interface FooRepository extends CrudRepository<Foo, Long> {
#Query(
value = "select id, amount, case when ?#{hasRole('admin')} then name else null end as name from foo where id=?1",
nativeQuery = true
)
Foo findOne(Long id);
}
You'd need to add an EvaluationContextExtensionSupport bean. This lets you use Spring Security expressions in Spring Data queries:
#Component
public class MyEvaluationContextExtensionSupport extends EvaluationContextExtensionSupport{
#Override
public String getExtensionId() {
return "security";
}
#Override
public SecurityExpressionRoot getRootObject() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return new SecurityExpressionRoot(authentication) {};
}
}
Or you could try Projections with Spring Data REST
//untested:
#Projection(name = "detailed", types = Foo.class)
public interface FooDetailProjection {
#Value("?#{ hasRole('admin')? target.name: null}")
public String getName();
}
Or consider using Column Security directly in your database.

How to solve this infinite recursion (StackoverflowError) when I use writeValueAsString() method of Jackson ObjectMapper() class?

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;

Mapping JSON to POJO not working

I have a spring MVC project,in the controller i have the following method :
#RequestMapping(value = IdentityServiceURIConstants.CREATE_NEW_USER, method = RequestMethod.POST)
public #ResponseBody User createUser(#RequestBody User user) {----}
this method receives a JSON object which is supposed to represent the user object.
My problem is that the fields in the user object are not identical to those received in the JSON object.
Example: first name is First_Name in JSON and firstName in object, hence, the mapping is not working.
Do u have any idea on how to solve this problem, given that i cant edit neither the user object nor the JSONobject
You can use #JsonProperty to name your java class property to the json key name like below:
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
#JsonProperty("FIRST_NAME")
private String firstName;
#JsonProperty("LAST_NAME")
private String lastName;
// getters & Setters methods
}
and your json will be something like this:
{
"FIRST_NAME": "first name",
"SECOND_NAME": "second name"
}
You can write a DTO class.
class UserDTO{
private User user;
// use getter setter to extract data from object
}

Process JSON with Jersey and Jackson

I am using jersey in Java. I want to get JSON data sent via a post request. However, I am not sure how to do this, despite my searching. I am able to receive JSON data at a path, yet I can't figure out how to parse it into java variables. I assume that I need to use jackson to do this. However, I don't understand how to pass the received JSON to jackson.
#Path("/register")
public class ResourceRegister
{
#POST
#Consumes(MediaType.APPLICATION_JSON)
public String RegisterUser(//not sure what to take in here to get the json )
{
//code to deal with the json
}
There are several ways of accepting the JSON and using it in back-end.
1. set POJO elements using JAXB APIs and use object of that POJO class to access passed parameters. this will be helpful while JSON size is large.
Example:
your service declaration would be as following
#Path("/register")
public class ResourceRegister
{
#POST
#Consumes(MediaType.APPLICATION_JSON)
public String RegisterUser(RegParams regParams)
{
//code to deal with the json
}
.....
}
and you will write a POJO like following
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
#JsonIgnoreProperties(ignoreUnknown=true)
#JsonWriteNullProperties(false)
public class RegParams implements Serializable {
#JsonProperty("userId")
private long userId;
#JsonProperty("userName")
private String userName;
..
..
}
retrive JSON as a string and use jersey APIs to work with the same.
in this case you can declare your service as following
#Path("/register")
public class ResourceRegister
{
#POST
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String RegisterUser(#FormParam("jsonObj")String jsonString)
{
//code to deal with the json
}
.....
}
and you can process that string by using jersey APIs like following
ObjectMapper om = new ObjectMapper();
JsonNode mainNode = om.readTree(jsonString);
//access fields
mainNode.get..(as per data passed, string, int etc)
for more referance you can refer this or this
You just need to place #JsonProperty annotation to your class properties and add that class to your Resource method as paramater.
You might need #JsonIgnoreProperties annotation as well if you are not deserializing everything inside the incoming json
See below:
#POST
#Consumes(MediaType.APPLICATION_JSON)
public String registerUser(MyUser myUser)
{
//code to deal with the json
}
public class MyUser{
#JsonProperty
private String name;
#JsonProperty
private String surname;
//getters & setters & constructors if you need
}

How to create type id using Custom Serilizer?

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 {
....
}