Return JSON from a RESTful service using CXF DOSGI

I have a simple service that is annotated with JAX-RS annotations and includes @Produces("application/json") annotation @Produces("application/json") . When registering the service, I set the following properties (I use DS, but it does not matter):

 service.exported.interfaces -> * service.exported.configs -> org.apache.cxf.rs org.apache.cxf.rs.address -> myURI 

When I launch my application, I can click the URL, but my browser returns:

 No message body writer has been found for response class MyClass. 

My OSGi console displays:

 Jan 11, 2012 2:29:48 PM org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor writeResponseErrorMessage WARNING: No message body writer has been found for response class MyClass. 

I read the documentation and thought maybe I needed to register a JSON provider. In the activation activator, I added:

 bundleContext.registerService(new String[] { "javax.ws.rs.ext.MessageBodyReader", "javax.ws.rs.ext.MessageBodyWriter" }, new org.apache.cxf.jaxrs.provider.JSONProvider(), null); 

but it didn’t matter.

How to fix "There is no message body writer for the MyClass response class." error message?

+6
source share
1 answer

No message writer means that your json provider does not understand how to marshal your class that you returned in JSON. If you use JSONProvider by default, you use Jackson, which uses JAXB annotations. In other words, the class you are returning must have @XmlRootElement annotation at the class level.

+1
source

Source: https://habr.com/ru/post/905775/


All Articles