What is the difference between #JsonIgnore and #JsonIgnoreProperties - spring

I need to understand the main difference between this two annotations: #JsonIgnore and #JsonIgnoreProperties in Spring Boot.
Thank you in advance.

#JsonIgnore is to ignore fields used at field level. while,
#JsonIgnoreProperties is used at class level, used for ignoring a list of fields.
Refer here for more info

Related

Accessing Transient field of Entity through Service

Currently, I am learning Spring Boot. I got trouble with accessing the #Transient variable of Entity class from my #Service class.
Actually, I want to calculate the popularity of the book in my Service and want to assign it to a specific book.
#Transient
private Double popularity;
Any tips on solving this problem.
Let me know if you need more information.
Thank you in advance!
Transient properties are properties that do not have an appropriate column in the database table. But of course, they should have getters and setters, as any other property.
So after you calculate the popularity, just simply use the setter (setPopularity) as for any other class property.

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.

Spring boot deserialization for two models

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 ?

Spring Data MongoDB: Specifying a hint on a Spring Data Repository find method

I am implementing a Spring Data Repository and having my repository extend the MongoRepository. I am looking for a way to specify a hint on my findBy methods so I can be control. I have seen several times when a non-optimal index would be picked as the winning plan.
This is what my repository looks like right now:
public interface AccountRepository extends MongoRepository<Account, ObjectId> {
#Meta(maxExcecutionTime = 60000L, comment = "Comment" )
public List<Account> findByUserIdAndBrandId(Long userId, Long brandId);
}
I researched a bunch and found that the JPARepository from spring data supports the #QueryHint annotation but I do not believe that annotation is supported for MongoDb. Is there a similar annotation I can specify on top of my findBy method to specify the hint?
MongoTemplate allows to specify a hint, however, I have a ton of findBy methods and I would hate to add an implementation underneath just to specify a hint.

Spring Rest, JsonManagedReferences and cyclic references

I'm facing an issue where using #JsonManagedReferences and #JsonBackReference won't break the infinite recursion loop when marshaling the Objects to Json.
I know that I could try to avoid bidirectional relationships but this would not serve very well. Another not desirable solution would be to drop the #RestResource(exported=false) annotation and follow the link provided just once.
One example would be:
#JsonManagedReference
#ManyToOne(cascade=CascadeType.ALL)
#RestResource(exported=false)
#JoinColumn(name="organisation_id")
private Organisation organisation;
with it's counterpart in another class:
#JsonBackReference
#OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
#JoinColumn(name ="organisation_id")
#RestResource(exported=false)
#Fetch(value = FetchMode.SUBSELECT)
private Set<OrganisationUnit> organisationUnits;
Bot classes have a #RepositoryRestResource with nothing special in it.
#RepositoryRestResource
public interface OrganisationRepository extends JpaRepository<Organisation, Long> {
public final static String ID_QUERY = "SELECT u FROM Organisation u where u.id=:objectId";
#Query(ID_QUERY)
public Organisation findObjectById(#Param("objectId") Long objectId);}
I know that Jackson 2 should be able to handle to kinds of situations but in this case it does not resolve the issue. Is this known behavior that I'm not aware of?
Please let me know of any obvious flaws as I'm not very experienced using JPA, Hibernate or Spring.
The error message which is provided is:
There was an unexpected error (type=Internal Server Error, status=500). Could not write content: Infinite recursion (StackOverflowError) (through reference chain: org.springframework.hateoas.PagedResources["_embedded"]);
nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion
I'd be happy about any pointers.
Which Jackson version are you using? Jackson 2.2.3 does not have this issue. Its taken care automatically

Resources