【问题标题】:Spring Boot with Jersey as Servlet in TomcatSpring Boot 与 Jersey 作为 Tomcat 中的 Servlet
【发布时间】: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


【解决方案1】:

(这只是一个评论)你能用你正在尝试的 GET url 更新问题吗?

答案- 问题是 pom.xml 中的 spring-boot-maven-plugin 插件没有被删除。根据https://spring.io/blog/2014/03/07/deploying-spring-boot-applications

【讨论】:

猜你喜欢
  • 2018-09-14
  • 1970-01-01
  • 2011-11-19
  • 2014-07-23
  • 2016-01-29
  • 1970-01-01
  • 2016-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多