今天给大家分享一个开发中遇到的bug
前台报错信息:
Can not construct instance of com.flowermall.shop.base.model.goods.RestrictWordInfo: no String-argument constructor/factory method to deserialize from String value ('sdsd')↵ at [Source: N/A; line: -1, column: -1]"
后台:
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.flowermall.shop.base.model.goods.RestrictWordInfo: no String-argument constructor/factory method to deserialize from String value ('sdsd')
at [Source: N/A; line: -1, column: -1]
其实是一个意思,就是无法构造该model实例;
问题代码块
Action
@DirectMethod(DirectMethodType.STORE_MODIFY)
public DirectStoreValidationResult<RestrictWordInfo> create(
RestrictWordInfo restrictWord,
@AuthenticationPrincipal AppUserDetails userDetails){
try{
restrictWord.setCreator(userDetails.getUsername());
restrictWord.setUpdater(userDetails.getUsername());
restrictWord = restrictWordService.create(restrictWord);
return new DirectStoreValidationResult<>(restrictWord);
}catch(FieldInvalidException ex){
return new DirectStoreValidationResult<>(restrictWord,ex);
}
}
model
public class RestrictWordInfo implements Serializable {
private static final long serialVersionUID = -6471558434681123441L;
private String restrictWord;
private String creator;
private Date createTime;
private String updater;
private Date updateTime;
private Boolean enabled;
private Boolean deleted;
private Integer version;
...
}
问题所在:action在引用model的时候使用了restrictWord作为RestrictWordInfo 类的对象( RestrictWordInfo restrictWord,),但是,restrictWord是RestrictWordInfo 类中的一个字段属性(private String restrictWord;)
Action改完,
@DirectMethod(DirectMethodType.STORE_MODIFY)
public DirectStoreValidationResult<RestrictWordInfo> create(
RestrictWordInfo restrictWordInfo,
@AuthenticationPrincipal AppUserDetails userDetails){
try{
restrictWordInfo.setCreator(userDetails.getUsername());
restrictWordInfo.setUpdater(userDetails.getUsername());
restrictWordInfo = restrictWordService.create(restrictWordInfo);
return new DirectStoreValidationResult<>(restrictWordInfo);
}catch(FieldInvalidException ex){
return new DirectStoreValidationResult<>(restrictWordInfo,ex);
}
}
success
Ps: 建类命名时一定要注意,类名和属性名不要有一样的,否则总会有奇奇怪怪的bug;另外,祝大家猪年开工大吉!!!