您好我正在尝试使用Moxy和Jersey 2使用进程JSON,我的理解是,只要它在类路径上就可以自动发现Moxy,它应该像我一样在我的pom.xml中设置依赖关系,我也尝试手动注册Moxy但没有成功。
尝试访问(" localhost:8080 / Test / hello")时的资源时,浏览器会生成HTTP状态404 - 未找到。
在访问资源时,eclipse控制台中会生成以下警告。
警告(警告:解析媒体类型时出错' MediaType.APPLICATION_JSON')
(警告:在SERVER运行时中注册的提供程序HelloRest不实现适用于SERVER运行时的任何提供程序接口。由于约束配置问题,将忽略提供程序HelloRest。)
的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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Test</groupId>
<artifactId>Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.23.1</version>
<exclusions>
<exclusion>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.23.1</version>
</dependency>
</dependencies>
</project>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp. org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Test</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>Test</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>ResourceLoader</param-value>
</init-param>
</servlet>
</web-app>
ResourceLoader.java
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import org.glassfish.jersey.server.ResourceConfig;
@ApplicationPath("/*")
public class ResourceLoader extends Application//ResourceConfig
{
@Override
public Set<Class<?>> getClasses()
{
final Set<Class<?>> classes = new HashSet<Class<?>>();
// register root resource
classes.add(HelloRest.class);
return classes;
}
/*
public ResourceLoader()
{
packages("src");
}*/
}
HelloRest.java
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.Produces;
@Path("/hello")
public class HelloRest
{
@GET
@Produces(MediaType.APPLICATION_JSON)
public Event message()
{
System.out.println("Hey");
Event event = new Event(1,"Terry","Edinburgh");
return event;
}
}
Event.java
public class Event
{
private int id;
private String name;
private String location;
public Event(){}
public Event(int id,String name, String location)
{
this.id = id;
this.name = name;
this.location = location;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}