2

我想将包含其他实体的休眠实体序列化和反序列化为平面 JSON 格式。假设我有以下实体:

钥匙:

@Entity
public class Key implements Serializable{

private static final long serialVersionUID = 1L;

@Id
@Column(name = "KeyID")
private Long id;

@Column
private String description;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "KeyTypeID", nullable = false)
private KeyType keyType;

public Long getId() {
    return id;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public KeyType getKeyType() {
    return keyType;
}

public void setKeyType(KeyType keyType) {
    this.keyType = keyType;
}
}

密钥类型:

@Entity
public class KeyType implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "KeyTypeID")
private Long id;

@Column(name = "Name", nullable = false, unique = true)
private String name;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

我想将 Key 类的对象序列化为:

{
    "keyID": 1,    
    "description": "key 1",
    "keyTypeName": "Type 5" //this is Key.keyType.name
}

我还希望能够将上面的 JSON 反序列化为包含 KeyType 实体的 Key 对象。这可能使用杰克逊还是我需要实现自定义代码?

4

1 回答 1

0

注意: 我是EclipseLink JAXB (MOXy)负责人,也是JAXB (JSR-222)专家组的成员。

我不确定 Jackson 是否支持这个用例,但下面是一个例子,说明如何使用 MOXy 的@XmlPath扩展来做到这一点。

钥匙

通过指定@XmlPath(".")引用对象的内容被拉到源对象对应的节点中。

import java.io.Serializable;

import javax.persistence.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@Entity
@XmlAccessorType(XmlAccessType.FIELD)
public class Key implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "KeyID")
    @XmlElement(name="keyID")
    private Long id;

    @Column
    private String description;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "KeyTypeID", nullable = false)
    @XmlPath(".")
    private KeyType keyType;

}

密钥类型

@XmlElement注释用于映射到 JSON 键。

import java.io.Serializable;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlElement;

@Entity
public class KeyType implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "KeyTypeID")
    private Long id;

    @Column(name = "Name", nullable = false, unique = true)
    @XmlElement(name="keyTypeName")
    private String name;

}

jaxb.properties

要将 MOXy 指定为您的 JAXB 提供程序,您需要包含一个jaxb.properties在与域模型相同的包中调用的文件,其中包含以下条目:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

演示

下面的演示代码将您的 JSON 文档转换为/从您的域模型转换。

import java.util.*;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Key.class}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StreamSource json = new StreamSource("src/forum13819583/input.json");
        Key key = unmarshaller.unmarshal(json, Key.class).getValue();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(key, System.out);
    }

}

输入.json/输出

{
    "keyID": 1,    
    "description": "key 1",
    "keyTypeName": "Type 5" 
}

了解更多信息

于 2012-12-12T18:49:42.617 回答