Java 如何在 Eclipse tomcat 中运行 Spring Boot 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34927236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run spring boot app in Eclipse tomcat?
提问by blue-sky
I have a Spring boot application and want to run this as a server app within Eclipse. So the app will be recognized as a Tomcat web app and can be added I update the facet :
我有一个 Spring 启动应用程序,想在 Eclipse 中将它作为服务器应用程序运行。因此该应用程序将被识别为 Tomcat Web 应用程序并且可以添加我更新方面:
When I run the web app my rest services are not being found. A spring boot app contains a different folder structure than spring apps before spring boot was released. Can a spring boot app be run from an eclipse configured tomcat ? Also prior to spring boot the controllers section of the app needs to specified. Do I need to update my spring boot app with these settings in order to run from Eclipse tomcat ?
当我运行 Web 应用程序时,找不到我的休息服务。spring boot 应用程序包含的文件夹结构与 spring boot 发布之前的 spring 应用程序不同。可以从 eclipse 配置的 tomcat 运行 spring boot 应用程序吗?同样在春季启动之前,需要指定应用程序的控制器部分。我是否需要使用这些设置更新我的 Spring Boot 应用程序才能从 Eclipse tomcat 运行?
I don't want to create a war and then copy it to webapps folder of tomcat.
我不想创建战争,然后将其复制到 tomcat 的 webapps 文件夹中。
Update : The reason to run this app within Eclipse tomcat container is that I have other non spring boot applications that my new spring boot app will depend on.
更新:在 Eclipse tomcat 容器中运行此应用程序的原因是我有其他非 Spring Boot 应用程序,我的新 Spring Boot 应用程序将依赖这些应用程序。
采纳答案by leo
Check your pom.xml:
检查你的 pom.xml:
<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>
Then check your main app class:
然后检查您的主要应用程序类:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Then configure Project Facets (as you have already did):
然后配置 Project Facets(正如您已经做过的那样):
- Dynamic Web Module
- Java
- Javascript
- 动态网页模块
- 爪哇
- Javascript
Then check your Deployment Assembly:
Done!
完毕!
回答by asardana
For those who are unable to see "Deployment Assembly" option, adding below nature to the .project file will enable the deployment assembly option.
对于那些看不到“部署程序集”选项的人,将下面的性质添加到 .project 文件将启用部署程序集选项。
org.eclipse.wst.common.modulecore.ModuleCoreNature
org.eclipse.wst.common.modulecore.ModuleCoreNature
After defining the packing structure for my project, I am able to run the application on Tomcat server without copying the war file in webapps directory manually.
为我的项目定义打包结构后,我可以在 Tomcat 服务器上运行应用程序,而无需手动复制 webapps 目录中的 war 文件。
回答by Sai Goud
Just add this annotation in the main class of spring boot.
只需在spring boot的主类中添加这个注解即可。
@SpringBootApplication
@Configuration
@ComponentScan(value={"packages name to scan"})
Add dependence in pom.xml
file.
在pom.xml
文件中添加依赖。
回答by andy
Short answer
简答
- use gradle plugins:
eclipse-wtp
andwar
- extend
SpringBootServletInitializer
inApplication
- 使用 gradle 插件:
eclipse-wtp
和war
- 延伸
SpringBootServletInitializer
到Application
Explanation:
解释:
In order to run a spring boot application on tomcat generally the produced artifact has to be a war
application.
为了在 tomcat 上运行 spring boot 应用程序,通常生成的工件必须是war
应用程序。
In order to make it run on tomcat inside Eclipse you have to use a WTP-Project (Web Tools Platform aka Dynamic Web Module) using the eclipse-wtp
gradle plugin.
为了让它在 Eclipse 中的 tomcat 上运行,您必须使用eclipse-wtp
gradle 插件使用 WTP-Project(Web 工具平台又名动态 Web 模块)。
The SpringBootServletInitializer
thing has to be extended in order to make Spring Boot application context launch when Tomcat is launched.
该SpringBootServletInitializer
事情是为了让春季启动应用程序环境中启动时启动Tomcat启动延长。
How to create a test project
如何创建测试项目
Spring offers a Getting Started Tutorialhow to build a REST webservice with Spring Boot. I enhanced the project to work with Tomcat and Eclipse:
Spring 提供了如何使用 Spring Boot 构建 REST web 服务的入门教程。我增强了项目以使用 Tomcat 和 Eclipse:
Check out
查看
the sample project:
示例项目:
git clone https://github.com/spring-guides/gs-rest-service.git
Import it into eclipse
将其导入eclipse
Import the complete/
subfolder as gradle project.
将complete/
子文件夹导入为 gradle 项目。
Change build.gradle
改变 build.gradle
remove
消除
apply plugin: 'java'
apply plugin: 'eclipse'
add
添加
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
Change src/main/java/hello/Application.java
改变 src/main/java/hello/Application.java
add extension of SpringBootServletInitializer
添加扩展名 SpringBootServletInitializer
public class Application extends SpringBootServletInitializer {
...
}
That done I could deploy the sample project to a Tomcat server configured in eclipse and start the server. The url is: http://localhost:8080/complete/greeting?name=User
完成后,我可以将示例项目部署到 Eclipse 中配置的 Tomcat 服务器并启动该服务器。网址是:http://localhost:8080/complete/greeting?name=User
Check out the Complete Sample.
查看完整示例。
回答by Fikreselam Elala
Follow this 2 simple steps and your good to go!
遵循这 2 个简单的步骤,一切顺利!
Step 1. Change packaging format to war instead of jar in your pom.xml file.
步骤 1. 在 pom.xml 文件中将打包格式更改为 war 而不是 jar。
**<packaging>war</packaging>**
Step 2. Right click on your project then do Maven -> update project.
步骤 2. 右键单击您的项目,然后执行 Maven -> 更新项目。
After doing this steps you will find an option to "Run on server" when you try to run your project.
完成这些步骤后,当您尝试运行项目时,您会发现“在服务器上运行”的选项。