no String-argument constructor/factory method to deserialize from

今天给大家分享一个开发中遇到的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;另外,祝大家猪年开工大吉!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值