Java 如何在tomcat服务器上部署spring boot web应用程序
我已经创建了SpringBootWeb应用程序,但我无法在tomcat上部署SpringBootWeb应用程序WAR文件,我能够将其作为java应用程序运行。如何在tomcat上以web服务的形式运行spring启动应用程序。我正在使用以下代码。如果可以在tomcat plz上运行,请帮助我在不使用web.xml和使用web.xml的情况下使用注释Java 如何在tomcat服务器上部署spring boot web应用程序,java,spring,maven,spring-mvc,tomcat,Java,Spring,Maven,Spring Mvc,Tomcat,我已经创建了SpringBootWeb应用程序,但我无法在tomcat上部署SpringBootWeb应用程序WAR文件,我能够将其作为java应用程序运行。如何在tomcat上以web服务的形式运行spring启动应用程序。我正在使用以下代码。如果可以在tomcat plz上运行,请帮助我在不使用web.xml和使用web.xml的情况下使用注释 @SpringBootApplication public class Application extends SpringBootServletI
@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<String> get() {
return new ResponseEntity<String>("Hello World", HttpStatus.OK);
}
}
@RestController
公共类HelloWorld{
@RequestMapping(value=“/hello”,method=RequestMethod.GET)
公众反应{
返回新的ResponseEntity(“HelloWorld”,HttpStatus.OK);
}
}
下面是我使用的Pom.xml
<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>
org.springframework
网络服务
0.1.0
org.springframework.boot
spring启动程序父级
1.3.0.1发布
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧启动机tomcat
org.springframework
春豆
org.springframework
SpringWebMVC
com.googlecode.json-simple
简单json
1.6
org.springframework.boot
springbootmaven插件
春假
https://repo.spring.io/libs-release
春假
https://repo.spring.io/libs-release
战争
将spring boot jar转换为spring boot war的过程记录在: 长话短说,按照示例中的方式设置初学者类,然后在.pom文件中将打包从jar切换到war。此外,您需要将spring引导启动器tomcat依赖项设置为提供。在上面的链接中,该过程再次以完整的形式记录下来。 关于这个主题的更多信息可以在SpringIO指南“将SpringBootJAR应用程序转换为WAR”中找到,该指南位于
如果我能提供任何进一步的帮助,请告诉我,我会帮助您。将
spring boot starter tomcat
依赖项标记为提供的,例如:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
它们是spring boot starter软件包的一部分
注2:让jar不是war这里有两个关于如何将Spring Boot
应用程序部署为war
文件的好文档
您可以按照以下spring引导文档进行操作-
根据本文件的步骤-
更新应用程序的主类以扩展SpringBootServletInitializer
下一步是更新构建配置,以便项目生成war文件而不是jar文件<代码>战争
将嵌入式servlet容器依赖项标记为提供的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
在pom.xml
中,删除springbean
和springwebmvc
依赖项。springbootstarterweb
依赖项将包括这些依赖项 Spring boot提供了在支持tomcat服务器的servlet 3.x
(无web.xml)中将应用程序部署为传统war文件的选项。有关此选项,请参阅。我将简要介绍您需要在这里做什么
步骤1:修改pom.xml
以将打包更改为war:(您已经这样做了)
步骤4:运行maven build创建war:干净安装
步骤5:在tomcat中部署生成的war文件webservice.war
,并在浏览器http://:/webservice/hello
您应该获得Hello World
注意:正如@Ali Dehghani所说,您还可以删除冗余依赖项。我曾经遇到过这个问题。以上大部分都是很好的建议。我的问题是最初部署在关键的TC服务器上
使pom中的包装成为一场战争
<packaging>war</packaging>
只需要最后一行-另一个代码之前保存在main()
中,并移动到此处以注入EntityManager
工作
所需的注释包括:
@EnableAutoConfiguration
@ComponentScan
//@SpringBootApplication will consist of both of these and @Configuration
某些URL现在可能需要更改根上下文以包括
<artifactId>contextname</artifactId>.
contextname。
希望这有帮助如果我导出为jar,我无法在远程tomcat中部署,也无法从浏览器调用此应用程序知道为什么您需要一个单独的tomcat从浏览器访问应用程序。spring boot本身包含嵌入式服务器,如果您仍然需要它作为战争,请参阅我的回答如果我制作为jar,我无法在远程tomcat服务器上部署,因此无法从web浏览器调用。您可以在linux或windows和mac中安装spring boot应用程序作为服务。我想说的是,下一次你应该多想想你是如何部署你的java应用程序(或者任何应用程序)的。这种应用程序部署的端口绑定风格有许多优点,并且更符合当今的最佳实践。我知道,有时我们不得不这样做,因为一些疯狂的企业文化如何将应用程序用作服务。在远程tomcat服务器WAR文件选项中,唯一的问题是我做了您所说的更改,但它抛出的选择无法在服务器上运行意味着它无法识别WAR.OK。这里是另一个Spring Boot传统部署文档-。你可以试试这种方法!有没有办法使POM的某些部分与生命周期相适应?战争代替jar有什么好处?请检查这个问题:我得到的选择不能在任何服务器上运行。您是从eclipse运行的?是的。我正在运行eclipse。你能给我打电话吗
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<build>
<finalName>web-service</finalName>
.....
<packaging>war</packaging>
<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>
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);
}
@EnableAutoConfiguration
@ComponentScan
//@SpringBootApplication will consist of both of these and @Configuration
<artifactId>contextname</artifactId>.