Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

RESTEASY-795

Add Jackson JAXB support to RESTEasy
  • Loading branch information...
commit 4133b36b8e6cd122bacabeb4faa40434acfe78ea 1 parent 97d793c
@liweinan liweinan authored
View
9 ...n/src/main/java/org/jboss/resteasy/plugins/providers/jackson/ResteasyJacksonProvider.java
@@ -1,5 +1,6 @@
package org.jboss.resteasy.plugins.providers.jackson;
+import org.codehaus.jackson.jaxrs.Annotations;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.jboss.resteasy.annotations.providers.NoJackson;
import org.jboss.resteasy.util.FindAnnotation;
@@ -22,6 +23,14 @@
@Produces({"application/*+json", "text/json"})
public class ResteasyJacksonProvider extends JacksonJsonProvider
{
+
+ public ResteasyJacksonProvider() {
+ super();
+ Annotations[] ANNOTATIONS = {Annotations.JACKSON, Annotations.JAXB};
+ _mapperConfig.setAnnotationsToUse(ANNOTATIONS);
+ }
+
+
@Override
public boolean isReadable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType)
{
View
71 ...roviders/jackson/src/test/java/org/jboss/resteasy/test/providers/jackson/JacksonTest.java
@@ -1,5 +1,8 @@
package org.jboss.resteasy.test.providers.jackson;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
import org.jboss.resteasy.annotations.providers.NoJackson;
import org.jboss.resteasy.annotations.providers.jaxb.json.BadgerFish;
import org.jboss.resteasy.client.ClientRequest;
@@ -7,20 +10,13 @@
import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.test.BaseResourceTest;
import org.junit.Assert;
-import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
-import javax.ws.rs.Consumes;
-import javax.ws.rs.GET;
-import javax.ws.rs.POST;
-import javax.ws.rs.Path;
-import javax.ws.rs.PathParam;
-import javax.ws.rs.Produces;
-import javax.xml.bind.annotation.XmlAccessType;
-import javax.xml.bind.annotation.XmlAccessorType;
-import javax.xml.bind.annotation.XmlAttribute;
-import javax.xml.bind.annotation.XmlRootElement;
+import javax.ws.rs.*;
+import javax.xml.bind.annotation.*;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
import static org.jboss.resteasy.test.TestPortProvider.generateBaseUrl;
import static org.jboss.resteasy.test.TestPortProvider.generateURL;
@@ -193,6 +189,7 @@ public static void setUp() throws Exception
{
dispatcher.getRegistry().addPerRequestResource(JacksonService.class);
dispatcher.getRegistry().addPerRequestResource(XmlService.class);
+ dispatcher.getRegistry().addPerRequestResource(JAXBService.class);
}
@Test
@@ -259,6 +256,58 @@ public void testJackson() throws Exception
}
+
+ @XmlRootElement
+ public static class XmlResourceWithJAXB {
+ String attr1;
+ String attr2;
+
+ @XmlElement(name = "attr_1")
+ public String getAttr1() {
+ return attr1;
+ }
+
+ public void setAttr1(String attr1) {
+ this.attr1 = attr1;
+ }
+
+ @XmlElement
+ public String getAttr2() {
+ return attr2;
+ }
+
+ public void setAttr2(String attr2) {
+ this.attr2 = attr2;
+ }
+ }
+
+ @Path("/jaxb")
+ public static class JAXBService
+ {
+
+ @GET
+ @Produces("application/json")
+ public XmlResourceWithJAXB getJAXBResource() {
+ XmlResourceWithJAXB resourceWithJAXB = new XmlResourceWithJAXB();
+ resourceWithJAXB.setAttr1("XXX");
+ resourceWithJAXB.setAttr2("YYY");
+ return resourceWithJAXB;
+ }
+
+ }
+
+
+ @Test
+ public void testJacksonJAXB() throws Exception
+ {
+ DefaultHttpClient client = new DefaultHttpClient();
+ HttpGet get = new HttpGet(generateBaseUrl() + "/jaxb");
+ HttpResponse response = client.execute(get);
+ BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
+ Assert.assertTrue(reader.readLine().contains("attr_1"));
+ }
+
+
@Test
public void testJacksonProxy() throws Exception
{
Please sign in to comment.
Something went wrong with that request. Please try again.