本文介绍了是否有可能隐藏“@ 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);
    }

}

输出

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

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



更多信息






  • For More Information

    • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
    • http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html
    • 这篇关于是否有可能隐藏“@ type”。使用EclipseLink MOXy(JAXB)将子类编组到JSON时的条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 19:55