I have a web service on Glassfish 4.1 that is working properly for XML but not for JSON.
The entity class is:
@XmlRootElement
public class Person implements Serializable {
/**
*
*/
private static final long serialVersionUID = -8969081094076790550L;
Integer id;
String firstName;
String lastName;
String employeeId;
/**
*
*/
public Person() {
}
@Override
public String toString() {
return firstName + " " + lastName + " [" + employeeId + "] [id: " + id + "]";
}
/**
* @return the firstName
*/
public String getFirstName() {
return this.firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return this.lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the employeeId
*/
public String getEmployeeId() {
return this.employeeId;
}
/**
* @param employeeId the employeeId to set
*/
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
/**
* @return the id
*/
public Integer getId() {
return this.id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
}
The relevant service code is:
@GET
@Path("xml")
@Produces(MediaType.TEXT_XML)
public Collection<Person> getPeopleXML() {
return personDao.getAllPeople();
}
@GET
@Path("json")
@Produces(MediaType.APPLICATION_JSON)
public Collection<Person> getPeopleJSON() {
return personDao.getAllPeople();
}
The XML call works, and I get:
<people>
<person>
<employeeId>2234</employeeId>
<firstName>Mike</firstName>
<id>2</id>
<lastName>Jones</lastName>
</person>
<person>
<employeeId>22314</employeeId>
<firstName>Joe</firstName>
<id>4</id>
<lastName>Smith</lastName>
</person>
</people>
I get an error with the JSON call:
HTTP Status 500 - Internal Server Error
type Exception report
messageInternal Server Error
descriptionThe server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: com/fasterxml/jackson/module/jaxb/JaxbAnnotationIntrospector root cause
org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: com/fasterxml/jackson/module/jaxb/JaxbAnnotationIntrospector root cause
java.lang.NoClassDefFoundError: com/fasterxml/jackson/module/jaxb/JaxbAnnotationIntrospector root cause
java.lang.ClassNotFoundException: com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector not found by com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider [129] note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1 logs.
Why the error? I have everything I need, I think. I have an ivy file and tried adding all kinds of jackson deps, but nothing seems to work. I cannot tell if I am having version problems when I add jackson to my ivy file since it's included with Glassfish, or what.
Glassfish provides the following:
/d/glassfish4/glassfish/modules/jackson-annotations.jar
/d/glassfish4/glassfish/modules/jackson-core.jar
/d/glassfish4/glassfish/modules/jackson-databind.jar
/d/glassfish4/glassfish/modules/jackson-jaxrs-base.jar
/d/glassfish4/glassfish/modules/jackson-jaxrs-json-provider.jar
/d/glassfish4/glassfish/modules/jersey-media-json-jackson.jar
Here is the solution:
Stop Glassfish
Delete all of the jackson stuff in the modules directory for Glassfish.
Delete the domains/domain1/osgi-cache/felix directory
Copy the following files to that directory:
/glassfish/modules/jackson-annotations-2.4.0.jar
/glassfish/modules/jackson-annotations-2.5.0.jar
/glassfish/modules/jackson-core-2.4.2.jar
/glassfish/modules/jackson-core-2.5.4.jar
/glassfish/modules/jackson-databind-2.4.2.jar
/glassfish/modules/jackson-databind-2.5.4.jar
/glassfish/modules/jackson-jaxrs-base-2.5.4.jar
/glassfish/modules/jackson-jaxrs-json-provider-2.5.4.jar
/glassfish/modules/jackson-module-jaxb-annotations-2.4.2.jar
/glassfish/modules/jackson-module-jaxb-annotations-2.5.4.jar
/glassfish/modules/jersey-media-json-jackson.jar
Then it works. Not sure what version the original files for Jackson were, but this works and upgrades your jackson module versions.
I had same problem in which i battled with it for days. The problem is a missing jar file ( jackson-module-jaxb-annotations ) in glashfish 4.1.1. You can download it from this link http://www.java2s.com/Code/Jar/j/Downloadjacksonmodulejaxbannotations212jar.htm or google download jackson module jaxb annotations. Then include it in C:\Program Files\glassfish-4.1.1\glassfish\modules directory. you can rename the jar file by removing the version number. Restart glassfish and redeploy your app. It works for me that way
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With