【问题标题】:JAX-RS Jersey JSON preserve null using annotationsJAX-RS Jersey JSON 使用注释保留 null
【发布时间】:2012-01-05 19:29:29
【问题描述】:

当接收到包含 null 或 "" 值的 JSON 字符串时,如何让 JAXB 保留 null。

字符串:{"id":null,"fname":"John","region":""}

返回对象:

 Person {
    Integer id = 0
    String fname = "John"
    Region regions = 0 }

我希望它返回 null 而不是 0

这是我目前所拥有的:

@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
    private JAXBContext context;
    private Class<?>[] types = {Person.class};

    public JAXBContextResolver() throws Exception {
        this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), types);
    }

    public JAXBContext getContext(Class<?> objectType) {
        for (Class<?> c : types) {
            if (c.equals(objectType)) {
                return context;
            }
        }
        return null;
    }
}

Person.class 用@XmlRootElement 进行了注释我已经尝试查看Jackson 注释但没有成功。

【问题讨论】:

  • 这里有同样的问题。有人为这个问题找到了解决方案。泽西岛似乎忽略了 ObjectMapper
  • 同样的问题。我也尝试过使用 ObjectMapper,但 Jersey 也未能成功。我不确定使用 ObjectMapper 是不是最简单的解决方案......
  • 我的类似问题已经解决。你可以看看对你有没有帮助...stackoverflow.com/questions/10420641/…

标签: json jaxb jersey jax-rs


【解决方案1】:

注意:我是EclipseLink JAXB (MOXy) 的负责人,也是JAXB (JSR-222) 专家组的成员。

以下是如何使用 MOXy 完成此操作的示例。

人物

MOXy 将根据需要解组 Person 对象,而无需指定任何元数据。要让输出 JSON 包含空值,您可以使用 @XmlElement(nillable=true) 注释(请参阅 Binding to JSON & XML - Handling Null)。

package forum8748537;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Person {

    @XmlElement(nillable=true)
    Integer id;

    @XmlElement(nillable=true)
    String fname;

    @XmlElement(nillable=true)
    Region regions;

}

jaxb.properties

要将 MOXy 指定为您的 JAXB (JSR-222) 提供程序,您需要在与域类相同的包中添加一个名为 jaxb.properties 的文件,并使用以下条目(请参阅 Specifying EclipseLink MOXy as Your JAXB Provider)。

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

演示

package forum8748537;

import java.io.StringReader;
import java.util.*;

import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(2);
        properties.put("eclipselink.media-type", "application/json");
        properties.put("eclipselink.json.include-root", false);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Person.class}, properties);

        StringReader json = new StringReader("{\"id\":null,\"fname\":\"John\",\"region\":\"\"}");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Person person = unmarshaller.unmarshal(new StreamSource(json), Person.class).getValue();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(person, System.out);
    }

}

输出

{
   "id" : null,
   "fname" : "John",
   "regions" : null
}

在 JAX-RS 应用程序中使用 MOXy

有关在 JAX-RS 应用程序中使用 MOXy 的示例,请参阅:

【讨论】:

    【解决方案2】:

    从 JSON-B JSR-367 (JAX-RS 2.1 JSR-370) 开始,以下注释代替 @XmlElement 起作用。

    @JsonbProperty(nillable = true)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-08
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多