Java 使用JAXB在Jersey RESTful API的JSON输出中包含null元素
我想通过Jersey RESTful API公开一个类。它看起来像这样:Java 使用JAXB在Jersey RESTful API的JSON输出中包含null元素,java,json,jaxb,jersey,jax-rs,Java,Json,Jaxb,Jersey,Jax Rs,我想通过Jersey RESTful API公开一个类。它看起来像这样: @XmlRootElement public class Data { public String firstName; public String lastName; } 我的问题是这些字段可能为空,在这种情况下,JSON输出中会忽略该字段。我希望所有字段都存在,无论其值如何。例如,如果lastName为null,JSON输出将为: { "firstName" : "Oleksi" } 而不是我
@XmlRootElement
public class Data {
public String firstName;
public String lastName;
}
我的问题是这些字段可能为空,在这种情况下,JSON输出中会忽略该字段。我希望所有字段都存在,无论其值如何。例如,如果lastName为null,JSON输出将为:
{
"firstName" : "Oleksi"
}
而不是我想要的:
{
"firstName" : "Oleksi",
"lastName" : null
}
我有一个JAXBContextResolver(ContextResolver的一个实现),如下所示:
@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
// internal state
private final JAXBContext context;
private final Set<Class> types;
private final Class[] cTypes = { Data.class };
public JAXBContextResolver()
throws Exception {
types = new HashSet(Arrays.asList(cTypes));
context = new JSONJAXBContext(JSONConfiguration.natural().humanReadableFormatting(true).build(), cTypes);
}
@Override
public JAXBContext getContext(Class<?> objectType) {
return (types.contains(objectType)) ? context : null;
}
}
@Provider
公共类JAXBContextResolver实现ContextResolver{
//内部状态
私有上下文上下文;
私有最终集类型;
私有最终类[]cTypes={Data.Class};
公共JAXBContextResolver()
抛出异常{
types=新的HashSet(Arrays.asList(cTypes));
context=newJSONJAXBContext(JSONConfiguration.natural().humanReadableFormatting(true.build(),cTypes);
}
@凌驾
公共JAXBContext getContext(类objectType){
返回(types.contains(objectType))?上下文:null;
}
}
一段时间以来,我一直在试图找出如何获得所需的输出,但我一直运气不佳。我愿意尝试其他ContextResolver/Serializer,但我还没有找到一个有效的,所以代码示例会很好。JavaNull是JavaScript的未定义。如果要将Java null转换为JavaScript null,需要查阅转换库。对于的JSON绑定,正确的映射如下所示。您可以与您的提供商一起尝试,看看它是否也适用:
@XmlRootElement
public class Data {
@XmlElement(nillable=true)
public String firstName;
@XmlElement(nillable=true)
public String lastName;
}
了解更多信息
更新2 EclipseLink 2.4包括
MOXyJsonProvider
,它是MessageBodyReader
/MessageBodyWriter
的实现,您可以直接使用它来利用MOXy的JSON绑定
MessageBodyReader
/MessageBodyWriter
可能更适合您:
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import javax.xml.transform.stream.StreamSource;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import javax.ws.rs.ext.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class MOXyJSONProvider implements
MessageBodyReader<Object>, MessageBodyWriter<Object>{
@Context
protected Providers providers;
public boolean isReadable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
return true;
}
public Object readFrom(Class<Object> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
throws IOException, WebApplicationException {
try {
Class<?> domainClass = getDomainClass(genericType);
Unmarshaller u = getJAXBContext(domainClass, mediaType).createUnmarshaller();
u.setProperty("eclipselink.media-type", mediaType.toString());
u.setProperty("eclipselink.json.include-root", false);
return u.unmarshal(new StreamSource(entityStream), domainClass).getValue();
} catch(JAXBException jaxbException) {
throw new WebApplicationException(jaxbException);
}
}
public boolean isWriteable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
return true;
}
public void writeTo(Object object, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException,
WebApplicationException {
try {
Class<?> domainClass = getDomainClass(genericType);
Marshaller m = getJAXBContext(domainClass, mediaType).createMarshaller();
m.setProperty("eclipselink.media-type", mediaType.toString());
m.setProperty("eclipselink.json.include-root", false);
m.marshal(object, entityStream);
} catch(JAXBException jaxbException) {
throw new WebApplicationException(jaxbException);
}
}
public long getSize(Object t, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
return -1;
}
private JAXBContext getJAXBContext(Class<?> type, MediaType mediaType)
throws JAXBException {
ContextResolver<JAXBContext> resolver
= providers.getContextResolver(JAXBContext.class, mediaType);
JAXBContext jaxbContext;
if(null == resolver || null == (jaxbContext = resolver.getContext(type))) {
return JAXBContextFactory.createContext(new Class[] {type}, null);
} else {
return jaxbContext;
}
}
private Class<?> getDomainClass(Type genericType) {
if(genericType instanceof Class) {
return (Class<?>) genericType;
} else if(genericType instanceof ParameterizedType) {
return (Class<?>) ((ParameterizedType) genericType).getActualTypeArguments()[0];
} else {
return null;
}
}
}
import java.io.*;
导入java.lang.annotation.annotation;
导入java.lang.reflect.*;
导入javax.xml.transform.stream.StreamSource;
导入javax.ws.rs.*;
导入javax.ws.rs.core.*;
导入javax.ws.rs.ext.*;
导入javax.xml.bind.*;
导入org.eclipse.persistence.jaxb.JAXBContextFactory;
@提供者
@产生(MediaType.APPLICATION_JSON)
@使用(MediaType.APPLICATION_JSON)
公共类MOXyJSONProvider实现
MessageBodyReader,MessageBodyWriter{
@上下文
受保护的提供者;
公共布尔值可读取(类类型、类型genericType、,
注释[]注释,MediaType(MediaType){
返回true;
}
公共对象读取自(类类型、类型genericType、,
注释[]注释,MediaType MediaType,
多值映射HttpHeader,InputStream entityStream)
抛出IOException、WebApplicationException{
试一试{
Class domainClass=getDomainClass(genericType);
Unmarshaller u=getJAXBContext(domainClass,mediaType).createUnmarshaller();
u、 setProperty(“eclipselink.media type”,mediaType.toString());
u、 setProperty(“eclipselink.json.include root”,false);
返回u.unmarshal(新的StreamSource(entityStream),domainClass).getValue();
}捕获(JAXBEException JAXBEException){
抛出新的WebApplicationException(JAXBEException);
}
}
公共布尔值可写(类类型、类型genericType、,
注释[]注释,MediaType(MediaType){
返回true;
}
public void writeTo(对象、类类型、类型genericType、,
注释[]注释,MediaType MediaType,
多值MAP HttpHeader,
OutputStream entityStream)引发IOException,
WebApplicationException{
试一试{
Class domainClass=getDomainClass(genericType);
Marshaller m=getJAXBContext(domainClass,mediaType).createMarshaller();
m、 setProperty(“eclipselink.media type”,mediaType.toString());
m、 setProperty(“eclipselink.json.include root”,false);
m、 封送处理(对象,entityStream);
}捕获(JAXBEException JAXBEException){
抛出新的WebApplicationException(JAXBEException);
}
}
公共长getSize(对象t、类类型、类型genericType、,
注释[]注释,MediaType(MediaType){
返回-1;
}
私有JAXBContext getJAXBContext(类类型,MediaType MediaType)
抛出异常{
上下文解析器
=providers.getContextResolver(JAXBContext.class,mediaType);
JAXBContext JAXBContext;
if(null==resolver | | null==(jaxbContext=resolver.getContext(type))){
返回JAXBContextFactory.createContext(新类[]{type},null);
}否则{
返回jaxbContext;
}
}
私有类getDomainClass(类型genericType){
if(类的genericType实例){
返回(类)genericType;
}else if(参数化类型的genericType实例){
返回(类)((ParameterizedType)genericType.getActualTypeArguments()[0];
}否则{
返回null;
}
}
}
我已经试过了,但还是没能成功。您能否提供更多有关所需的任何其他代码更改的详细信息(上面列出的内容除外)。例如,我是否必须更改我的JAXBContextResolver类?我还很难下载此项目所需的依赖项。我试着看了看这份文件,但没有结果。下面是一个pom.xml示例:我下载了它,并使用JAXBProvider进行了尝试,但它不起作用。空的lastName是输出lastName:{“nil”:“true”}。我换成了Moxyjson