此内容没有您所选择的语言版本。

Chapter 19. JSON Support via Jackson


Apart from the Jettison JAXB adapter for JSON, RESTEasy also supports integration with the Jackson project. Many users find Jackson's output format more intuitive than the format provided by either BadgerFish or Jettison.
Jackson is available from http://jackson.codehaus.org. It lets you easily marshal Java objects to and from JSON. Jackson has a JavaBean-based model and JAXB-like APIs. RESTEasy integrates with the JavaBean model as described in the Jackson Tutorial.
To include Jackson in your project, add the following Maven dependency to your build:
    <repository>
       <id>jboss</id>
       <url>http://repository.jboss.org/maven2</url>
    </repository>

       ...
    <dependency>
       <groupId>org.jboss.resteasy</groupId>
       <artifactId>resteasy-jackson-provider</artifactId>
       <version>1.1.GA</version>
    </dependency>
Copy to Clipboard Toggle word wrap
RESTEasy expands the JAX-RS integration built into Jackson in several ways. The first expansion provided support for application/*+json. Previously, Jackson accepted only application/json and text/json as valid media types. application/*+json support lets you marshal your JSON-based media types with Jackson. For example:
@Path("/customers")
public class MyService {

   @GET
   @Produces("application/vnd.customer+json")
   public Customer[] getCustomers() {}
}
Copy to Clipboard Toggle word wrap
Using RESTEasy JAXB providers alongside Jackson is also problematic. Rather than use Jackson to output your JSON, you can use Jettison and JAXB. To do so, you must either not install the Jackson provider, or use the @org.jboss.resteasy.annotations.providers.NoJackson annotation on your JAXB annotated classes, like so:
    @XmlRootElement
    @NoJackson
    public class Customer {...}

    @Path("/customers")
    public class MyService {

       @GET
       @Produces("application/vnd.customer+json")
       public Customer[] getCustomers() {}
    }
Copy to Clipboard Toggle word wrap
If you cannot annotate the JAXB class with @NoJackson, tehn you can annotate a method parameter instead:
        @XmlRootElement
        public class Customer {...}

        @Path("/customers")
        public class MyService {

           @GET
           @Produces("application/vnd.customer+json")
           @NoJackson
           public Customer[] getCustomers() {}

           @POST
           @Consumes("application/vnd.customer+json")
           public void createCustomer(@NoJackson Customer[] customers) {...}
        }
Copy to Clipboard Toggle word wrap

19.1. Possible Conflict With JAXB Provider

If your Jackson classes are annotated with JAXB annotations and the resteasy-jaxb-provider is on your classpath, you can trigger the Jettison JAXB marshalling code. To disable the JAXB JSON Marshaller, annotate your classes with @org.jboss.resteasy.annotations.providers.jaxb.IgnoreMediaTypes("application/*+json").
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat