通过逆转@XmlElement 重命名的影响来解组 json?
Unmarshalling json by reversing the affect of @XmlElement renaming?
我有一个class定义如下:
public class Contact implements Serializable
{
private final static long serialVersionUID = 1L;
@XmlElement(name = "last-name", required = true)
protected String lastName;
@XmlElement(name = "first-name", required = true)
protected String firstName;
@XmlElement(required = true)
protected String id;
@XmlElement(name = "primary-phone")
protected String primaryPhone;
@XmlElement(name = "cellular-phone")
protected String cellularPhone;
}
此 class 用于生成通过 Internet 通信的编组 JSON 版本。在接收端,我试图解组 JSON,但由于命名不同,我遇到了困难,例如,解组库需要一个名为 primaryPhone
的变量,而不是 [=15] =] 这就是我在接收端所拥有的。
除了预处理收到的 JSON 文本以手动将 primary-phone
实例替换为 primaryPhone
之外,还有其他更自动化的方法可以避免此问题吗?手动转换字符串的问题是,明天如果 Class 定义发生变化,我正在编写的代码也需要更新。
这是一个代码片段,展示了我目前正在做的事情,没有任何手动字符串转换:
String contact = "\"last-name\": \"ahmadka\"";
ObjectMapper objMapper = new ObjectMapper();
Contact cObj = objMapper.readValue(contact, Contact.class);
但是使用上面的代码,我在最后一行读到这个异常:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "last-name" (class Contact), not marked as ignorable (5 known properties: "lastName", "cellularPhone", "id", "primaryPhone", "firstName", ])
at ...........//rest of the stack
Jackson 默认情况下不知道 JAXB 注释(即 @XmlRootElement
)。它需要配置外部模块才能具有此功能。在服务器上,您很可能在不知情的情况下拥有它。
客户端如果要配置ObjectMapper
,则需要添加following module:
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>${jackson2.version}</version>
</dependency>
然后只需注册 JAXB 注释模块。
ObjectMapper objMapper = new ObjectMapper();
mapper.registerModule(new JaxbAnnotationModule());
我有一个class定义如下:
public class Contact implements Serializable
{
private final static long serialVersionUID = 1L;
@XmlElement(name = "last-name", required = true)
protected String lastName;
@XmlElement(name = "first-name", required = true)
protected String firstName;
@XmlElement(required = true)
protected String id;
@XmlElement(name = "primary-phone")
protected String primaryPhone;
@XmlElement(name = "cellular-phone")
protected String cellularPhone;
}
此 class 用于生成通过 Internet 通信的编组 JSON 版本。在接收端,我试图解组 JSON,但由于命名不同,我遇到了困难,例如,解组库需要一个名为 primaryPhone
的变量,而不是 [=15] =] 这就是我在接收端所拥有的。
除了预处理收到的 JSON 文本以手动将 primary-phone
实例替换为 primaryPhone
之外,还有其他更自动化的方法可以避免此问题吗?手动转换字符串的问题是,明天如果 Class 定义发生变化,我正在编写的代码也需要更新。
这是一个代码片段,展示了我目前正在做的事情,没有任何手动字符串转换:
String contact = "\"last-name\": \"ahmadka\"";
ObjectMapper objMapper = new ObjectMapper();
Contact cObj = objMapper.readValue(contact, Contact.class);
但是使用上面的代码,我在最后一行读到这个异常:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "last-name" (class Contact), not marked as ignorable (5 known properties: "lastName", "cellularPhone", "id", "primaryPhone", "firstName", ])
at ...........//rest of the stack
Jackson 默认情况下不知道 JAXB 注释(即 @XmlRootElement
)。它需要配置外部模块才能具有此功能。在服务器上,您很可能在不知情的情况下拥有它。
客户端如果要配置ObjectMapper
,则需要添加following module:
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>${jackson2.version}</version>
</dependency>
然后只需注册 JAXB 注释模块。
ObjectMapper objMapper = new ObjectMapper();
mapper.registerModule(new JaxbAnnotationModule());