private static final ObjectMapper objectMapper = new XmlMapper();
public static <T> T xmlToObj(final String data, Class<T> clazz) {
try {
return objectMapper.readValue(data, clazz);
} catch (Exception e) {
log.error("{}", data);
log.error(e.getMessage(), e);
}
return null;
}
public static String objToXML(final Object object) {
try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
log.error(e.getMessage(), e);
}
return "";
}
//这个虽然是写着 JsonIgnoreProperties , 但它在 XML 内容中同样生效的.表示忽略未知的属性
@JacksonXmlRootElement
@JsonIgnoreProperties(ignoreUnknown = true)
public class WxBaseMsg {
@JacksonXmlProperty(localName = "ToUserName")
private String toUserName;
@JacksonXmlProperty(localName = "FromUserName")
private String fromUserName;
@JacksonXmlProperty(localName = "CreateTime")
private long createTime;
@JacksonXmlProperty(localName = "MsgType")
private String msgType;
@JacksonXmlProperty(localName = "AgentID")
private int agentId;
@JacksonXmlProperty(localName = "MsgId")
private long msgId;
public String getToUserName() {
return toUserName;
}
public void setToUserName(final String toUserName) {
this.toUserName = toUserName;
}
public String getFromUserName() {
return fromUserName;
}
public void setFromUserName(final String fromUserName) {
this.fromUserName = fromUserName;
}
public long getCreateTime() {
return createTime;
}
public void setCreateTime(final long createTime) {
this.createTime = createTime;
}
public String getMsgType() {
return msgType;
}
public void setMsgType(final String msgType) {
this.msgType = msgType;
}
public int getAgentId() {
return agentId;
}
public void setAgentId(final int agentId) {
this.agentId = agentId;
}
public long getMsgId() {
return msgId;
}
public void setMsgId(final long msgId) {
this.msgId = msgId;
}
}