lombook注意点1
1. pId格式的值传递成pid问题.
问题描述
一个实体类存有pId,即第一个字母小写,第二个字母大写的情况.
public class Department {
private Long id;
private Long pId;
private String name;
}
如果走idea自动生成的get/set方法,为区分其驼峰,get/set后面跟的第一个字母不会大写,这样输出给前端的值将是正常的驼峰格式.
public class Department {
private Long id;
private Long pId;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
// 这里的get方法时将p小写.没有遵循驼峰格式
public Long getpId() {
return pId;
}
public void setpId(Long pId) {
this.pId = pId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
但通过lombook的@data标签修饰的类生成的class源文件中的类如下.其pId的get/set方法严格按照驼峰格式.
public class Department {
private Long id;
private Long pId;
private String name;
public Department() {
}
public Long getId() {
return this.id;
}
public Long getPId() {
return this.pId;
}
public String getName() {
return this.name;
}
public void setId(final Long id) {
this.id = id;
}
public void setPId(final Long pId) {
this.pId = pId;
}
public void setName(final String name) {
this.name = name;
}
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Department)) {
return false;
} else {
Department other = (Department)o;
if (!other.canEqual(this)) {
return false;
} else {
label47: {
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id == null) {
break label47;
}
} else if (this$id.equals(other$id)) {
break label47;
}
return false;
}
Object this$pId = this.getPId();
Object other$pId = other.getPId();
if (this$pId == null) {
if (other$pId != null) {
return false;
}
} else if (!this$pId.equals(other$pId)) {
return false;
}
Object this$name = this.getName();
Object other$name = other.getName();
if (this$name == null) {
if (other$name != null) {
return false;
}
} else if (!this$name.equals(other$name)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof Department;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $id = this.getId();
int result = result * 59 + ($id == null ? 43 : $id.hashCode());
Object $pId = this.getPId();
result = result * 59 + ($pId == null ? 43 : $pId.hashCode());
Object $name = this.getName();
result = result * 59 + ($name == null ? 43 : $name.hashCode());
return result;
}
public String toString() {
return "Department(id=" + this.getId() + ", pId=" + this.getPId() + ", name=" + this.getName() + ")";
}
}
这样在序列化的时候通过get方法转参数时,会将P与I都转成小写.从而输出给前端的时pid这样的.
解决方法
重写get/set方法.破坏驼峰格式. 如pId的get方法直接写成getpId().
修改序列化方法,不从get方法中反推需要序列化的参数
@Configuration public class WebConfigurer implements WebMvcConfigurer { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { val converter = new FastJsonHttpMessageConverter(); converter.setSupportedMediaTypes( Lists.newArrayList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_UTF8)); converter.getFastJsonConfig().setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue); converters.add(0, converter); val builder = Jackson2ObjectMapperBuilder.xml(); builder.indentOutput(true); converters.add(new MappingJackson2XmlHttpMessageConverter(builder.build())); } }