是否有可能隐藏“@type"?使用 EclipseLink MOXy (JAXB) 将子类编组为 JSON 时的条目? [英] Is there a possibility to hide the "@type" entry when marshalling subclasses to JSON using EclipseLink MOXy (JAXB)?

查看:34
本文介绍了是否有可能隐藏“@type"?使用 EclipseLink MOXy (JAXB) 将子类编组为 JSON 时的条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我即将开发一个基于 JAX-RS 的 RESTful Web 服务,我使用 MOXy (JAXB) 来自动生成我的 Web 服务的 JSON 响应.

I'm about to develop a JAX-RS based RESTful web service and I use MOXy (JAXB) in order to automatically generate my web service's JSON responses.

一切都很酷,但由于 Web 服务将是基于 JavaScript 的 Web 应用程序的后端,因此可以公开访问,我不想公开某些细节,例如类名等.

Everything is cool, but due to the fact that the web service will be the back-end of a JavaScript-based web application and therefore publicly accessible I don't want to expose certain details like class names, etc.

但是,我意识到在某些情况下 MOXy 会在编组后的字符串中嵌入一个@type"条目,并且该条目后跟刚刚编组的对象的类名.

But, I've realized that under certain conditions MOXy embeds a "@type" entry into the marshalled string and this entry is followed by the class name of the object that has just been marshalled.

特别是,我意识到 MOXy 在编组扩展类的实例时会以这种方式运行.

In particular, I've realized that MOXy behaves in this way when marshalling instances of extended classes.

假设以下超类MyBasicResponse"

Assume the following super class "MyBasicResponse"

@XmlRootElement(name="res")

public class MyBasicResponse {

@XmlElement
private String msg;

public MyBasicResponse() {
    // Just for conformity
}

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}
}

还有这个专门的(扩展的)类MySpecialResponse"

And this specialized (extended) class "MySpecialResponse"

@XmlRootElement(name="res")

public class MySpecialResponse extends MyBasicResponse {

@XmlElement
private String moreInfo;

public MySpecialResponse() {
    // Just for conformity
}

public String getMoreInfo() {
    return moreInfo;
}

public void setMoreInfo(String moreInfo) {
    this.moreInfo = moreInfo;
}
}

所以,MyBasicResponse 对象的编组字符串是

So, the MyBasicResponse object's marshalled string is

{"msg":"A Message."}

(没关系!)

但是,MySpecialResponse 对象的编组字符串就像

But, the MySpecialResponse object's marshalled string is like

{"@type":"MySpecialResponse","msg":"A Message.","moreInfo":"More Information."}

有什么办法可以去掉

"@type":"MySpecialResponse"

我没有回复?

推荐答案

您可以将对象包装在 JAXBElement 的实例中,指定要编组的子类以摆脱类型键.下面是一个完整的例子.

You can wrap your object in an instance of JAXBElement specifying the subclass being marshalled to get rid of the type key. Below is a full example.

与问题相同,但添加了以下 package-info 类以指定字段访问以匹配这些类

Same as from the question, but with the following package-info class added to specifying the field access to match those classes

@XmlAccessorType(XmlAccessType.FIELD)
package com.example.foo;

import javax.xml.bind.annotation.*;

演示代码

演示

import java.util.*;
import javax.xml.bind.*;
import javax.xml.namespace.QName;

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>(2);
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {MySpecialResponse.class}, properties);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        MySpecialResponse msr = new MySpecialResponse();
        marshaller.marshal(msr, System.out);

        JAXBElement<MySpecialResponse> jaxbElement = new JAXBElement(new QName(""), MySpecialResponse.class, msr);
        marshaller.marshal(jaxbElement, System.out);
    }

}

输出

我们看到,当对象被编组时,type 键被编组(对应于 XML 表示中的 xsi:type 属性),因为对于 MOXy 而言有必要区分 MyBasicResponseMySpecialResponse.当我们将对象包装在 JAXBElement 的实例中并限定类型时,MOXy 不需要添加 type 键.

We see that when the object was marshalled an type key was marshalled (corresponding to the xsi:type attribute in the XML representation), because as MOXy is concerned it was necessary to distinguish between MyBasicResponse and MySpecialResponse. When we wrapped the object in an instance of JAXBElement and qualified the type MOXy didn't need to add the type key.

{
   "type" : "mySpecialResponse"
}
{
}

更多信息

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