java - Spring boot RESTful 服务作为 WAR 而不是 JAR

标签 java maven spring-mvc tomcat spring-boot

我正在使用 Spring-boot 创建 Java REST 应用程序。我已成功加载 example here并且我尝试将 JAR 文件转换为 Spring-boot 站点上显示的 WAR 文件。我修改了我的 pom.xml 文件,添加:

<!-- other pom.xml conf -->
<packaging>war</packaging>

<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

然后我修改了 Application.java 类来初始化 servlet(这是 Spring-boot 用来替换 web.xml 文件的):

public class Application extends SpringBootServletInitializer {
//    public static void main(String[] args) {
//        new SpringApplicationBuilder(Application.class).run(args);
//    }

@Bean
public ServletRegistrationBean jerseyServlet() {
    ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/*");
      registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyInitialization.class.getName());
    return registration;
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
}

}

我生成了 .WAR 文件,但是当我将它部署到 Tomcat 上时,服务返回 404。Tomcat 日志也没有显示任何错误。

所以我不确定这可能是什么问题。如果您有任何想法,请分享。谢谢!


更新:

最初它不起作用,因为除了 Application 类的 SpringBootApplication 注释之外,我还有其他注释。把那些去掉,现在 Tomcat 日志显示此错误。

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!

我不确定那里还有什么其他 ContextLoader。


更新到更新:

好的,将 jars 更新到最新版本后,使用 Application.java 类的注释 @SpringBootApplication,应用程序启动,但是当我调用我收到的服务之一时:

java.lang.ClassNotFoundException: org.glassfish.jersey.process.internal.RequestExecutorFactory

谷歌搜索说我应该添加 jersey-common 和 jersey-core jar ,我确实添加了,但没有解决。由于某种原因,看起来 RequestExecutorFactory.class 没有打包在 jersey-common-2.19.jar 中。

最佳答案

为什么你的 Application 类中有这么多注解 here

@SpringBootApplication 应该足以启用自动配置。 尝试移除其他的。

然后放回main方法。

我认为您混合了两种配置来创建 war :pre 3.0 和 post 3.0 servlet 容器(根据 Spring Boot documentation)

编辑:

我找到了这个 question与您的问题相关。

Jersey 正在加载 Spring ApplicationContext。查看日志的这一行:Spring WebApplicationInitializers detected on classpath: [com.jersey.Application@148ac084, org.glassfish.jersey.server.spring.SpringWebApplicationInitializer@7807c6d3]

您可以更新您的 Spring Boot 版本吗? 至少 1.20,这样您就可以使用 spring-boot-starter-jersey。集成 Spring 和 Jersey 会容易得多。

你可以找到一个例子here (Spring Boot官方示例)。

或者你必须排除初始化器的 org.glassfish.jersey.server.spring.SpringWebApplicationInitializer

关于java - Spring boot RESTful 服务作为 WAR 而不是 JAR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31180140/

相关文章:

java - 无法使用 Apache log4j2/slf4j 发送错误邮件

java - 如何将字符串发送到 GWT DataGrid?

java - 调用 JScrollPane 时未调用 PaintComponent

java从文件夹加载文件,但项目是一个模块

java.lang.IllegalAccessError : class lombok. javac.apt.LombokProcessor 无法访问类 com.sun.tools.javac.processing.JavacProcessingEnvironment

java - 如何在spring mvc中将空数字字段绑定(bind)到默认值0

java - 什么是 AbstractTestNGSpringContextTests

Java:groupingBy子值作为值

java - 如何使用 Maven FindBugs 生成 HTML 报告?

java - 如何使用 Spring MVC 4 从另一个 Web 项目引用静态资源