问题
I am trying to return a JSON object from a jax-rs but I get an internal server error whenever I try to access.
This is my method, I am using javax.ws.rs.GET
.
@GET
@Produces("application/json")
public Testy getJson() {
return new Testy("hello");
}
This is the class:
public class Testy {
private final String value;
public Testy(String value){
this.value = value;
}
public String getValue(){
return value;
}
}
My Pom.xml has this dependency, I have tried various dependencies, but none work. There are various maven resources for jersey, there's jersey-client jersey-core.
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.3.3</version>
</dependency>
I am using Glassfish 4.
Questions about working with Jersey:
I have seen some places where they mention you need to have initialize POJO support, it seems like its for jersey 1.* but I am not sure. I don't need this if I am using 2.* ?
Do I have to modify the web.xml to point to the jersey servlet ?
How can I produce and consume JSON objects using POJO classes !?
Edit
Here is my auto generated config class.

回答1:
I had to add the JacksonFeature resource to my ApplicationConfig Class.
@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
addRestResourceClasses(resources);
resources.add(org.glassfish.jersey.jackson.JacksonFeature.class);
return resources;
}
/**
* Do not modify addRestResourceClasses() method.
* It is automatically populated with
* all resources defined in the project.
* If required, comment out calling this method in getClasses().
*/
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(com.wfscorp.restapitwo.GenericResource.class);
}
}
Then everything worked!
Note
When I used the com.fasterxml.jackson.jaxrs dependency I was unable to deploy my application. I started getting a WELD-001408 Unsatisfied dependencies for type
error so I had to exclude the jersey media multi part.
These are the dependencies in my pom.xml
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.13</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.7</version>
<scope>provided</scope>
</dependency>
</dependencies>
回答2:
This will guide through the entire process Reference ,Simple and Easy - http://examples.javacodegeeks.com/enterprise-java/rest/jersey/json-example-with-jersey-jackson/
回答3:
It may be that you need to expose the value property of your Testy object with a getter.
来源:https://stackoverflow.com/questions/26849356/how-to-return-json-objects-from-java-rest-api