常州专业网站建设公司咨询,中国建设人才网证书查询,建个静态网站,顺德电子画册网站建设lombok是一个Java库#xff0c;使用注解方式来简化 Java 代码#xff0c;可以减少诸如getter、setter的方法。 它常用的注解有#xff1a;
Getter、Setter、ToString 不必多说#xff0c;分别实现getter、setter、toString、hashCode等方法。Data 则是包含上面的那些…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 ResultBoolean 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不能被正常的反序列化。