Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning JSON from a RESTful service using CXF DOSGI

Tags:

jax-rs

cxf

dosgi

I have a simple service which is annotated with JAX-RS annotations and includes the @Produces("application/json") annotation. I have set up the following properties when I register the service (I am using DS but that shouldn't matter):

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

When I run my application I can hit 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 may 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 this has not made any difference.

How do I fix the "No message body writer has been found for response class MyClass." error message?

like image 240
rancidfishbreath Avatar asked Jan 11 '12 23:01

rancidfishbreath


1 Answers

No message body writer means that your json provider does not understand how to marshal your class that you returned into JSON. If you are using the default JSONProvider, then you are using Jackson, which uses JAXB annotations. In other words, the class that you return should have a @XmlRootElement annotation on the class level.

like image 186
Jeff Wang Avatar answered Oct 01 '22 03:10

Jeff Wang