栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用JAXB在Jersey RESTful API的JSON输出中包括空元素

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用JAXB在Jersey RESTful API的JSON输出中包括空元素

对于 Eclipselink JAXB(MOXy)
的JSON绑定,正确的映射如下。您可以与您的提供商一起尝试,以查看它是否也可以工作:

@XmlRootElementpublic class Data {    @XmlElement(nillable=true)    public String firstName;    @XmlElement(nillable=true)    public String lastName;}

想要查询更多的信息

  • http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html
  • http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html

更新2

Eclipselink 2.4包含的

MOXyJsonProvider
MessageBodyReader
/
的实现
MessageBodyWriter
,您可以直接使用它来利用MOXy的JSON绑定

  • http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html

更新1

以下

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;        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/570882.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号