MOXy作为您的JAX-RS JSON提供程序–服务器端

在以前的系列文章中,我介绍了如何利用EclipseLink JAXB(MOXy)创建RESTful数据访问服务。 在本文中,我将介绍在服务器端利用MOXy的新JSON绑定添加对基于JAXB映射的JSON消息的支持有多么容易。

为什么选择EclipseLink JAXB(MOXy)?

以下是将MOXy用作JSON绑定提供程序的一些优点:

客户服务

使用@Produces和@Consumes批注控制JAX-RS服务理解的消息类型。 在这篇文章中,我指定了除“ application / xml”外,所有操作现在都支持“ application / json”。 以下帖子提供了对该服务的更详细描述: 创建RESTful Web服务–第4/5部分

package org.example;
  
import java.util.List;
import javax.ejb.*;
import javax.persistence.*;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
  
@Stateless
@LocalBean
@Path("/customers")
public class CustomerService {
  
    @PersistenceContext(unitName="CustomerService",
                        type=PersistenceContextType.TRANSACTION)
    EntityManager entityManager;
  
    @POST
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public void create(Customer customer) {
        entityManager.persist(customer);
    }
  
    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    @Path("{id}")
    public Customer read(@PathParam("id") long id) {
        return entityManager.find(Customer.class, id);
    }
  
    @PUT
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public void update(Customer customer) {
        entityManager.merge(customer);
    }
  
    @DELETE
    @Path("{id}")
    public void delete(@PathParam("id") long id) {
        Customer customer = read(id);
        if(null != customer) {
            entityManager.remove(customer);
        }
    }
  
    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    @Path("findCustomersByCity/{city}")
    public List<Customer> findCustomersByCity(@PathParam("city") String city) {
        Query query = entityManager.createNamedQuery("findCustomersByCity");
        query.setParameter("city", city);
        return query.getResultList();
    }
  
}

MOXyJSONProvider

我们将实现JAX-RS MessageBodyReader / MessageBodyWriter,以插件支持MOXy的JSON绑定。 此实现足够通用,可以直接使用MOXy作为JAXB提供程序为任何JAX-RS服务启用JSON绑定。 一些有趣的注意事项:

  • MOXy没有编译时间依赖性。
  • eclipselink.media-type属性用于在unmarshaller(第34行)和marshaller(第55行)上启用JSON绑定。
  • eclipselink.json.include-root属性用于指示@XmlRootElement批注在JSON绑定中应被忽略(第35和56行)。
  • 创建JAXBContext时,代码首先检查以查看是否已为此类型注册JAXBContext(第70和71行)。 如果要利用MOXy的外部映射文档: JAX-RS Service中的MOXy的XML元数据,这将很有用。
package org.example;
 
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.*;
 
@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 JAXBContext.newInstance(type);
        } 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;
        }
    }
 
}

服务器设置

如果将GlassFish用作应用程序服务器,则需要使用EclipseLink 2.4安装中的对应软件包替换以下EclipseLink软件包。

  • org.eclipse.persistence.antlr.jar
  • org.eclipse.persistence.asm.jar
  • org.eclipse.persistence.core.jar
  • org.eclipse.persistence.jpa.jar
  • org.eclipse.persistence.jpa-modelgen.jar
  • org.eclipse.persistence.moxy.jar
  • org.eclipse.persistence.oracle.jar

进一步阅读

如果您喜欢这篇文章,那么您可能也会对以下内容感兴趣:

参考: MOXy作为您的JAX-RS JSON提供程序–来自Java XML和JSON绑定博客的JCG合作伙伴 Blaise Doughan的服务器端


翻译自: https://www.javacodegeeks.com/2012/04/moxy-as-your-jax-rs-json-provider.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值