#JsonIgnore with #Getter Annotation - java

Can I use #JsonIgnore with #Getter annotation from lombok without explicitly define the getter, because I have to use this JsonIgnore while serializing the object but while deserializing, the JsonIgnore annotation must be ignored so the field in my object must not be null?
#Getter
#Setter
public class User {
private userName;
#JsonIgnore
private password;
}
I know, just by define the JsonIgnore on the getter of password I can prevent my password to be serialized but for that, I have to explicitly define the getter thing that I don't want.
Any idea please, Any help will be appreciated.

To put the #JsonIgnore to the generated getter method, you can use onMethod = #__( #JsonIgnore ). This will generate the getter with the specific annotation. For more details check
http://projectlombok.org/features/GetterSetter.html
#Getter
#Setter
public class User {
private userName;
#Getter(onMethod = #__( #JsonIgnore ))
#Setter
private password;
}

Recently i had the same issue using jackson-annotation 2.9.0 and lombok 1.18.2
This is what worked for me:
#Getter
#Setter
public class User {
#JsonIgnore
#JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;
So basically adding the annotation #JsonProperty(access = JsonProperty.Access.WRITE_ONLY) means that the property may only be written for deserialization (using setter) but will not be read on serialization (using getter)

This could be quite obvious but I lost a lot of time not thinking this solution before:
#Getter
#Setter
public class User {
private userName;
#Setter
private password;
#JsonIgnore
public getPassword() { return password; }
}
As Sebastian said #__( #JsonIgnore ) can resolve this issue but sometimes the use of the onX Lombok feature (#__()) can have side-effects for example breaking the javadoc generation.

I recently had the same issue.
There are several ways to solve it:
Create file lombok.config in the root folder of your project with content:
// says that it's primary config (lombok will not scan other folders then)
config.stopBubbling = true
// forces to copy #JsonIgnore annotation on generated constructors / getters / setters
lombok.copyableAnnotations += com.fasterxml.jackson.annotation.JsonIgnore
...
and in your class you can use this annotation as usual, on the field level:
#JsonIgnore
private String name;
Note: if you use lombok #RequiredArgsConstructor or #AllArgsConstructor, then you should remove all usages of #JsonIgnore with #JsonIgnoreProperties (as described in solution #4, or you may still choose solution #2 or #3). This is required because #JsonIgnore annotation is not applicable for constructor arguments.
Define Getters / Setters manually + add #JsonIgnore annotation on them:
#JsonIgnore
public String getName() { return name; }
#JsonIgnore
public void setName(String name) { this.name = name; }
Use #JsonProperty (it's either read-only or write-only, but no both):
#JsonProperty(access = JsonProperty.Access.READ_ONLY) // will be ignored during serialization
private String name;
#JsonProperty(access = JsonProperty.Access.WRITE_ONLY) // will be ignored during deserialization
private String name;
Use #JsonIgnoreProperties({ "fieldName1", "fieldName2", "..."})
I personally use solution #1 globally and solution #4 for exceptions when class also has annotations #AllArgsConstructor or #RequiredArgsConstructor.

With JDK version 8 use below:
// #Getter(onMethod=#__({#Id, #Column(name="unique-id")})) //JDK7
// #Setter(onParam=#__(#Max(10000))) //JDK7
#Getter(onMethod_={#Id, #Column(name="unique-id")}) //JDK8
#Setter(onParam_=#Max(10000)) //JDK8
Source : https://projectlombok.org/features/experimental/onX

Related

Why #RequestBody works without setters?

I am writing a controller with the annotation #RequestBody in order to map to a Java object. The method that uses the annotation is:
#PostMapping("/users")
public ResponseEntity<Object> createUserForProject(#Valid #RequestBody User user) {
log.info("Creating a user " + user.getEmail());
}
This is the User class:
#Getter
#AllArgsConstructor
#Slf4j
#EqualsAndHashCode
#ToString
public class User {
#NotEmpty
#Email
private String email;
#NotEmpty
private String firstName;
#NotEmpty
private String lastName;
#JsonIgnore
private Optional<LocalDate> lastLogonDate = Optional.empty();
#JsonIgnore
private Optional<LocalDate> lastModificationDate = Optional.empty();
#JsonIgnore
private Optional<LocalDate> creationDate = Optional.empty();
#JsonIgnore
private Optional<LocalDate> mfaWarningDate = Optional.empty();
#JsonIgnore
private Optional<LocalDate> auditStartNotificationDate = Optional.empty();
#JsonIgnore
private boolean enabled = true;
public User() {
log.info("HI");
}
(More code without explicit setters)
So when I make a POST call with the body
{
"email":"test#test.com",
"firstName":"testName",
"lastName":"testLastName"
}
Outputs HI and the log with the Creating a user test#test.com message, so the object is created. My point here is... why does this really work? The HttpMessageConverter is calling the no-args constructor and there are no setters to call after create the object with the constructor. How do the object attributes get their values without any setter? What am I missing here?
Spring boot uses Jackson for Object <-> JSON conversion, and Jackson does not need setters, it sets fields via reflection.
Here is a relevant question about Jackson and why it doesn't need setters
How does jackson set private properties without setters?

How do I create a safe Lombok JPA entity?

I have an #Entity with 20 fields including the index and a timestamp updated by Hibernate:
#Entity
public class MyEntity {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#UpdateTimestamp
private LocalDateTime updatedTime;
private String ....
private String ....
I have a default constructor for Hibernate and a secondary constructor to set everything but the id and updatedTime.
I don't need (or want) setters for id or updatedTime because I only want Hibernate to set them, and it does that with reflection.
I wanted to try out Lombok to see if I could avoid a lot of boilerplate involved here but #Data adds both getters and setters and doesn't create the same constructors.
I'm also concerned that Lomboks generated equals/hashCode and toString methods can cause subtle problems with Hibernate.
This will mean I will have to use a combination of the other Lombok annotations to do this.
How do I safely create an Entity using Lombok like this?
Am I going to have to use a mixture of annotations and manual methods?
Some lombok annotations like #EqualsAndHashCode and #ToString have Exclude option. But neither #Data nor #AllArgsConstructor has a similar option.
But #Data generates setters for all fields for which a setter is not already defined. So you would define a setter as below for the required fields, which does nothing.
private void setId(Long id) {
// Do nothing
}
Instead of the #AllArgsConstructor, you could either use #RequiredArgsConstructor, but annotate all the fields to be in the constructor with #NonNull (or the field should be final).
Refer this answer for RequiredArgsConstructor.
My suggested approach : Another way would be to use #Builder annotation along with #AllArgsConstructor(access = AccessLevel.PRIVATE). (NOTE : Builder by default adds a private all argument constructor, but this is done only if there are no other constructors. But in your case, a default constructor exists and you need to explicitly mention the all args annotation.)
This would prevent the use of the constructor from outside, but at the same time allow you to create objects using the builder. At this point, you could set the values to id and updateTime using the builder. To prevent this you need to add the below code as well.
public static class MyEntityBuilder {
// access is restricted using
// these private dummy methods.
private MyEntityBuilder id(Long id) {
return this;
}
private MyEntityBuilder updateTime(LocalDateTime time) {
return this;
}
}
So, even though it is not possible to achieve your requirement directly, you could do so by adding two dummy setter methods and another two dummy methods within the builder class.
we have #NoArgsConstructor #AllArgsConstructor for generating constructor with lombok.
this is how i create them.
#Entity
#Table(schema = "S25", name = "bank")
#NoArgsConstructor
#AllArgsConstructor
#ToString
#Getter
#Setter
public class Bank {
#Id
#SequenceGenerator(name = "bankEntitySeq", sequenceName = "SEQ_BANKS", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "bankSeq")
#Column(name = "bank_id")
private Long bankId;
#Column(name = "bank_name")
private String bankName;
#Column(name = "created_on")
private Date createdOn =
new Date(); //Date.from(Instant.now().atZone(ZoneId.of("UTC")).toInstant());
}

How do I use a JPARepository inside of a PrePersist Annotation?

I have an alert table which is transactional and an alert type table which is master. I would like to send out an email whenever an alert is added to the table, so I figured I would use PrePersist. However, in my email, I want to include some information that is included in the alert type table.
I have tried to add a reference to the AlertTypeRepository in the Alert class but I can't because my alert class is a #Table and alertTypeRepository is not a column.
Below is my Alert class
#Entity
#Table
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
public class Alert {
#Id
#GeneratedValue
int id;
#Column
String name;
#Column
String alertTypeId;
#Column
String detailedMessage;
#Column
String status;
#Temporal(TemporalType.TIMESTAMP)
Date time;
}
Below is my AlertType class
#Entity
#Table
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
public class AlertType {
#Id
#GeneratedValue
int id;
#Column
String name;
#Column
String header;
#Column
String footer;
#Column
String summary;
#Column
String deliveryType;
#Column
Boolean active ;
#Column
String recipients;
}
I would like to have a PrePersist function inside of the Alert class. That allows me to access its corresponding header and footer from the AlertType class.
I figured out a solution so I hope this helps anyone facing a similar issue. Basically I had to create an EntityListener to the Alert class and then add the following class.
#Component
public class AlertListener {
static AlertTypeRepository alertTypeRepository;
#Autowired
public void init(AlertTypeRepository alertTypeRepository)
{
this.alertTypeRepository = alertTypeRepository;
}
#PrePersist
public void prePersist(Alert alert) {
List<AlertType> alertType= this.alertTypeRepository.findAll();
}
}
As I know the are two approaches to archive the purpose. Your alterType is not managed by Spring .
Define a JPA EntityListener and apply it on your entity class, which does not seem to interest you.
The second approach, annotated your entity with Spring #Configurable annotation:
#Configurable(preConstruction = true)
class AlterType{
#Inject YourRepository bean as normal.
}
To make it work. Firstly you have to add aspectj related jars into your project dependencies. Secondly you can choose load-time weaving or compile-time weaving to handling the injection for you class.
There is an example of aspectj compiler config in Maven can be used for compile-time weaving(note, just for aspectj compiler maven plugin config, I did not use #Configurable here.).

Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object

I am getting below error while using lombok and even it doesn't allow me to set id and version while creating student instance.
Multiple markers at this line
- overrides com.example.demo.IModel.canEqual
- Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object. If this is
intentional, add '#EqualsAndHashCode(callSuper=false)' to your type.
- overrides com.example.demo.IModel.hashCode
- overrides com.example.demo.IModel.toString
- overrides com.example.demo.IModel.equals
IModel
#Data
#Builder
#NoArgsConstructor
#AllArgsConstructor
public class IModel {
private String id;
private String version;
}
Student
#Data
#Builder
#NoArgsConstructor
#AllArgsConstructor
public class Student extends IModel{
private String firstName;
private String lastName;
}
In the main method, it doesn't allow me to set the value of Id and version field
Student s = Student.builder().firstName("Adam").lastName("Kerr").build();
Edit-1
#sfiss - As suggested, now I changed like below, but now I am not able to set firstName and lastName, only cab set id and version
Student.java
#Data
#Builder(builderMethodName = "studentBuilder")
#NoArgsConstructor
#AllArgsConstructor
#EqualsAndHashCode(callSuper = true)
public class Student extends IModel {
#NotEmpty(message = "{email.notempty}")
#Email
private String firstName;
private String lastName;
public Student(final String firstName, final String lastName, final String id, final String version) {
super(id, version);
this.firstName = firstName;
this.lastName = lastName;
}
}
IModel.java
#Builder
#Data
#NoArgsConstructor
#AllArgsConstructor
public class IModel {
private String id;
private String version;
}
There are multiple problems here, all of them relating to using lombok with inheritance:
Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object. If this is
intentional, add '#EqualsAndHashCode(callSuper=false)' to your type.
The warning is given by #Data because that usually generates equals/hashcode without the call to super. Fix it by adding #EqualsAndHashCode(callSuper = true).
The #Builder gives you a compile warning because it will generate two static methods with the same name in both the super- and the subclass. Fix it by defining #Builder(builderMethodName = "studentBuilder") on Student.
You won't be able to set superclass properties on you studentBuilder because your superclass and subclass have a default constructor. Fix it by creating a constructor and moving the #Builder annotation to it (i.e. annotate the constructor with #Builder, not the class):
Code:
#Builder(builderMethodName = "studentBuilder")
public Student(
final String firstName,
final String lastName,
final String id,
final String version) {
super(id, version);
this.firstName = firstName;
this.lastName = lastName;
}
Call your builder with the correct method (IModel.builder() vs Student.studentBuilder()):
Student.studentBuilder().firstName("Name").build();
I also want to add some improvements to the above solution. While I like lombok as a tool (I really don't need to read that much boilerplate), the first solution to preventing boilerplate is to think whether you need all those getters and setters and ask yourself these questions:
Do you want bags of data? It is fine for some use cases, in others you want objects more in the sense of OOP, i.e. don't expose your state but behavior.
Do you really need mutability? If not, prefer #Value.
Do you really need both constructor types (especially the no-args-constructor)? They are part of the problem here. Sometimes you need them for frameworks (proxies, reflection, ...) to work properly.
More specific to your code: You prefixed the superclass with "I" but it is not an interface. If it is meant as an abstract class, declare it abstract and don't give it a #Builder.
You can use #sfiss solution
or
You can use #Getter and #Setter annotations instead of #Data annotation.
The warning from building a Spring Boot project
When I built my Spring boot project, I got 20 warnings about a same thing. The warning shows: Generating equals/hashCode implementation but without a call to superclass
Description of the warning
This warning is from lombook, it happens when we inherit a child class from parent class by using #Data #ToString #EqualsAndHashCode, IDE will trigger the warning: Generating equals/hashCode implementation but without a call to superclass .
Solution
There are two solutions:
add annotation #EqualsAndHashCode(callSuper = true) on the class
create lombook config file in the project root path: src/main/java. Note: this solution requires the version of lombook > 1.14.
I recommend the solution 2, since you will not need to add the annotation to all the required classes.
To impletement the solution, you need to create lombok.config in the path of src/main/java. If you have more than one packages, you may need to create multiple config files.
The content of the config file includes:
config.stopBubbling=true
lombok.equalsAndHashCode.callSuper=call
When we rebuild our project, you will not get these warnings anymore.
Cheers!
I had the same problem and i resolved it in this way hope it helps you.
That help you also with abstract classes
Student.java
#Data
#NoArgsConstructor
#AllArgsConstructor
#EqualsAndHashCode(callSuper = true)
public class Student extends IModel {
#NotEmpty(message = "{email.notempty}")
#Email
private String firstName;
private String lastName;
#Builder
public Student(final String firstName, final String lastName, final String id, final String version) {
super(id, version);
this.firstName = firstName;
this.lastName = lastName;
}
}
IModel.java
#Data
#NoArgsConstructor
#AllArgsConstructor
public class IModel {
private String id;
private String version;
}
The error says If this is
intentional, add '#EqualsAndHashCode(callSuper=false)' to your type.
So, add #EqualsAndHashCode(callSuper = false) to solve the problem.

Thymeleaf not recognizing Lombok getters and setters

Lombok plugin is installed. Using IntelliJ 15. Structure of model shows the getters and setters but I get the following error from Thymeleaf.
Error:
Invalid property 'postTitle' of bean class [com.blog.domain.Post]: Bean property 'postTitle' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
The model:
#Entity
public #Data class Post {
#Id
#GeneratedValue
private Long id;
#NotEmpty
private String postTitle;
#NotNull
#ManyToOne
private Author postAuthor;
#NotNull
#Temporal(TemporalType.DATE)
private Date postDate;
#Column(columnDefinition = "TEXT")
private String postTeaser;
#Column(columnDefinition = "TEXT")
private String postBody;
}
The controller method that loads the form:
#GetMapping("/create")
public String postForm(Post post, Model model) {
model.addAttribute("authors", authorService.getAllAuthors());
return "postform";
}
The field where error occurs:
<input id="postTitle" type="text" th:field="*{postTitle}" />
Any ideas where I'm going wrong? Adding the getters and setters manually solves the problem. What is lombok doing here that is breaking it?
I ran into this using a boolean named "isEnabled". Renaming it to "enabled" worked. Not sure about the reason for this though.

Categories