JavaEE Jersey 2.0项目中的MOXy异常

Bas*_*ase 14 rest jersey java-ee moxy

我试图在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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

TestEntity类(在同一个包中):

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

    public String getContent() {
        return content;
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

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

所以我添加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>
Run Code Online (Sandbox Code Playgroud)

我在部署时遇到此错误

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

为了好玩,我尝试删除web.xml,将依赖项jersey-container-servlet-core更改为jersey-container-servlet并改为创建一个Application类(如在ContainerRequestFilter中讨论的那样不会在JavaEE jersey项目中运行)但是给出相同的错误.事实上,如果发布一个干净的javaee-api 7.0依赖项目而不是球衣依赖项并且给出相同的错误(我认为使用泽西的glassfish),它会给出相同的错误.

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

Bas*_*ase 19

降级为Glassfish 4.1.0然后它完美运行.有些问题可能与4.1.1版本有关吗?也会尝试每晚,但它现在有效.

  • 谢谢你拯救我的理智. (2认同)
  • 懒惰的直接链接:https://glassfish.java.net/download-archive.html (2认同)

小智 19

我设法通过更新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上发布并修复此问题的人