How do I use the Jersey JSON POJO support?

前端 未结 11 1826
离开以前
离开以前 2020-11-27 12:24

I have an object that I\'d like to serve in JSON as a RESTful resource. I have Jersey\'s JSON POJO support turned on like so (in web.xml):

  
         


        
相关标签:
11条回答
  • 2020-11-27 13:02

    I followed the instructions here which show how to use Jersey and Jackson POJOs(as opposed to JAXB). It worked with Jersey 1.12 as well.

    0 讨论(0)
  • 2020-11-27 13:03

    The following worked for me. I'm using Jersey 2.7 with Jackson with Apache Felix (OSGi) running on Tomcat6.

    public class MyApplication extends ResourceConfig {
    
        public MyApplication() {
            super(JacksonFeature.class);
            // point to packages containing your resources
            packages(getClass().getPackage().getName());
        }
    }
    

    And then, in your web.xml (or in my case, just a Hashtable), you would specify your javax.ws.rs.Application like so

    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value><MyApplication.class.getName()></param-value>
    </init-param>
    

    No need to specify com.sun.jersey.config.property.pacakges or com.sun.jersey.api.json.POJOMappingFeature

    Just make sure you specify dependency on

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.7</version>
    </dependency>
    
    0 讨论(0)
  • 2020-11-27 13:07

    This worked for in Jersey 2.30:

    In pom.xml:

    <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-json-jackson</artifactId>
      <version>2.30</version>
    </dependency>
    

    In WEB-INF/web.xml:

     <servlet>
         <servlet-name>My Jersey REST Service</servlet-name>
         <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
         <!-- ATTENTION: Use Jackson for JSON: -->
         <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.jackson.JacksonFeature</param-value>
         </init-param>
         <!-- ... -->
         <init-param>
             <param-name>jersey.config.server.provider.packages</param-name>
             <param-value>com.example.myapp</param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
    </servlet>
    
    0 讨论(0)
  • 2020-11-27 13:11

    This did it for me - Jersey 2.3.1

    In the web.xml file :

    <servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value><my webapp packages>;org.codehaus.jackson.jaxrs</param-value>
    </init-param>
    </servlet>
    

    In the pom.xml file :

    <dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.3.1</version>
    </dependency>
    
    0 讨论(0)
  • 2020-11-27 13:13

    You can use @XmlRootElement if you want to use JAXB annotations (see other answers).

    However, if you prefer pure POJO mapping, you must do the following (Unfortunately it isn't written in docs):

    1. Add jackson*.jar to your classpath (As stated by @Vitali Bichov);
    2. In web.xml, if you're using com.sun.jersey.config.property.packages init parameter, add org.codehaus.jackson.jaxrs to the list. This will include JSON providers in the scan list of Jersey.
    0 讨论(0)
提交回复
热议问题