在使用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
}
参考:
最新评论
网飞没问题, 迪士尼+有解决方案么?
pp助手是安卓手机用的,根本下载用不来苹果
已解决
这样的话数据库里的结构为{"attachment":{"content":"xxx"}}, 要怎么才能变成{"content":"xxx"},从而使结构保持一致?
赞! make test不过的坑都写到的,谢谢楼主~
谢谢你
用了root用户还是一直502是怎么回事呢
student id 是空的