【问题标题】:MOXy exceptions in JavaEE Jersey 2.0 projectJavaEE Jersey 2.0 项目中的 MOXy 异常
【发布时间】:2016-01-24 00:17:49
【问题描述】:

我正在尝试在 JavaEE 项目中实现 Json 支持,但在生成 MOXy 相关异常时遇到了问题。我在 jersey.java.net 上读到 MOXy 应该是可自动发现的,但是当我尝试时它似乎不起作用。

所以为了便于查明,我刚刚生成了一个新的 'jersey-quickstart-webapp' 项目并将 MyResource 更改如下(我的目标是使用应用程序class 而不是 web.xml,但这是 och 查明它的最简单方法。无论如何都会发生错误)。

@Path("myresource")
public class MyResource {
        @GET
        @Produces(MediaType.APPLICATION_JSON)
        public Response getIt() {
            return Response.status(Response.Status.ACCEPTED).entity(new TestEntity()).build();
        }
    }

TestEntity 类(在同一个包中):

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class TestEntity {
    private String content = "SOME CONTENT";

    public String getContent() {
        return content;
    }
}

POM.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>a.b.c</groupId>
    <artifactId>server</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>server</name>

    <build>
        <finalName>server</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
    </dependencies>
    <properties>
        <jersey.version>2.22.1</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

我使用 IntelliJ 将它部署在干净的 Glassfish 4.1.1 上。 在此之后我收到

java.lang.ClassNotFoundException: 找不到 javax.xml.parsers.ParserConfigurationException org.eclipse.persistence.moxy

所以我添加 beans.xml 如下(尝试为空以及我在 Oracle 文档中看到的)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       bean-discovery-mode="all">
</beans>

我在部署时收到此错误

java.lang.NoClassDefFoundError: 无法初始化类 org.eclipse.persistence.jaxb.BeanValidationHelper

为了好玩,我尝试删除 web.xml,将依赖项 jersey-container-servlet-core 更改为 jersey-container- servlet 并改为创建一个 Application 类(如 ContainerRequestFilter wont run in JavaEE jersey project 中所述),但给出了相同的错误。事实上,如果发布一个干净的 javaee-api 7.0 依赖项目而不是 jersey 依赖项,它会给出相同的错误并给出相同的错误(我想 glassfish 无论如何都使用 jersey)。

所以我想我在这里错过了一些东西,任何善良的灵魂可以让我填满什么? :)

【问题讨论】:

    标签: rest jakarta-ee jersey moxy


    【解决方案1】:

    降级到 Glassfish 4.1.0,然后它完美运行。 4.1.1 版本可能存在一些问题?也会尝试每晚,但现在可以了。

    【讨论】:

    • 感谢您拯救了我的理智。
    • nightly 对我也不起作用...我想我会使用 4.1 直到 4.1.2 出来
    • 为我工作!!
    【解决方案2】:

    我设法通过更新 Glassfish 4.1.1 附带的 org.eclipse.persistence.moxy.jar 文件中的 Manifest 解决了这个问题,而不是降级到 Glassfish 4.1.0

    我采取的步骤:

    1. 从此帖子获取更新的 Manifest.mf 文件(附于 2015-03-26 06:08:50 EDT) https://bugs.eclipse.org/bugs/show_bug.cgi?id=463169

    2. 将 org.eclipse.persistence.moxy.jar 文件中的 Manifest.mf 文件替换为您下载的文件。 该文件可在此处找到: {c}:\glassfish4\glassfish\modules\org.eclipse.persistence.moxy.jar

    3. 重启 Glassfish

    感谢那些在 bugs.eclipse.org 上发布并修复此问题的人

    【讨论】:

    • 我只是downloaded eclipse version 2.6.1 并抓住了org.eclipse.persistence.moxy.jar 并将我的.../glassfish4/glassfish/modules/ 文件夹中的那个替换为新的。似乎对我很有效。感谢您的回答!
    • 这应该是那些坚持 4.1.1 的人的答案
    • 与此处的另一个答案一致:stackoverflow.com/a/45762121/500452,建议下载新 jar(而不是修改旧 jar)
    【解决方案3】:

    我没有降级到 4.1.0,而是发现切换到 Payara 是一个不错的选择。

    【讨论】:

      猜你喜欢
      • 2016-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多