How do I use the Jersey JSON POJO support?

2019-01-05 10:39发布

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):

<servlet>  
    <servlet-name>Jersey Web Application</servlet-name>  
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>  
</servlet>  

But when I try to access the resource, I get this exception:

SEVERE: A message body writer for Java type, class com.example.MyDto, and MIME media type, application/json, was not found
SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException
...

The class that I'm trying to serve isn't complicated, all it's got are some public final fields and a constructor that sets all of them. The fields are all strings, primitives, classes similar to this one, or Lists thereof (I've tried using plain Lists instead of generic List<T>s, to no avail). Does anyone know what gives? Thanks!

Java EE 6

Jersey 1.1.5

GlassFish 3.0.1

10条回答
在下西门庆
2楼-- · 2019-01-05 10:42

I'm new to this but I was able to use POJOs after adding the jackson-all-1.9.0.jar to the classpath.

查看更多
Animai°情兽
3楼-- · 2019-01-05 10:43

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>
查看更多
祖国的老花朵
4楼-- · 2019-01-05 10:46

Moving jersey-json dependency to the top of the pom.xml solved this problem for me.

<dependencies>
  <dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.18.1</version>
  </dependency>

  <!-- other dependencies -->

</dependencies>
查看更多
淡お忘
5楼-- · 2019-01-05 10:52

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.
查看更多
Melony?
6楼-- · 2019-01-05 10:53

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.

查看更多
姐就是有狂的资本
7楼-- · 2019-01-05 10:55

Jersey 2.0 provides support for JSON using MOXy an Jackson.

MOXy support is enabled by default if the JAR exists in the classpath and Jackson support can be enabled using a Feature. This is all explained in detail in Jersey 2.0 User Guide chapter on JSON Binding:

https://jersey.java.net/documentation/latest/media.html#json

To add MOXy support without the need for configuration add the following dependency to you maven pom.xml

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>2.6</version>
</dependency>
查看更多
登录 后发表回答