I'm on my way to migrate to TomEE and I was doing some Restful services, I needed to serialize POJOs as JSON, but as you know sometimes the standard JAXB is not enough for this kind of work, furthermore, I have a good experience with Gson and wanted to write a MessageBodyWriter / MessagedBodyReader in order to serialize my objects using this library
Basically I read this article http://www.ibm.com/developerworks/library/wa-jaxrs and write this: @Provider @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public class GsonJsonProvider<T> implements MessageBodyReader<T>, MessageBodyWriter<T> { private Gson gson; public GsonJsonProvider() { gson = new Gson(); } @Override public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return true; } @Override public T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { InputStreamReader streamReader = new InputStreamReader(entityStream, StandardCharsets.UTF_8); return gson.fromJson(streamReader, type); } @Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return true; } @Override public long getSize(T t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return -1; } @Override public void writeTo(T t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { System.out.println("Hey I'm trying to write" + t); OutputStreamWriter writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8); gson.toJson(t, writer); } } ..And the Application class @ApplicationPath("web") public class CustomersApplication extends Application{ @Override public Set<Class<?>> getClasses() { Set<Class<?>> set = new HashSet<>(); set.add(GsonJsonProvider.class); set.add(CustomersResource.class); return set; } } a Resource class @Path("/customers") @Produces(MediaType.APPLICATION_JSON) public class CustomersResource { private CustomersDao customersDao = new CustomersDao(); @GET @Path("{id}") public Customers buscarCliente(@PathParam("id") String id) { Customers cust = customersDao.findById(id); return cust; } } And switch to the browser http://localhost:8090/ModeloAplicacionSimpleAjaxRest/web/customers/ALFKI And no result, just no result at all. This is a bit strange because according to the standard implementation JAX-RS the previous should work, but it doesn't. Here my log oct 19, 2014 8:03:33 PM org.apache.openejb.config.ConfigurationFactory configureApplication INFORMACIÓN: Configuring enterprise application: D:\Documentos de Nestor\NetBeansProjects\ModeloAplicacionSimpleAjaxRest\build\web oct 19, 2014 8:03:34 PM org.apache.openejb.config.AppInfoBuilder build INFORMACIÓN: Enterprise application "D:\Documentos de Nestor\NetBeansProjects\ModeloAplicacionSimpleAjaxRest\build\web" loaded. oct 19, 2014 8:03:34 PM org.apache.openejb.assembler.classic.Assembler createApplication INFORMACIÓN: Assembling app: D:\Documentos de Nestor\NetBeansProjects\ModeloAplicacionSimpleAjaxRest\build\web oct 19, 2014 8:03:34 PM org.apache.openejb.cdi.CdiBuilder initSingleton INFORMACIÓN: Existing thread singleton service in SystemInstance(): org.apache.openejb.cdi.ThreadSingletonServiceImpl@31206beb oct 19, 2014 8:03:34 PM org.apache.openejb.cdi.OpenEJBLifecycle startApplication INFORMACIÓN: OpenWebBeans Container is starting... oct 19, 2014 8:03:34 PM org.apache.webbeans.plugins.PluginLoader startUp INFORMACIÓN: Adding OpenWebBeansPlugin : [CdiPlugin] oct 19, 2014 8:03:34 PM org.apache.webbeans.plugins.PluginLoader startUp INFORMACIÓN: Adding OpenWebBeansPlugin : [OpenWebBeansJsfPlugin] oct 19, 2014 8:03:34 PM org.apache.webbeans.config.BeansDeployer validateInjectionPoints INFORMACIÓN: All injection points were validated successfully. oct 19, 2014 8:03:34 PM org.apache.openejb.cdi.OpenEJBLifecycle startApplication INFORMACIÓN: OpenWebBeans Container has started, it took 6 ms. oct 19, 2014 8:03:34 PM org.apache.tomee.catalina.TomcatWebAppBuilder deployWebApps INFORMACIÓN: using context file D:\Documentos de Nestor\NetBeansProjects\ModeloAplicacionSimpleAjaxRest\build\web\META-INF\context.xml oct 19, 2014 8:03:34 PM org.apache.openejb.assembler.classic.Assembler createApplication INFORMACIÓN: Deployed Application(path=D:\Documentos de Nestor\NetBeansProjects\ModeloAplicacionSimpleAjaxRest\build\web) oct 19, 2014 8:03:34 PM org.bibeault.frontman.CommandBroker init INFORMACIÓN: Done initializing. oct 19, 2014 8:03:34 PM org.apache.openejb.server.cxf.rs.CxfRsHttpListener configureFactory INFORMACIÓN: Using providers: oct 19, 2014 8:03:34 PM org.apache.openejb.server.cxf.rs.CxfRsHttpListener configureFactory INFORMACIÓN: pe.dmsolutions.support.ViewableMessageBodyWriter@6199a810 oct 19, 2014 8:03:34 PM org.apache.openejb.server.cxf.rs.CxfRsHttpListener configureFactory INFORMACIÓN: pe.dmsolutions.support.GsonJsonProvider@7002cac8 oct 19, 2014 8:03:34 PM org.apache.openejb.server.cxf.rs.CxfRsHttpListener configureFactory INFORMACIÓN: org.apache.openejb.server.cxf.rs.EJBAccessExceptionMapper@40116dea oct 19, 2014 8:03:34 PM org.apache.openejb.server.cxf.rs.CxfRsHttpListener logEndpoints INFORMACIÓN: REST Application: http://localhost:8090/ModeloAplicacionSimpleAjaxRest/web -> pe.dmsolutions.support.CustomersApplication oct 19, 2014 8:03:34 PM org.apache.openejb.server.cxf.rs.CxfRsHttpListener logEndpoints INFORMACIÓN: Service URI: http://localhost:8090/ModeloAplicacionSimpleAjaxRest/web/customers -> Pojo pe.dmsolutions.resource.CustomersResource oct 19, 2014 8:03:34 PM org.apache.openejb.server.cxf.rs.CxfRsHttpListener logEndpoints INFORMACIÓN: GET http://localhost:8090/ModeloAplicacionSimpleAjaxRest/web/customers/{id} -> Customers buscarCliente(String) oct 19, 2014 8:03:34 PM org.apache.catalina.core.StandardContext reload Hey I'm trying to write{"customerID":"ALFKI","companyName":"Alfreds Futterkiste","contactName":"Maria Anders","contactTitle":"Sales Representative","address":"Obere Str. 57","city":"Berlin","postalCode":"12209","country":"Germany","phone":"030-0074321","fax":"030-0076545"} thank you so much! -- View this message in context: http://tomee-openejb.979440.n4.nabble.com/TomEE-JAX-RS-MessageBodyWriter-Reader-not-working-like-the-standard-tp4672454.html Sent from the TomEE Users mailing list archive at Nabble.com.
