欢迎光临
我们一直在努力

jackson使用Map转Java Bean报错:UnrecognizedPropertyException

在使用jackson将Map类属性转换为Java实体类的过程中出现以下错误:

错误信息

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "receiverIds" (class com.inspur.message.model.MessageSend), not marked as ignorable (13 known properties: "typeTag", "messageId", "isAnonymous", "parentId", "isInvaild", "sendPersonName", "sendCorpId", "isSended", "sendTime", "msgTitle", "sendPersonId", "msgContents", "sendCorpName"])

转换代码

其中我的Map类的属性如下:

{"receiverIds": 'FENGMENG', 'msgTitle': '消息', 'msgContents': '内容', 'parentId': ''}

Java实体类的代码如下:

@Data
@ApiModel(value="com.inspur.message.model.MessageSend")
@Table(name = "message_send")
public class MessageSend implements Serializable {

private static final long serialVersionUID = 1L;

        @Id
        @Column(name = "MESSAGE_ID")
        @ApiModelProperty(value="主键")
        private String messageId;

        @Column(name = "PARENT_ID")
        @ApiModelProperty(value="父级ID,用于消息回复关联,非回复信息用0表示")
        private String parentId;

        @Column(name = "TYPE_TAG")
        @ApiModelProperty(value="消息类型,默认00:普通消息")
        private String typeTag;

        @Column(name = "MSG_TITLE")
        @ApiModelProperty(value="标题")
        private String msgTitle;

        @Column(name = "MSG_CONTENTS")
        @ApiModelProperty(value="内容")
        private String msgContents;

        @Column(name = "SEND_PERSON_ID")
        @ApiModelProperty(value="发送人ID")
        private String sendPersonId;

        @Column(name = "SEND_PERSON_NAME")
        @ApiModelProperty(value="发送人")
        private String sendPersonName;

        @Column(name = "SEND_CORP_ID")
        @ApiModelProperty(value="发送人单位ID")
        private String sendCorpId;

        @Column(name = "SEND_CORP_NAME")
        @ApiModelProperty(value="发送人单位")
        private String sendCorpName;

        @Column(name = "SEND_TIME")
        @ApiModelProperty(value="发送时间")
        private String sendTime;

        @Column(name = "IS_ANONYMOUS")
        @ApiModelProperty(value="是否匿名,0:否,1:是,默认0")
        private String isAnonymous;

        @Column(name = "IS_SENDED")
        @ApiModelProperty(value="是否已发送,0:否,1:是,默认1")
        private String isSended;

        @Column(name = "IS_INVAILD")
        @ApiModelProperty(value="是否删除,0:否,1:是,默认否")
        private String isInvaild;

}

转换的代码如下:

@PostMapping
public Page sendMessage(@RequestBody HashMap<String, Object> parameters) {
    Page page = new Page(Collections.emptyList());
    String receiverIds = (String) parameters.get("receiverIds");
    // 根据接收人receiverIds
    String[] receiversArray = StringUtils.split(receiverIds, ",");

    ObjectMapper m = new ObjectMapper();
    MessageSend messageSend = m.convertValue(parameters, MessageSend.class);

    if (receiverIds.length() > 0) {
        boolean result = service.sendMessage(Arrays.asList(receiversArray), messageSend);
        if (!result) {
            page.setCode(-1000);
            page.setMsg("error");
        }
    }
    return page;
}

出错原因

出错原因:可以看出来Map类有属性receiverIds,而Java实体类里并没有此属性。

解决办法

解决办法有两种:

1.更改转换代码

@PostMapping
public Page sendMessage(@RequestBody HashMap<String, Object> parameters) {
    Page page = new Page(Collections.emptyList());
    String receiverIds = (String) parameters.get("receiverIds");
    // 根据接收人receiverIds
    String[] receiversArray = StringUtils.split(receiverIds, ",");

    ObjectMapper m = new ObjectMapper();
    // 解决不存在属性报错情况
    m.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    MessageSend messageSend = m.convertValue(parameters, MessageSend.class);

    if (receiverIds.length() > 0) {
        boolean result = service.sendMessage(Arrays.asList(receiversArray), messageSend);
        if (!result) {
            page.setCode(-1000);
            page.setMsg("error");
        }
    }
    return page;
}

2.在Java实体类前增加注解

@Data
@ApiModel(value="com.inspur.message.model.MessageReceive")
@Table(name = "message_receive")
@JsonIgnoreProperties(ignoreUnknown = true)
public class MessageReceive implements Serializable {
    // other codes
}

参考:

  1. Jackson反序列化错误:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field的解决方法
赞(0)
未经允许不得转载:Ddmit » jackson使用Map转Java Bean报错:UnrecognizedPropertyException

评论 抢沙发

登录

找回密码

注册