我在 Glassfish 4.1 上有一个 Web 服务,它对 XML 正常工作,但对 JSON 不正常。
实体类是:
@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;
}
}
相关服务代码为:
@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();
}
XML 调用有效,我得到:
<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>
我在 JSON 调用中遇到错误:
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.
为什么会出错?我认为我拥有我需要的一切。我有一个 ivy 文件并尝试添加各种 jackson deps,但似乎没有任何效果。当我将 jackson 添加到我的 ivy 文件时,我无法判断我是否遇到版本问题,因为它包含在 Glassfish 中,或者什么。
Glassfish 提供以下内容:
/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
最佳答案
解决方法如下:
停止 Glassfish
删除 Glassfish 模块目录中的所有 jackson 内容。
删除domains/domain1/osgi-cache/felix目录
将以下文件复制到该目录:
/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
然后就可以了。不确定 Jackson 的原始文件是什么版本,但这可以工作并升级您的 jackson 模块版本。
关于java - Glassfish 4 REST XML 工作 JSON 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31275480/