JavaEE Jersey 2.0项目中的MOXy异常

14
我正在尝试在JavaEE项目中实现Json支持,但出现了与MOXy相关的异常。我在jersey.java.net上读到MOXy应该是可自动发现的,但当我尝试时似乎不起作用。
所以为了更容易定位问题,我只是生成了一个新的“jersey-quickstart-webapp”项目,并将MyResource更改如下(我的目标是使用Application类而不是web.xml,但这是最简单的方法来确定它。无论如何都会出现错误)。
@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 not found by 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: Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper

为了调试,我尝试删除web.xml文件,将依赖项jersey-container-servlet-core更改为jersey-container-servlet,并创建一个Application类(如ContainerRequestFilter wont run in JavaEE jersey project中所讨论的),但仍然报同样的错误。实际上,如果发布依赖于Java EE API 7.0而不是Jersey依赖项的干净项目,也会给出相同的错误(我想GlassFish无论如何都会使用Jersey)。

因此,我猜我在这里漏掉了一些东西,有没有好心人可以告诉我缺少了什么? :)

3个回答

19

降级为Glassfish 4.1.0,然后它就完美地运行了。也许是4.1.1版本存在某些问题?我将尝试夜版,但现在它已经可用。


2
谢谢你拯救了我的理智。 - fatihpense
夜间版本对我也不起作用...我想我会使用4.1直到4.1.2发布。 - wutzebaer
对我有用!! - Aimal Khan

19

我通过更新随Glassfish 4.1.1一起提供的org.eclipse.persistence.moxy.jar文件中的清单(而不是降级到Glassfish 4.1.0)来解决了这个问题。

我所采取的步骤:

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

  2. 将下载的Manifest.mf文件替换org.eclipse.persistence.moxy.jar文件中的文件。该文件位于以下位置: {c}:\glassfish4\glassfish\modules\org.eclipse.persistence.moxy.jar。

  3. 重启Glassfish

感谢那些在bugs.eclipse.org上发布和解决了这个问题的人。


1
我刚刚下载了Eclipse 2.6.1版本,并获取了org.eclipse.persistence.moxy.jar,用新的替换了我的.../glassfish4/glassfish/modules/文件夹中的旧的。对我来说似乎工作得很好。感谢您的回答! - brain_bacon
这应该是那些卡在4.1.1版本的人的答案。 - kosgeinsky
与此答案的另一个回答保持一致:建议下载新的jar文件(而不是修改旧的jar文件)。 - Yohanes Khosiawan 许先汉

2

我认为,与其降级到4.1.0版本,切换到Payara是一个不错的选择。

该句涉及IT技术相关内容。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接