Quantcast

RESTful webservice with CDI and JSON

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RESTful webservice with CDI and JSON

quagaar
I'm struggling for a few days now, to setup a simple REST service with CDI support, using annotations instead of XML configuration. I've finally managed to get something running using PAX-CDI (with Weld support) and CXF, but now I'm stuck trying to get my REST service produce JSON. Calling my service results in:

No message body writer has been found for class java.util.LinkedList, ContentType: application/json

My service class looks like this:

@Path("/service")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Singleton
public class MyRestService {

        @GET
        @Path("/test1")
        public String test1() {
                return "test1";
        }

        @GET
        @Path("/test2")
        public List<String> test2() {
                final List<String> res = new LinkedList<>();
                res.add("test2");
                return res;
        }
}

Calling the "test1" resource method works, but calling "test2" results in the error message described above.

I'm using Java 7, Karaf 3.0.3, PAX-CDI 0.11.0 and CXF 3.1.0.

Grateful for any help,

Q
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: RESTful webservice with CDI and JSON

quagaar
Hi guys.

I solved the problem. If anyone is interested, here is what I had to do to get it working:

Basically, I just had to register a provider handling the JSON input/output. After some back and forth, I came up with this:

@Provider
public class JsonProvider extends JacksonJsonProvider {
}

After adding the necessary bundle dependencies, everything worked as expexted.

For the sake of completeness, here the dependency list for the JacksonJsonProvider:

mvn:com.fasterxml.jackson.jaxrs/jackson-jaxrs-json-provider/2.5.3
mvn:com.fasterxml.jackson.jaxrs/jackson-jaxrs-base/2.5.3
mvn:com.fasterxml.jackson.core/jackson-core/2.5.3                
mvn:com.fasterxml.jackson.core/jackson-annotations/2.5.3        
mvn:com.fasterxml.jackson.core/jackson-databind/2.5.3            

Greetings,

Q
Loading...