brief introduction
because Lombok Convenience of annotation , Now entity classes are all used Lombok annotation .
Which is used in the entity class Lombok Notes mainly include the following :
@Data
Annotations in
class
On ; Providing all properties of the class get and set Method , It also provides equals、canEqual、hashCode、toString Method .
@Setter
Annotations in
attribute
On ; Provide... For a single property set Method ; Annotations in
class
On , Provide... For all properties of this class set Method .
@Getter
Annotations in
attribute
On ; Provide... For a single property get Method ; Annotations in
class
On , Provide... For all properties of this class get Method .
@EqualsAndHashCode
Annotations in
class
On , Can generate equals、canEqual、hashCode Method .
@ToString
This annotation is used for
class
On , Can generate all parameters of toString Method .
@Data Annotations contain the functions of other annotations . In general , Use it directly @Data Realization .
@Builder
Use builder mode to build entity objects , Can only be annotated on classes , A chain construction factory that will generate a current process of the class .
@NoArgsConstructor
Generate a parameterless constructor
@AllArgsConstructor
The construction method that generates all parameters for all attributes in the entity .
If this annotation is applied , Must be used at the same time NoArgsConstructor Generate a parameterless constructor .
Java It is clearly stated in , When there is no constructor defined in a class , By default, the system provides a parameterless construction method for this class , This constructor is called the default constructor . When a class explicitly defines the constructor , Then the system will no longer provide a default constructor for this class by default . Development must manually provide a parameterless constructor for the current class , Because parameterless construction methods are very common .
Problem description
Online problems are caused by Lomkob Improper use of annotations . After the system goes online today , It is found that interface access often times out . The troubleshooting found that an exception occurred when calling the order center interface , This function is to judge whether the user has a corresponding type of order . The corresponding interface request entity classes are as follows :
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class OrderQueryReq {
/**
* user id
*/
private Long userId;
/**
* Number of entries per page
*/
private Integer pageSize = 1;
/**
* Course type
*/
private String courseType;
/**
* The order status
*/
private String status;
/**
* Payment time ends
*/
private String payEndTime;
}
The corresponding entity class defaults to pageSize by 1. But in the process of screening , It is found that there is no pageSize attribute .
PageResp<OrderResp> orderPage = orderCenterAgent.getOrder(OrderQueryReq.builder()
.userId(userId).payEndTime(DateUtil.timeToString(payTime)).courseType(courseType)
.status(orderStatus).build());
Request the Builder The builder builder provided by the annotation .
however class File decompiled Builder There is no pageSize The default value is
So there is no object constructed pageSize Parameters .
Cause analysis
The business function is only to judge whether the user has a corresponding type of order . Because there is no transmission PageSize Parameters , Query all orders of this type for this user . Because some users have large orders , Therefore, there will be a timeout problem in the query process .
resolvent
Lombok In the use of @Builder when , Member variables are not set to default . Can be set by
@Builder.Default
After the note , Use the ID to determine whether the default value is enabled .
On the attribute field that needs to set the default value , Add @Builder.Default annotation
Generated after compilation Class as follows :
In Builder build When , It will determine whether to assign values to the fields with this annotation . without , Then use the initial default value of the field in the entity object .
The interface timeout problem is solved .
原网站版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041429166722.html