怎样给网站或者商品做推广,go语言做网站,房天下房官网,wordpress 升级 权限在以前的系列文章中#xff0c;我介绍了如何利用EclipseLink JAXB#xff08;MOXy#xff09;创建RESTful数据访问服务。 在本文中#xff0c;我将介绍在服务器端利用MOXy的新JSON绑定添加对基于JAXB映射的JSON消息的支持有多么容易。 MOXy作为您的JAX-RS JSON提供程序–服… 在以前的系列文章中我介绍了如何利用EclipseLink JAXBMOXy创建RESTful数据访问服务。 在本文中我将介绍在服务器端利用MOXy的新JSON绑定添加对基于JAXB映射的JSON消息的支持有多么容易。 MOXy作为您的JAX-RS JSON提供程序–服务器端 MOXy作为您的JAX-RS JSON提供程序–客户端 为什么选择EclipseLink JAXBMOXy 以下是将MOXy用作JSON绑定提供程序的一些优点 JSON绑定提供程序中对JAXB注释的最广泛支持。 支持XML和JSON 绑定到JSON和XML – Geocode示例 。 MOXy包含XmlInverseReference之类的扩展名用于将JPA实体映射到JSON和XML 第3部分–将JPA实体映射到XML使用JAXB 。 外部映射文档可作为注释的替代方法 JAX-RS Service中的MOXy的XML元数据 。 客户服务 使用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(unitNameCustomerService,typePersistenceContextType.TRANSACTION)EntityManager entityManager;POSTConsumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})public void create(Customer customer) {entityManager.persist(customer);}GETProduces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})Path({id})public Customer read(PathParam(id) long id) {return entityManager.find(Customer.class, id);}PUTConsumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})public void update(Customer customer) {entityManager.merge(customer);}DELETEPath({id})public void delete(PathParam(id) long id) {Customer customer read(id);if(null ! customer) {entityManager.remove(customer);}}GETProduces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})Path(findCustomersByCity/{city})public ListCustomer 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 implementsMessageBodyReaderObject, MessageBodyWriterObject{Contextprotected Providers providers;public boolean isReadable(Class? type, Type genericType,Annotation[] annotations, MediaType mediaType) {return true;}public Object readFrom(ClassObject type, Type genericType,Annotation[] annotations, MediaType mediaType,MultivaluedMapString, 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,MultivaluedMapString, 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 {ContextResolverJAXBContext 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 进一步阅读 如果您喜欢这篇文章那么您可能也会对以下内容感兴趣 RESTful服务 MOXy作为您的JAX-RS JSON提供程序–客户端 创建一个RESTful服务 第1部分–数据库 第2部分–将数据库映射到JPA实体 第3部分–将JPA实体映射到XML使用JAXB 第4部分– RESTful服务 第五部分–客户 JAX-RS服务中的MOXy的XML元数据 JSON绑定 使用EclipseLink MOXy进行JSON绑定– Twitter示例 绑定到JSON和XML –地理编码示例 应用服务器集成 GlassFish 3.1.2充满了MOXyEclipseLink JAXB EclipseLink MOXy是WebLogic Server 12c中的JAXB提供程序 参考 MOXy作为您的JAX-RS JSON提供程序–来自Java XML和JSON绑定博客的JCG合作伙伴 Blaise Doughan的服务器端 。 翻译自: https://www.javacodegeeks.com/2012/04/moxy-as-your-jax-rs-json-provider.html