Spring Boot 如何让项目在 Eclipse 中的服务器上运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39994693/
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
Spring boot how to make project run on server in eclipse
提问by Kamini
I am new to Spring Boot, I have sample spring boot code in my Eclipse IDE.
Now to run the project. In project there is class "Application", I run that using Run As Java Application and then Its running on given port.
But I want it to run using Run on Server option of Eclipse, so whenever I try to run that on server it gives me 404.
我是 Spring Boot 的新手,我的 Eclipse IDE 中有示例 Spring Boot 代码。
现在运行项目。在项目中有类“应用程序”,我使用 Run As Java Application 运行它,然后它在给定的端口上运行。
但我希望它使用 Eclipse 的“在服务器上运行”选项运行,所以每当我尝试在服务器上运行它时,它都会给我 404。
Please give me any idea to resolve this issue.
请给我解决此问题的任何想法。
Code:
代码:
@ComponentScan
@Configuration
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
System.out.println(Arrays.toString(beanNames));
}
}
application.properties
应用程序属性
server.address=localhost
server.port=8080
server.contextPath=/spring-security-cas
app.service.security=http://localhost:8080/j_spring_cas_security_check
app.service.home=http://localhost:8080/
pom.xml
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.esco.demo</groupId>
<artifactId>spring-security-cas</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo-spring-security-cas</name>
<description>Demo project for Spring Boot</description>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>1.2.1.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-cas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency>
<!-- Usefull for accessing to jsp -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- END Usefull for accessing to jsp -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.8</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>org.esco.demo.ssc.Application</start-class>
<java.version>1.7</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Note: Project running when run Application as Run as Java Application, problem is I want it to run using Run on Server option.
注意:当将 Application 作为 Run as Java Application 运行时,项目正在运行,问题是我希望它使用 Run on Server 选项运行。
回答by M. Deinum
If you want to deploy it to a container, instead of using the embedded container follow this sectionin the reference guide.
如果要将其部署到容器中,而不是使用嵌入式容器,请按照参考指南中的这一部分进行操作。
In short the steps are
简而言之,步骤是
- Use
war
packaging instead ofjar
packaging - Let your
Application
class extendsSpringBootServletInitializer
and implement theconfigure
method. - Mark the container dependencies (tomcat I guess) as
provided
- 使用
war
包装代替jar
包装 - 让您的
Application
类扩展SpringBootServletInitializer
并实现该configure
方法。 - 将容器依赖项(我猜是 tomcat)标记为
provided
So in a nutshell
所以简而言之
- Change
<packaging>jar</packaging>
to<packaging>war</packaging>
Change your
Application
public class Application extends SpringBootServletInitializer { ... protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); } }
Add tomcat dependency as
provided
.<dependency> <groupId>org.springframework.boot</groupId> <artifcatId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
- 更改
<packaging>jar</packaging>
为<packaging>war</packaging>
改变你的
Application
public class Application extends SpringBootServletInitializer { ... protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); } }
将 tomcat 依赖项添加为
provided
.<dependency> <groupId>org.springframework.boot</groupId> <artifcatId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
For more elaborate information check the reference guide.
有关更多详细信息,请查看参考指南。
回答by farrellmr
Do you mean a tomcat instance on eclipse? What are the logs saying? That there is no application deployed?
你是说eclipse上的tomcat实例吗?日志在说什么?那有没有应用部署?
Then you need to deploy a war to tomcat -
然后你需要部署一个war到tomcat——
Change -
改变 -
<packaging>jar</packaging>
To
到
<packaging>war</packaging>
And it should work.
它应该工作。