微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

一个域模型,多个json视图

我们有一组域类,通过杰克逊使用泽西服务序列化为json.我们目前正在使用JAXB对类进行注释(尽管我们并不依赖于它).这很好用.但我们希望为不同的用例提供不同的类序列化.

>网站
>移动应用程序
>管理工具
>公共API

在每种情况下,我们可能会或可能不希望包含在json视图中的不同字段.例如,管理工具可能需要一些参数来设置数据权限.移动客户端需要与网站不同的媒体流URL.该网站具有字段所需的特定命名约定.

在Jersey中管理不同服务端点的json不同映射的最佳实践是什么?

谢谢!

解决方法

注意:我是 EclipseLink JAXB (MOXy)领导者,也是 JAXB (JSR-222)专家组的成员.

MOXy提供基于JAXB注释的JSON绑定以及允许您将备用映射应用于域模型的外部绑定文档.我将在下面举例说明.

元数据作为JAXB注释

下面是使用标准JAXB注释的简单Java模型映射.

package forum10761762;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccesstype.FIELD)
public class Customer {

    int id;

    @XmlElement(name="first-name")
    String firstName;

    @XmlElement(name="last-name")
    String lastName;

}

备用元数据#1(alternate1.xml)

在这里,我们将使用XML映射文档通过使它们成为@XmlTransient来取消映射几个字段.

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum10761762">
    <java-types>
        <java-type name="Customer">
            <java-attributes>
                <xml-transient java-attribute="id"/>
                <xml-transient java-attribute="firstName"/>
             </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

备用元数据#2(alternate2.xml)

在这里,我们将使用MOXy的基于路径的映射扩展将Java模型映射到不同的JSON结构.

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum10761762">
    <java-types>
        <java-type name="Customer">
            <java-attributes>
                <xml-element java-attribute="firstName" xml-path="personalInfo/firstName/text()"/>
                <xml-element java-attribute="lastName" xml-path="personalInfo/lastName/text()"/>
             </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

演示代码

package forum10761762;

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

public class Demo {

    public static void main(String[] args) throws Exception {
        Customer customer = new Customer();
        customer.id = 123;
        customer.firstName = "Jane";
        customer.lastName = "Doe";

        Map<String,Object> properties = new HashMap<String,Object>();
        properties.put(JAXBContextProperties.MEDIA_TYPE,"application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT,false);

        // Output #1
        JAXBContext jc1 = JAXBContext.newInstance(new Class[] {Customer.class},properties);
        marshal(jc1,customer);

        // Output #2
        properties.put(JAXBContextProperties.OXM_MetaDATA_SOURCE,"forum10761762/alternate1.xml");
        JAXBContext jc2 = JAXBContext.newInstance(new Class[] {Customer.class},properties);
        marshal (jc2,"forum10761762/alternate2.xml");
        JAXBContext jc3 = JAXBContext.newInstance(new Class[] {Customer.class},properties);
        marshal(jc3,customer);
    }

    private static void marshal(JAXBContext jc,Object object) throws Exception {
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
        marshaller.marshal(object,System.out);
        System.out.println();
    }

}

产量

以下是运行演示代码输出.从同一对象模型中注意到产生了3个不同的JSON文档.

{
   "id" : 123,"first-name" : "Jane","last-name" : "Doe"
}
{
   "last-name" : "Doe"
}
{
   "id" : 123,"personalInfo" : {
      "firstName" : "Jane","lastName" : "Doe"
   }
}

更多信息(来自我的博客)

> JSON Binding with EclipseLink MOXy – Twitter Example
> MOXy as Your JAX-RS JSON Provider – MOXyJsonProvider
> MOXy’s XML Metadata in a JAX-RS Service
> Specifying EclipseLink MOXy as Your JAXB Provider

原文地址:https://www.jb51.cc/js/156651.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐