简体   繁体   English

将Java对象编组为JSON使用JAX-RS添加空属性

[英]marshalling java object to JSON adds empty property using JAX-RS

I'm using jax-rs and adding entity for being marshalled in Jetty by JAXRSOutInterceptor, but JSON output is being modified with additional empty property which looks like this: "$": "" 我正在使用jax-rs并添加由JAXRSOutInterceptor在Jetty中编组的实体,但是JSON输出正在使用其他空属性修改,如下所示:“ $”:“”

The JSONProvider is created and configured as here: JSONProvider的创建和配置如下:

JSONProvider jsonProvider = new JSONProvider();
jsonProvider.setConvertTypesToStrings(true);
jsonProvider.setIgnoreNamespaces(true);
jsonProvider.setIgnoreMixedContent(true);
jsonProvider.setUnmarshallAsJaxbElement(true);
providers.add(jsonProvider);

It is also being marshalled to XML which uses namespaces but I don't want them in JSON output and input. 它也被编组为使用名称空间的XML,但我不希望它们出现在JSON输出和输入中。

The object that is being marshalled is similar to this: 被编组的对象与此类似:

@XmlRootElement(name="myObject1")
@XmlAccessorType(XmlAccessType.FIELD)
@SuppressWarnings("serial")
public class MyObject1 implements Serializable {

    MyObject2 a;
    MyObject2 b;
    MyObject2 c;

// includes getters, setters, hashCode, equals, toString,   
}

When MyObject2 is: 当MyObject2是:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@SuppressWarnings("serial")
public class MyObject2 implements Serializable {

    String x;
    String y;
    List<String> z;

// includes getters, setters, hashCode, equals, toString,   
}

The rest output is as following: 其余输出如下:

{
   "myObject1": {
      "a": {
         "x": "value1",
         "y": "value2",
         "z": "value3",
         "$": ""
      },
      "$": ""
   }
}

How do I get rid of the ending "$": "" I read that the Jettison (which is the default JSONProvider implementation that I am using) is by default will represent properties mapped with @XmlValue as '$'s but there's no property ? 我如何摆脱结尾的“ $”:“”我读到Jettison(这是我正在使用的默认JSONProvider实现)默认情况下会将表示与@XmlValue映射的属性表示为'$',但没有属性?

Is that caused by implementing Serializable? 这是由实现Serializable引起的吗?

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group. 注意:我是EclipseLink JAXB(MOXy)的负责人,并且是JAXB(JSR-222)专家组的成员。

Jettison isn't really a JSON provider but an API that can allow XML libraries to produce/consume JSON. Jettison并不是真正的JSON提供程序,而是可以允许XML库产生/使用JSON的API。 It does a decent job, but there are problems that can occur like the one you are experiencing now. 它做得不错,但是可能会像您现在遇到的问题一样出现问题。 You are also seeing the issues where lists of size one are not marshalled as JSON arrays. 您还会看到没有将大小为1的列表编组为JSON数组的问题。

If you can't find a way to make this work with your current set up. 如果您无法找到一种方法来使当前设置生效。 Below is what you could do with MOXy as your JSON provider: 下面是将MOXy用作JSON提供程序时可以做的事情:

MyObject1 MyObject1

package forum11262807;

import java.io.Serializable;
import javax.xml.bind.annotation.*;

@XmlRootElement(name="myObject1")
@XmlAccessorType(XmlAccessType.FIELD)
@SuppressWarnings("serial")
public class MyObject1 implements Serializable {

    MyObject2 a;
    MyObject2 b;
    MyObject2 c;

}

MyObject2 MyObject2

package forum11262807;

import java.io.Serializable;
import java.util.List;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@SuppressWarnings("serial")
public class MyObject2 implements Serializable {

    String x;
    String y;
    List<String> z;

}

jaxb.properties jaxb.properties

To specify MOXy as your JAXB provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry: 要将MOXy指定为JAXB提供程序,您需要在与域模型相同的包中包含一个名为jaxb.properties的文件,并带有以下条目:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Demo 演示版

Below is some standalone code to demonstrate the reading/writing of the JSON. 下面是一些独立的代码,用于演示JSON的读取/写入。

package forum11262807;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(MyObject1.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setProperty("eclipselink.media-type", "application/json");
        File json = new File("src/forum11262807/input.json");
        MyObject1 myObject1 = (MyObject1) unmarshaller.unmarshal(json);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty("eclipselink.media-type", "application/json");
        marshaller.marshal(myObject1, System.out);
    }

}

input.json/Output input.json /输出

The resulting JSON message contains no "$" properties and the list of size 1 is property represented as a JSON array. 所得的JSON消息不包含"$"属性,并且大小为1的列表以JSON数组表示。

{
   "myObject1" : {
      "a" : {
         "x" : "value1",
         "y" : "value2",
         "z" : [ "value3" ]
      }
   }
}

JAX-RS Integration JAX-RS集成

MOXy includes the MOXyJsonProvider class that makes it easy to configure in your JAX-RS application: MOXy包含MOXyJsonProvider类,可轻松在JAX-RS应用程序中进行配置:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM