通过反转@XmlElement重命名的影响来解组JSON吗? [英] Unmarshalling json by reversing the affect of @XmlElement renaming?

查看:138
本文介绍了通过反转@XmlElement重命名的影响来解组JSON吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定义如下的类:

I have a class defined as follows:

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;
}

该类用于生成通过Internet进行通信的编组JSON版本.在接收端,我试图解组JSON,但是由于命名上的差异,我遇到了困难,例如,解组库需要一个名为primaryPhone而不是primary-phone的变量,这正是我所拥有的接收端.

This class is being used to generate marshalled JSON versions which are communicated over the internet. On the receiving end I'm trying to unmarshall the JSON, but I'm having difficulty because of the difference in naming, i.e. for example the unmarshalling library expects a variable named primaryPhone, not primary-phone which is what I have at the receiving end.

除了预处理接收到的JSON文本以将primary-phone的实例手动替换为primaryPhone之外,还有其他一些更自动化的方法来避免此问题吗?手动转换字符串的问题是,如果明天更改类定义,那么我正在编写的代码也将需要更新.

Besides pre-processing the received JSON text to manually replace instances of primary-phone with primaryPhone, is there some other more automated way to avoid this problem ? The problem with manually converting strings is that tomorrow if the Class definition changes, the code I'm writing will also need to be updated.

下面是一个代码片段,显示了我当前正在执行的操作,而无需进行任何手动字符串转换:

Here's a code snippet showing what I'm currently doing without any manually string conversion:

String contact = "\"last-name\": \"ahmadka\"";  
ObjectMapper objMapper = new ObjectMapper();
Contact cObj = objMapper.readValue(contact, Contact.class);

但是使用上面的代码,我在最后一行看到了一个例外:

But with the above code I get an exception on the last line reading this:

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

推荐答案

默认情况下,杰克逊不了解JAXB注释(即@XmlRootElement).为了具有此功能,需要为它配置一个外部模块.在服务器上,您很可能甚至不知道它.

Jackson by default doesn't know about JAXB annotations (i.e. @XmlRootElement). It needs to be configured with an external module in order to have this capability. On the server, you most likely have this without even knowing it.

如果要在客户端配置ObjectMapper,则需要添加

On the client side if you want to configure the ObjectMapper, then you need to add the following module:

<dependency>
  <groupId>com.fasterxml.jackson.module</groupId>
  <artifactId>jackson-module-jaxb-annotations</artifactId>
  <version>${jackson2.version}</version>
</dependency>

然后只需注册JAXB注释模块.

Then just register the JAXB annotations module.

ObjectMapper objMapper = new ObjectMapper();
mapper.registerModule(new JaxbAnnotationModule());

这篇关于通过反转@XmlElement重命名的影响来解组JSON吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆