我正在使用没有任何web.xml的servlet 3.1创建Jersey 2.x服务 我的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>testex</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>testex Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.22.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.22.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<finalName>testex</finalName>
</build>
</project>
我使用下面给出的@ApplicationPath资源配置配置了服务:
package com.testex.service;
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
@ApplicationPath("resources")
public class TestexResourceConfig extends ResourceConfig {
public TestexResourceConfig() {
super();
registerClasses(TestexService.class);
}
}
我的服务如下:
package com.testex.service;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.testex.config.Config;
import com.testex.config.ConfigLoader;
import com.testex.provider.ProjectDetailsProvider;
import com.testex.provider.VersionsProvider;
import com.testex.provider.bean.Project;
import com.google.gson.Gson;
@Path("/rest")
public class TestexService {
private final Config config;
public TestexService() {
super();
config = ConfigLoader.getConfig();
}
@GET
@Path("getDefaultProjectId")
@Produces(MediaType.APPLICATION_JSON)
public Response getDefaultProjectId() {
return Response.status(200).entity(new Gson().toJson(new Project(String.valueOf(config.getProjectId()))))
.build();
}
@GET
@Path("getProjectDetails")
@Produces(MediaType.APPLICATION_JSON)
public Response getProjectDetails(@QueryParam("projectId") int projectId) {
return Response.status(200).entity(new Gson().toJson(new ProjectDetailsProvider().getProjectDetails(projectId)))
.build();
}
@GET
@Path("getProjectVersionDetails")
@Produces(MediaType.APPLICATION_JSON)
public Response getProjectVersionDetails(@QueryParam("projectId") int projectId) {
return Response.status(200).entity(new Gson().toJson(new VersionsProvider().getVersionsForProject(projectId)))
.build();
}
}
在上下文根&#34; testex&#34;上部署到Tomcat 8时,我点击了以下网址:
http://localhost:8080/testex/resources/rest/getDefaultProjectId
但是我收到404 Not Found错误。请帮忙。
答案 0 :(得分:0)
好吧我明白了。我需要做的就是将下面的依赖项添加到我的pom中,现在所有的工作都像魅力一样:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.2</version>
</dependency>