Tomee 1.5.2 JAX-RS with Jackson 1.9.12

I have been trying for many years to get Tomee 1.5.2 JAX-RS to work with Jackson. It seems I tried 100 ways.
Here is my last attempt:

I added the following to conf / system.properties:

openejb.cxf.jax-rs.providers = org.codehaus.jackson.jaxrs.JacksonJsonProvider, org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider 


I added to the Tomee lib folder:

  • jackson-mapper-asl-1.9.12.jar
  • JACKSON-core-ASL-1.9.12.jar
  • JACKSON-jaxrs-1.9.12.jar


I have a simple JAX-RS class in a pure NetBeans Maven web project. Mirc is a POJO with a name and a car.

 ...imports @Path("") public class MyJson { @GET @Produces(APPLICATION_JSON) public Object myMeth() { return new Mirc("Peter", "BMW"); } 

I keep getting "No message body writer found for myclass response class."
What am I missing? How can I make it work? I checked all posts in stackoverflow with no success.
I would really appreciate help. Thanks.

+4
source share
5 answers

I finally figured out how to do this, thanks to the great support of Tome. So ... there it is!

I'll start by explaining how this can be achieved in the latest version of the Tomee 1.6 JAX-RS , which will be released soon as stable. By the way, he is very stable, even now.

Suppose you have a Maven Java EE 6 web application project (use NetBeans to create it), here are the steps:
1. Add Jackson dependency to pom.xml

 <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>1.9.13</version> </dependency> 

2. Create openejb-jar.xml in WEB-INF (folder with web.xml) containing:

 <openejb-jar xmlns="http://www.openejb.org/openejb-jar/1.1"> <pojo-deployment class-name="jaxrs-application"> <properties> cxf.jaxrs.providers = org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider </properties> </pojo-deployment> </openejb-jar> 

For more information about this config, see 1 and 2
Edit from @rmannibucau : if you are using a custom jaxrs Application subclass (for example, with @ApplicationPath), you will set the qualified name of this class instead of "jaxrs-application" (which means the default application).
3. Create a JAX-RS resource that will not work without Jackson (example: regular list):

 import java.util.Arrays; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/jackson") public class Resource { @GET @Produces({MediaType.APPLICATION_JSON}) public Object sayHelloJson() { return Arrays.asList(new String[]{"Peter", "pan", "Ihihi"}); } } 

4. Deploy JAX-RS on Tomee 1.6.0 and run the application at: http://localhost:8080/yourAppPath/jackson This guide has been tested with version 1.6.0 2013.10.24 on NetBeans 7.4.

If you want to use the latest Jackson, replace the previous dependency as follows:

 <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-json-provider</artifactId> <version>2.2.3</version> </dependency> 

and change the openjb-jar.xml to contain:

 cxf.jaxrs.providers = com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider 

Tomee 1.5.2
For this version, providers must be specified for each resource, therefore, not at the application level, as in 1.6.0. More information can be found here .

+9
source

you can use this code:

 ...imports @Path("") public class MyJson { @GET @Produces(APPLICATION_JSON) public String myMeth() { ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(new Mirc("Peter", "BMW")); } 

is this a solution to your problem?

you can use mapper.readValue() to convert String to Object

+1
source

I had to add:

 <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-xc</artifactId> <version>1.9.13</version> </dependency> 

To room I got

  java.lang.ClassNotFoundException: org.codehaus.jackson.xc.JaxbAnnotationIntrospector 

otherwise.

+1
source

I use openejb separately from TomcatEE and wanted to use Jackson globally for all JSon requests.

Adding this property to the original context at server startup did the trick. Sharing because it was hard to track, maybe it can help someone else.

 properties.setProperty("cxf.jaxrs.providers", "com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider"); 
+1
source

I noticed that tomee 1.6.0 had gson-2.1 in its lib directory. Thus, we can use gson instead of jackson and not depend on 6 Jackson-related cans (which come with jackson-jaxrs-json-provider). GsonProvider (instead of JacksonJaxbJsonProvider) can be found here here . I tested with a very simple example and it worked correctly.

0
source

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


All Articles