如何在tomcat服务器上部署spring boot web应用程序
我已经创建了spring boot web应用程序,但我无法在tomcat上部署spring boot web应用程序WAR文件,我可以将其作为java应用程序运行。 如何在tomcat上将spring boot应用程序作为Web服务运行。 我正在使用以下代码。 如果可以在tomcat上运行,请在不使用web.xml和使用web.xml的情况下帮助我使用注释。
@SpringBootApplication public class Application extends SpringBootServletInitializer { protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } }
以下代码为rest控制器
@RestController public class HelloWorld{ @RequestMapping(value = "/hello", method = RequestMethod.GET) public ResponseEntity get() { return new ResponseEntity("Hello World", HttpStatus.OK); } }
在我使用的Pom.xml之后
org.springframework web-service 0.1.0 org.springframework.boot spring-boot-starter-parent 1.3.0.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.springframework spring-beans org.springframework spring-webmvc com.googlecode.json-simple json-simple 1.6 org.springframework.boot spring-boot-maven-plugin spring-releases https://repo.spring.io/libs-release spring-releases https://repo.spring.io/libs-release war
以下是关于如何将Spring Boot
App部署为war
文件的两个很好的文档。
你可以按照这个春季启动howto-traditional-deployment文档 –
根据此文档的步骤 –
-
您更新应用程序的主类以扩展
SpringBootServletInitializer
。 -
下一步是更新构建配置,以便项目生成war文件而不是jar文件。
war -
标记所提供的嵌入式servlet容器依赖项。
org.springframework.boot spring-boot-starter-tomcat provided
还有一种方法 –
请参阅本春季io文档 ,其中概述了如何将spring boot应用程序部署到应用程序服务器。
脚步 –
-
将
jar
包装jar
war
。 -
在
pom.xml
注释spring-boot-maven-plugin
插件的声明 -
通过扩展
SpringBootServletInitializer
并覆盖configure
方法,将Web入口点添加到应用程序中 -
删除
spring-boot-starter-tomcat dependency
并修改spring-boot-starter-web
依赖项
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat
在pom.xml
,删除spring-beans
和spring-webmvc
依赖项。 spring-boot-starter-web
依赖包括那些依赖。
Spring引导提供了将应用程序部署为servlet 3.x
( 不带web.xml )支持tomcat服务器的传统war文件的选项。请参阅spring boot文档 。 我将简要介绍一下你需要做什么。
第1步 :修改pom.xml
以将包装更改为war :(您已经这样做了)
war
第2步 :改变你的依赖
org.springframework.boot spring-boot-starter-tomcat
至
org.springframework.boot spring-boot-starter-tomcat provided
步骤3 :在
标签下的pom.xml
修改您的战争名称(如果您需要避免附加战争名称的版本详细信息)。
web-service .....
步骤4 :运行maven build来创建war: clean install
步骤5:在tomcat中部署生成的war文件web-service.war
并在浏览器中请求url http://
你应该得到Hello World
。
注意:您也可以删除多余的依赖项,如@Ali Dehghani所说。
标记spring-boot-starter-tomcat
依赖项,如:
org.springframework.boot spring-boot-starter-tomcat provided
注意1:从pom.xml
删除多余的依赖项,如:
org.springframework spring-beans org.springframework spring-webmvc
它们是弹簧靴启动包的一部分
注2:让jar不战
将弹簧启动jar转换为弹簧启动战争的过程记录在: http : //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packaging Long简而言之,按照你在例子中的方式设置你的入门类,然后在.pom文件中将包装从jar切换到war。 此外,您需要设置spring-boot-starter-tomcat依赖项。 再一次,该过程在上面的链接中以完整的forms记录。 有关此主题的更多信息,请参见spring io指南“将Spring Boot JAR应用程序转换为WAR”,该指南可从https://spring.io/guides/gs/convert-jar-to-war/获得。如果我可以提供任何进一步的帮助,让我知道,我会帮助你。
我遇到了这个问题。 以上大部分都是好建议。 我的问题是最初部署在Pivotal TC服务器上。
-
使pom中的包装成为WAR。
war -
将依赖项添加到pom
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat provided -
我使用Application类来保存main()。 Main有配置代码,以便可以注入
EntityManager
等。 此EntityManager
使用ApplicationContext
和persistence.xml
文件中的信息来获取持久性信息。 在SpringBoot下工作正常但在Tomcat下没有。 事实上,在Tomcat下,Main()
不会被调用。 Application类扩展了SpringBootServletInitializer
。 -
Application类中添加了以下方法:
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { Application obj = new Application(); @SuppressWarnings("resource") ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF/spring/applicationContext.xml"); applicationContext.registerShutdownHook(); applicationContext.getBeanFactory().autowireBeanProperties( obj, AutowireCapableBeanFactory.AUTOWIRE_NO, false); return application.sources(Application.class); }
只需要最后一行 – 其他代码之前保存在main()
,并在此处移动以注入EntityManager
工作。
-
所需的注释是:
@EnableAutoConfiguration @ComponentScan //@SpringBootApplication will consist of both of these and @Configuration
-
一些url现在可能需要更改根上下文以包含
contextname .
希望有所帮助