我尝试使用jersey moxy来测试json,我的maven依赖只有版本为2.22.2的'jersey-media-moxy'。
web.xml如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>webapp</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Json服务类是:
@Path("/json")
public class JsonService {
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject getJson() {
JsonObject obj = new JsonObject();
obj.setId(1);
obj.setName("json_test");
return obj;
}
@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_JSON)
public Response createJson(JsonObject obj) {
String result = "json object saved : "+obj;
return Response.status(201).entity(result).build();
}
}
但是,当我在eclipse中使用Tomcat 8.0运行此项目时,请键入
抛出以下错误:
A MultiException has 2 exceptions. They are:
1. java.lang.NoSuchFieldError: WADL
2. java.lang.IllegalStateException: Unable to perform operation: create on org.glassfish.jersey.server.wadl.processor.WadlModelProcessor
org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:394)
.........
root cause
java.lang.NoSuchFieldError: WADL
org.glassfish.jersey.server.wadl.processor.WadlModelProcessor.<init>(WadlModelProcessor.java:96)
.........
我的pom如下:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>webapp</groupId>
<artifactId>webapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<finalName>webapp</finalName>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.22.2</version>
</dependency>
</dependencies>
</project>
有什么问题?
提前致谢!