问题描述
我已经创建了 spring boot web 应用程序,但是我无法在 tomcat 上部署 spring boot web 应用程序 WAR 文件,我可以将它作为 java 应用程序运行.如何在 tomcat 上将 Spring Boot 应用程序作为 Web 服务运行.我正在使用以下代码.如果可以在 tomcat 上运行,请帮助我在不使用 web.xml 和使用 web.xml 的情况下使用注释.
@SpringBootApplication公共类应用程序扩展 SpringBootServletInitializer {受保护的 SpringApplicationBuilder 配置(SpringApplicationBuilder 应用程序){返回 application.sources(Application.class);}public static void main(String[] args) 抛出异常 {SpringApplication.run(Application.class, args);}}
rest 控制器的以下代码
@RestController公共类HelloWorld{@RequestMapping(value = "/hello", method = RequestMethod.GET)公共响应实体得到() {return new ResponseEntity("Hello World", HttpStatus.OK);}}
在我使用的 Pom.xml 之后
<artifactId>web-service</artifactId><version>0.1.0</version><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.0.RELEASE</version></父母><依赖项><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></依赖><依赖><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId></依赖><依赖><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId></依赖><依赖><groupId>com.googlecode.json-simple</groupId><artifactId>json-simple</artifactId></依赖></依赖项><属性><java.version>1.6</java.version></属性><构建><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></插件></插件></build><存储库><存储库><id>spring-releases</id><url>https://repo.spring.io/libs-release</url></repository></repositories><pluginRepositories><插件库><id>spring-releases</id><url>https://repo.spring.io/libs-release</url></pluginRepository></pluginRepositories><包装>战争</包装>
这里有两个很好的文档,介绍了如何将 Spring Boot
应用程序部署为 war
文件.
你可以关注这个spring boot howto-traditional-deployment 文档 -
根据本文档的步骤 -
您更新应用程序的主类以扩展
SpringBootServletInitializer
.下一步是更新您的构建配置,以便您的项目生成一个 war 文件而不是 jar 文件.
将嵌入的 servlet 容器依赖项标记为已提供.
<groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><范围>提供</范围></依赖>
还有一种方式 -
看到这个 spring io 文档,其中概述了如何将 Spring Boot 应用程序部署到应用程序服务器.
步骤 -
将
jar
包装改为war
.注释掉
pom.xml
中通过扩展
SpringBootServletInitializer
并覆盖configure
方法,将 Web 入口点添加到您的应用程序中移除
spring-boot-starter-tomcat依赖
并将你的spring-boot-starter-web
依赖修改为
spring-boot-maven-plugin
插件的声明<groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><排除事项><排除><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></排除></排除项></依赖>
在您的 pom.xml
中,删除 spring-beans
和 spring-webmvc
依赖项.spring-boot-starter-web
依赖将包含这些依赖.
I have created spring boot web application, but I am unable to deploy spring boot web application WAR file on tomcat and I am able to run it as java application. How to run spring boot application as web service on tomcat. I am using following code. If it is possible to run on tomcat plz help me using annotations without using web.xml and with using 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);
}
}
Following code for rest controller
@RestController
public class HelloWorld{
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public ResponseEntity<String> get() {
return new ResponseEntity<String>("Hello World", HttpStatus.OK);
}
}
Following Pom.xml I am using
<groupId>org.springframework</groupId>
<artifactId>web-service</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>
<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>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.6</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
<packaging>war</packaging>
Here are two good documentations on how to deploy the Spring Boot
App as a war
file.
You can follow this spring boot howto-traditional-deployment documentation -
Steps according to this documentation -
You update your application’s main class to extend
SpringBootServletInitializer
.The next step is to update your build configuration so that your project produces a war file rather than a jar file.
<packaging>war</packaging>
Mark the embedded servlet container dependency as provided.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
and one more way -
See this spring io documentation which outlines how to deploy the spring boot app to an application server.
Steps -
Change
jar
packaging towar
.Comment out the declaration of the
spring-boot-maven-plugin
plugin in yourpom.xml
Add a web entry point into your application by extending
SpringBootServletInitializer
and override theconfigure
methodRemove the
spring-boot-starter-tomcat dependency
and modfiy yourspring-boot-starter-web
dependency to
In your pom.xml
, remove spring-beans
and spring-webmvc
dependencies. The spring-boot-starter-web
dependency will include those dependecies.
这篇关于如何在tomcat服务器上部署spring boot web应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!