Glassfish 4 REST XML 工作 JSON 错误

Glassfish 4 REST XML Working JSON Error

我在 Glassfish 4.1 上有一个 Web 服务,它在 XML 上正常工作,但在 JSON 上不能正常工作。

实体class是:

@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

解决方法如下:

  1. 停止 Glassfish

  2. 删除 Glassfish 模块目录中的所有 jackson 内容。

  3. 删除domains/domain1/osgi-cache/felix目录

  4. 将以下文件复制到该目录:

/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 模块版本。

我遇到了同样的问题,我为此苦苦挣扎了好几天。 问题是 glashfish 4.1.1 中缺少 jar 文件 ( jackson-module-jaxb-annotations )。您可以从此 link http://www.java2s.com/Code/Jar/j/Downloadjacksonmodulejaxbannotations212jar.htm 或 google download jackson module jaxb annotations 下载它。 然后将其包含在 C:\Program Files\glassfish-4.1.1\glassfish\modules 目录中。您可以通过删除版本号来重命名 jar 文件。重新启动 glassfish 并重新部署您的应用程序。这样对我有用

同样的问题,在我的例子中,我的 pom.xml 重新部署并准备好了“Jackson 模块:JAXB 注释”的 Maven 存储库。我不得不两次调用 http 我真的不知道为什么,但这在第二次调用中对我有用(GET/POST http 请求)。

希望这对其他人有帮助。