如何将Play框架模型转换为XML和JSON? [英] How to convert Play Framework Models into XML and JSON?

查看:171
本文介绍了如何将Play框架模型转换为XML和JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Play Framework是否具有将Play模型转换为XML / JSON的原生推荐方式?类似于JAXB或Jackson的东西。

Does the Play Framework have a Native or Recommended way of converting Play models into XML/JSON? Something similar to JAXB or Jackson.

有人推荐模板方法但这非常冗长,并不能保证格式良好的XML / JSON。

Some people recommend the template approach but this is very verbose and doesn't guarantee well-formed XML/JSON.

播放XML文档只显示XML响应使用字符串连接构建如下:

The Play Documentation on XML just shows an XML response being built using String concatenation like so:

return ok("<message \"status\"=\"OK\">Hello " + name + "</message>");

同样,JSON上的Play文档显示一次一行构建一个JSON对象。 / p>

Similarly, the Play Documentation on JSON shows a JSON object being built up one line at a time.

ObjectNode result = Json.newObject();
result.put("status", "OK");
result.put("message", "Hello " + name);

是否有使用Play将模型序列化为XML / JSON的标准方式?

Is there a Standard way of serializing Models into XML/JSON using Play?

此主题是否有官方游戏文档

推荐答案

简短回答: Jackson 了解 JSON JAXB XML

播放本身不提供编组的任何文档模型,但它附带第三方库,可以完成这项工作。

Play itself doesn't provide any documentation on marshalling models but it does ship with 3rd party libraries that can do the job.

JSON:

型号:

public class User extends Model {
    public String username;
    public Long   age; 

    @JsonIgnore
    public String password; // field won't be marshalled
}

使用jackson的 ObjectMapper.writeValueAsString ()方法。

Marshall it to JSON using jackson's ObjectMapper.writeValueAsString() method.

import org.codehaus.jackson.map.ObjectMapper;
//
ObjectMapper mapper     = new ObjectMapper();
String       jsonString = mapper.writeValueAsString(country);

JSON输出:

{
    "username" : "John Smith",
    "age"      : "25"
}






XML:

必须小心谨慎,因为Play的模型是如何为它的模型播放生成getter和setter 。您不会在代码中看到getter和setter,但它们在运行时存在。

Care must be taken because of how Play generates getters and setters for it's models under the hood. You won't see the getter and setters in the code but they exist at runtime.

在模型上,设置 XmlAccessorType 注释到 PROPERTY 。这告诉JAXB从 getter / setters 序列化,而不是从基础字段序列化。

On the model, it's important to set the XmlAccessorType annotation to PROPERTY. This tells JAXB to serialize from the getter/setters and not from the underlying fields.

@XmlAccessorType(XmlAccessType.PROPERTY)

我们还需要添加 @ XmlRootElement 注释,指定根XML节点的名称:

We also have to add an @XmlRootElement annotation which specifies the name of the root XML node:

@XmlRootElement(name = "UserRoot")

要省略字段,我们必须添加 @ XmlTransient 注释到getter。由于源代码中没有getter,我们必须为每个要省略的字段添加一个。

To omit a field, we must add the @XmlTransient annotation to the getter. Since there is no getter in the source code, we must add one for every field we want to omit.

@XmlAccessorType(XmlAccessType.PROPERTY)
public class User extends Model {
    public String username;
    public Long   age;

    @JsonIgnore
    public String password;


    @XmlTransient // This means ignore this property
    public String getPassword() {
        return this.password;
    }
}

编组由JAXB类执行 Marshaller JAXBContext

The marshalling is performed by the JAXB classes Marshaller and JAXBContext

JAXBContext context    = JAXBContext.newInstance(User.class);
Marshaller  marshaller = context.createMarshaller();

// Use linefeeds and indentation in the outputted XML
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

marshaller.marshal(user, System.out);

输出:

<UserRoot>
    <name>John Smith</name>
    <age>25</age>
</UserRoot>






摘要:

播放有关XML的文档并且在JSON上播放文档确实提供了有关使用json / xml的一些信息,但是没有似乎是任何Play Docs描述如何做编组。为此,我们必须查看第三方库和文档。

The Play docs on XML and the Play docs on JSON do provide some information on working with json/xml but there doesn't seem to be any Play Docs describing how to do Marshalling. For that we have to look at 3rd Party libraries and documentation.

这篇关于如何将Play框架模型转换为XML和JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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