mik*_*keb 5 java xml json glassfish jaxb
我在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;
}
}
Run Code Online (Sandbox Code Playgroud)
相关服务代码是:
@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();
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我在JSON调用时遇到错误:
HTTP状态500 - 内部服务器错误
类型异常报告
messageInternal服务器错误
description服务器遇到内部错误,导致无法完成此请求.
例外
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.jaxrs.jackson-jaxrs-json-provider [129]找不到com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector注意异常的完整堆栈跟踪及其根本原因可在GlassFish Server Open Source Edition 4.1日志中找到.
为什么错误?我想,我有我需要的一切.我有一个常春藤文件,并尝试添加各种杰克逊deps,但似乎没有任何工作.当我将jackson添加到我的常春藤文件中时,我无法判断我是否遇到版本问题,因为它包含在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
Run Code Online (Sandbox Code Playgroud)
mik*_*keb 11
这是解决方案:
停止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模块版本.