一个域模型,多个json视图

一个域模型,多个json视图,json,jaxb,jersey,jackson,Json,Jaxb,Jersey,Jackson,我们有一组域类,它们使用jersey服务通过jackson序列化为json。我们目前正在用JAXB注释这些类(尽管我们并不局限于此)。这个很好用。但我们希望为不同的用例提供不同的类序列化 网站 移动应用程序 管理工具 公共API 在每种情况下,都有不同的字段,我们可能希望也可能不希望包含在json视图中。例如,管理工具可能需要一些参数来设置对数据的权限。移动客户端需要与网站不同的媒体流URL。该网站对字段有特定的命名约定 在泽西岛管理不同服务端点的json映射的最佳实践是什么 谢谢 注意:我

我们有一组域类,它们使用jersey服务通过jackson序列化为json。我们目前正在用JAXB注释这些类(尽管我们并不局限于此)。这个很好用。但我们希望为不同的用例提供不同的类序列化

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

在泽西岛管理不同服务端点的json映射的最佳实践是什么


谢谢

注意:我是专家组的负责人和成员

MOXy提供了基于JAXB注释的JSON绑定,以及一个外部绑定文档,允许您将替代映射应用于域模型。下面我将用一个例子来演示

元数据作为JAXB注释

下面是一个带有标准JAXB注释的简单Java模型映射

用于UM10761762的包;
导入javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
公共类客户{
int-id;
@xmlement(name=“first name”)
字符串名;
@xmlement(name=“last name”)
字符串lastName;
}
备用元数据#1(alternate1.xml)

在这里,我们将使用XML映射文档通过使两个字段
@xmltransive
取消映射


备用元数据#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>
有关更多信息(来自我的博客)


您的最终解决方案是什么?这是一个非常有趣的话题,但为什么没有任何回应或答案呢。我正在处理同样的问题。我认为Jacson JsonView是个不错的选择。你可以参考导言。最后,我们为每个类/视图组合创建了一个小哈希集,其中包含我们希望在json中使用的白名单属性,然后将对象传递给和ObjectMapper,并使用SimpleBeanPropertyFilter.filterOutAllExcept创建jsonRick。谢谢你的帮助。它非常有用。@RickMangi-它可以与任何JAX-RS实现一起使用。泽西队和莫西队紧密合作:
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();
    }

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