我尝试在 JavaEE 项目中实现 Json 支持,但遇到了生成 MOXy 相关异常的问题。我在 jersey.java.net 上读到 MOXy 应该是可自动发现的,但当我尝试时它似乎不起作用。
因此,为了使这一点易于查明,我刚刚生成了一个新的“jersey-quickstart-webapp”项目并更改了 MyResource,如下所示(我的目标是使用应用程序class 而不是 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 中所述),但给出了相同的错误。事实上,如果发布一个干净的 javaee-api 7.0 依赖项目而不是 jersey 依赖项,它会给出相同的错误,并给出相同的错误(我想 glassfish 无论如何都使用 jersey)。
所以我想我在这里错过了一些东西,任何善良的灵魂可以填补我的什么? :)
最佳答案
降级到Glassfish 4.1.0,然后就完美运行了。 4.1.1 版本可能存在一些问题?也会尝试每晚,但现在可以了。
关于rest - JavaEE Jersey 2.0 项目中的 MOXy 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33319659/