Spring boot deserialization for two models - spring-boot

I want to deserialize json into two models which are not related, after some research about custom deserialization in JACKSON I cannot see how I can do it?
I know I can solve the issue by creating a wrapper model for the two models but isnt there a way to deserialize on the fly without JACKSON?

I can use ObjectMapper twice for the same json input. you may need some annotation like
#JsonIgnoreProperties(ignoreUnknown = true)
and/or
#JsonInclude(Include.NON_NULL)
on the producer side can help too.
if you give us your two models ?

Related

How to change Jackson to detect all fields in a POJO, other than only public ones?

When using Spring Boot for a project, Jackson came as default to serialize objects back and from Jsons. I realize that Jackson fails if you don't have public accessors, e.g., getters/setters, or public fields in your POJO.
The behavior is different when I switch to Gson. It detects all fields regardless of their visibility. For this reason, I ended up using Gson.
I felt a little uncomfortable about switching my POJO access rules; It would force some refactoring in the project structure.
So, no problems using Gson, but is there a way of change Jackson's behavior?
Jackson does support reading values from private member fields, but does not do it by default.
You can configure the behavior globally in the Spring Boot config like
jackson:
visibility.field: any
visibility.getter: none
visibility.setter: none
visibility.is-getter: none
(this config will only look for member fields and no longer check get, set and is methods)
You could also use the #JsonAutoDetect annotation to do the same setting for a specific class.
Try to set visibility at ObjectMapper level,
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

Bit confused with #JsonAutoDetect, ObjectMapper()

With reference to my previous linked in question I'm bit confused with the usability of #JsonAutoDetect.
I solved the problem by adding #Getter to FieldValues class and removed the #JsonAutoDetect.
So now it let me thinking, what would be the scenario where #JsonAutoDetect can be used, as I can achieve the same result without having it. What is the purpose of having #JsonAutoDetact annotation over having getter methods. Am I missing something.
Not able to write any comment for previous question so created a new one.
Here is an article that I think can help you. The url is https://www.baeldung.com/jackson-jsonmappingexception .
At my point, if you use jackson-databind jar, spring underlying use ObjectMapper to serialize JavaBean. If neither javaBean's field nor getter method is public, spring could not serialize JavaBean automaticlly. Annotation #JsonAutoDetect is used to custom your javaBean, by which way you can set field limits to any level (e.g. protected public private... so that you can serialize the javaBean successfully).
If I don't understand wrong, the #Getter is from lombok that automaticlly help you generate public getter method.

jackson - root element read Tree vs pojo

Hi I want to parse a json that i retrieve by hitting an legacy system, and build a response json. We are using Spring Boot having a jackson dependency. The problem i have is almost 75% of fields from legacy can be mapped directly or on basis of simple rules (0: false, 1:true). But, there are some complex rules as well like based on certain conditions and data present in some fields, we can map them to a nested object etc. To cater to this requirement which approach should we consider -
POJO approach to fetch the data from legacy target. Use bean util. copyproperties to populate the response bean (75% of properties), and then apply the business transformations on this POJO to tranform based on business logic. (Would we need two pojos here a. to copy from beanutil.copyproperties and then b. create final response dto ??)
Do not use pojo directly parse the JSON apply the transformations and then create a new POJO or response DTO. (But, this may not be generic solution and would need to be done on case by case basis).
Main considerations are approach should be fast, and generic to be applied like a framework. Thanks aakash
The considerations should be like below:
- Are the POJOs reusable?
- Is the JSON multilevel and very large?
If the answer is yes for both, then better to choose POJOs for cleaner implementation. Otherwise JsonObject parsing.
Hope this will help.

Implementing cross-validation in java

I use Spring Roo + jpa + hibernate and I would like to implement cross-validation (validation of several fields at the same time) in my application.
I am not sure how to go about implementing it. Can anyone please advise me and/or direct me to relevant documentation?
Have a look at Hibernate Validator, which allows entity validation (using annotations).
http://www.hibernate.org/subprojects/validator.html
In short, you annotate your field constraints by placing hibernate validator/ JPA annotations above them. (E.g. #Min(10)) and use the following piece of code to find any invalid fields;
ValidatorFactory factory = Validation.byDefaultProvider().configure().traversableResolver(new CustomTraversableResolver() ).buildValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<BaseValidationObject>> constraintViolations = Validator.validate(myEntityToValidate);
If you need to validate specific relationships between entities, you can write custom validators to fit that need.

Jersey JSONConfiguration FEATURE_POJO_MAPPING - how to skip unwanted entries during deserialization

I'm defining pojos for facebook objects, which can be consumed by clients who dont have the capacity to parse JSON. Some of the FB object's data structure is loosely defined, like work
"work":[
{"employer":{"id":"xxxxxxxxx","name":"ABC"},
"location":{"id":"xxxxxxxxx","name":"Philadelphia, Pennsylvania"}
"position":{"id":"198376496853401","name":"Manager"}
"with":[{"id":"xxxxxxxxxxxx","name":"Dogbert Smith"}]}
]
My question is how to skip these objects while deserializing. I'm using
ClientConfig config = new DefaultClientConfig();
config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Is there a way I can customize what to deserialize?
Thanks for any pointers.
I found the solution. Added #JsonIgnoreProperties to the pojo for which I need to ignore some properties. Works great.

Resources