【发布时间】:2017-03-23 06:46:37
【问题描述】:
我尝试将一个简单的 Jersey 应用程序部署到我的 Tomcat 服务器。但端点没有响应 (404)。
当我将此应用程序作为 Spring Boot 应用程序启动时,一切正常。但是作为 Tomcat 中的 Servlet,TestEndpoint 不起作用。
小服务程序:
@SpringBootApplication
public class ServletInitializer extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ServletInitializer.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ServletInitializer.class);
}
}
我有 3 个文件: 泽西配置:
@Configuration
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
packages("com.example");
register(TestEndpoint.class);
}
}
测试端点:
@Component
@Path("/test")
public class TestEndpoint {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@GET
@Path("/test")
@Produces("application/json")
public Response test () {
log.error("test");
return Response.ok("test").build();
}
}
pom.xml:
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
请求: 作为应用程序: curl http://localhost:8080/test/test(有效) 作为 servlet (api.war):http://localhost:8080/api/test/test(不起作用) 有什么想法吗?
【问题讨论】:
-
你能显示你的 pom.xml
-
添加到我的问题中
标签: java spring tomcat servlets jersey