【发布时间】:2015-07-02 09:03:48
【问题描述】:
我正在使用 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 日志显示此错误。
严重:向 org.springframework.web.context.ContextLoaderListener 类的侦听器实例发送上下文初始化事件的异常 java.lang.IllegalStateException:无法初始化上下文,因为已经存在一个根应用程序上下文 - 检查您的 web.xml 中是否有多个 ContextLoader* 定义!
我不确定还有什么其他 ContextLoader。
UpdateToUpdate:
好的,在将 jar 更新到最新版本后,使用注释 @SpringBootApplication for Application.java 类,应用程序启动但是当我调用我收到的服务之一时:
java.lang.ClassNotFoundException: org.glassfish.jersey.process.internal.RequestExecutorFactory
谷歌搜索说我应该添加 jersey-common 和 jersey-core jar,我做了,但它没有解决它。由于某种原因,看起来 RequestExecutorFactory.class 没有打包在 jersey-common-2.19.jar 中。
【问题讨论】:
-
你用
@SpringBootApplication注释类Application了吗? -
是的,我确实用'@SpringBootApplication'注释了Application类,并创建了:public @interface SpringBootApplication {}(我不确定这是否正确。)
-
这个注解来自spring boot,不能自己创建。在 github 上发布您的项目 - 您在此处粘贴的文件之外有问题
-
好的,这里是github项目github.com/elf11/Spring-boot/tree/master/…(我删除了为SpringBootApplication创建的接口)
-
首先 - 你真的需要 Jersey 作为 REST 端点吗?如果您这样做,请先查看github.com/spring-projects/spring-boot/tree/master/… - 在您的代码中,我感觉您让事情变得过于复杂(pom 中的依赖项)。
标签: java maven spring-mvc tomcat spring-boot