Lombok Builder mode to create objects

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

Insert picture description here
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()

Insert picture description here
The build() method can return the object we want~
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_38045214/article/details/115300012