Lombok Builder mode to create objects
1. Traditionally created objects
ResponseResultVO vo = new ResponseResultVO();
vo.setCode(1);
vo.setData("");
vo.setMsg("");
2. Builder mode
Introduce dependencies
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
1. Add @Builder annotation to the entity
@Data
@Builder
public class ResponseResultVO {
private long code;
private String msg;
private Object data;
}
2. Use
After adding the @Builder annotation, I found that there is no way to new, it should be Lombok who helped us privatize the constructor.
Create objects through Buider
ResponseResultVO.builder().code(ErrorCodeConstant.VALID_ERROR).msg(e.getMessage()).build()
The build() method can return the object we want~