一个域模型,多个json视图 [英] One domain model, multiple json views

查看:51
本文介绍了一个域模型,多个json视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一组域类,它们使用球衣服务通过杰克逊序列化为json.我们目前正在用JAXB注释类(尽管我们并不局限于此).这很好.但是我们想为不同的用例提供不同的类序列化.

We have a set of domain classes which are serialized to json via jackson using jersey services. We are currently annotating the classes with JAXB (although we're not tied to that). This works fine. But we want to offer different serializations of the classes for different use cases.

  • 网站
  • 移动应用
  • 管理工具
  • 公共API

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

In each of these cases there are different fields which we may or may not want included in the json view. For example, the admin tool might need some parameters for setting permissions on data. The mobile client needs a different URL to a media stream than the website. The website has particular naming conventions it needs for fields.

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

What is the best practice for managing different mappings of json for different service endpoints in Jersey?

谢谢!

推荐答案

注意:我是 JAXB(JSR-222) 专家组.

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

MOXy提供基于JAXB批注的JSON绑定,以及允许您将备用映射应用于域模型的外部绑定文档.我将在下面通过示例进行演示.

MOXy offers JSON-binding based on JAXB annotations as well as an external binding document that allows you to apply alternate mappings to a domain model. I will demonstrate below with an example.

元数据作为JAXB注释

下面是带有标准JAXB批注的简单Java模型映射.

Below is a simple Java model mapping with the standard JAXB annotations.

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来取消映射几个字段.

Here we will use the XML mapping document to unmap a couple of fields by making them @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结构.

Here we will map the Java model to a different JSON structure using MOXy's path based mapping extension.

<?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, customer);

        // Output #2
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "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文档.

Below is the output from running the demo code. Note from the same object model 3 different JSON documents were produced.

{
   "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

这篇关于一个域模型,多个json视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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