本文介绍了是否有可能隐藏&"@ 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而言,它区分 MyBasicResponse MySpecialResponse 是必要的.当我们将对象包装在 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"
}
{
}

更多信息

07-23 19:54