当前位置: 首页> 文旅> 文化 > 慎用lombok.Builder

慎用lombok.Builder

时间:2025/7/7 23:17:54来源:https://blog.csdn.net/tiantang_1986/article/details/139358033 浏览次数:0次

lombok是一个Java库,使用注解方式来简化 Java 代码,可以减少诸如getter、setter的方法。
它常用的注解有:

  • @Getter、@Setter、@ToString 不必多说,分别实现getter、setter、toString、hashCode等方法。
  • @Data 则是包含上面的那些,有这个可以不需要写上面的注解
  • @NoArgsConstructor 生成无参构造函数
  • @AllArgsConstructor 生成有参构造函数
  • @Builder 、@Accessors 是定义链式调用设置属性值,其中@Accessors需要配置属性(chain = true)才能使用链式调用

在使用lombok.Builder时遇到一个问题,代码如下
请求实体

import lombok.Builder;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;@Data
@Builder
public class SettingRequest {private Long id;@NotNull(message = "请选择店铺")private Integer storeId;... .../*** 配置项,json格式*/@NotBlank(message = "请填写配置项")private String configOptions;
}

controller

@PostMapping("/saveSetting")
public Result<Boolean> saveSetting(@RequestBody @Validated SettingRequest request) {Boolean res = settingService.saveSetting(request);return Result.success(res);
}

前端代码

let obj = {setting: this.form.setting, sales: this.form.other};
this.form.configOptions = JSON.stringify(obj);
saveSetting(this.form).then(res => {if(res.code === 0){this.$modal.msgSuccess("操作成功");}this.getList();
});

请求时后端直接报错

Resolved[org.springframework.http.converter.HttpMessageNotReadableException: 
JSON parse error: Cannot construct instance of `com.xxx.request.SettingRequest` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); 
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.xxx.request.SettingRequest` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)at [Source: (PushbackInputStream); line: 1, column: 2]]

报错的原因就是无法正常反序列化,最后的解决办法是使用@Accessors(chain = true)替代@Builder即可解决。
具体原因可以分别看一下两个注解生成的代码。

  • 使用@Accessors(chain = true)编译后的代码
public SettingRequest() {
}public Long getId() {return this.id;
}@NotNull(message = "请选择店铺"
)
public Integer getStoreId() {return this.storeId;
}@NotNull(message = "请填写配置项"
)
public String getConfigOptions() {return this.configOptions;
}public SettingRequest setId(Long id) {this.id = id;return this;
}public SettingRequest setStoreId(@NotNull(message = "请选择店铺") Integer storeId) {this.storeId = storeId;return this;
}public SettingRequest setConfigOptions(@NotNull(message = "请填写配置项") String configOptions) {this.configOptions = configOptions;return this;
}//toString()
  • 使用@Builder编译后的代码
SettingRequest(Long id, @NotNull(message = "请选择店铺") Integer storeId, @NotNull(message = "请填写配置项") String configOptions) {this.id = id;this.storeId = storeId;this.configOptions = configOptions;
}public static SettingRequest.SettingRequestBuilder builder() {return new SettingRequest.SettingRequestBuilder();
}@NotNull(message = "请填写配置项"
)
public String getConfigOptions() {return this.configOptions;
}public void setConfigOptions(@NotNull(message = "请填写配置项") String configOptions) {this.configOptions = configOptions;
}public static class SettingRequestBuilder {private Long id;private Integer storeId;private String configOptions;SettingRequestBuilder() {}public SettingRequest.SettingRequestBuilder id(Long id) {this.id = id;return this;}public SettingRequest.SettingRequestBuilder storeId(@NotNull(message = "请选择店铺") Integer storeId) {this.storeId = storeId;return this;}public SettingRequest.SettingRequestBuilder configOptions(@NotNull(message = "请填写配置项") String configOptions) {this.configOptions = configOptions;return this;}public SettingRequest build() {return new SettingRequest(this.id, this.storeId, this.whiteList, this.configOptions);}// toString()}

@Builder不能被正常的反序列化。

关键字:慎用lombok.Builder

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: