json - JAX-RS Jersey JSON 使用注释保留 null

标签 json jaxb jersey jax-rs

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

字符串:{"id":null,"fname":"John","re​​gion":""}

返回对象:

 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 注释,但没有成功。

最佳答案

注意:我是EclipseLink JAXB (MOXy) JAXB (JSR-222) 的领导者和成员专家组。

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

MOXy 将根据需要解码 Person 对象,而无需指定任何元数据。要使输出 JSON 包含 null 值,您可以使用 @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 的示例,请参阅:

关于json - JAX-RS Jersey JSON 使用注释保留 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8748537/

相关文章:

使用 IntelliJ 生成代码时 Java java.lang.ClassCastException : javax. xml.bind.JAXBElement 无法转换异常

java - 使用 JAXB 编码/解码字段以标记属性

java - 我的 jar 中缺少标准消息正文阅读器提供程序

java - 带有 Jersey Java 的 POST 返回 415 不支持的媒体类型

javascript - 使用 HTML 将 JSON 数据转换为动态表

java - Jersey / jackson : how to catch json mapping exception?

json - Square 的 Retrofit 响应解析逻辑 : streaming?

java - CascadeType导致的MessageBodyWriter错误

Java + Jersey 服务器启动错误

python - 在 Python 中将 CSV 数据转换为嵌套的 JSON