JAX-RS Jersey JSON使用注释保留null [英] JAX-RS Jersey JSON preserve null using annotations
问题描述
当接收包含null或值的JSON sting时,如何让JAXB保留空值。
How do I have JAXB preserve nulls when receiving a JSON sting that contains a null or "" value.
字符串: {id:null,fname:John,region:}
返回对象:
Person {
Integer id = 0
String fname = "John"
Region regions = 0 }
我希望它返回null而不是0
I would like it to return null instead of 0
这是我到目前为止所拥有的:
Here is what I have so far:
@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
我试过调查杰克逊注释但没有成功。
Person.class is annotated with @XmlRootElement
I've tried looking into Jackson annotations but have had no success.
推荐答案
注意:我是 EclipseLink JAXB(MOXy) 领导和 JAXB(JSR-222) 专家组。
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
以下是使用MOXy如何完成此操作的示例。
Below is an example of how this can be done with MOXy.
人
MOXy将根据需要解组 Person
对象,而不指定任何元数据。要使输出JSON包含空值,可以使用 @XmlElement(nillable = true)
注释(请参阅绑定到JSON和XML - 处理空值)。
MOXy will unmarshal the Person
object as you want without specifying any metadata. To have the output JSON contain null values, you can use the @XmlElement(nillable=true)
annotation (see 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
的文件。与包含以下条目的域类相同的包(请参阅指定EclipseLink MOXy作为您的JAXB提供程序)。
To specify MOXy as your JAXB (JSR-222) provider you need to add a file called jaxb.properties
in the same package as your domain classes with the following entry (see 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中使用MOXy- RS应用程序
有关在JAX-RS应用程序中使用MOXy的示例,请参阅:
For an example of using MOXy in a JAX-RS application see:
- MOXy as Your JAX-RS JSON Provider - Server Side
- MOXy as Your JAX-RS JSON Provider - Client Side
这篇关于JAX-RS Jersey JSON使用注释保留null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!