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.
请给我任何解决此问题的想法。
代码:
@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));
}
}
应用程序属性
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
<?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>
注意:项目在run Application as Run as Java Application时运行, 问题是我希望它使用“在服务器上运行”选项运行。
最佳答案
如果你想将它部署到容器中,而不是使用嵌入式容器,请遵循 this section在引用指南中。
简而言之,步骤是
- 使用
war
包装而不是jar
包装 - 让您的
Application
类扩展SpringBootServletInitializer
并实现configure
方法。 - 将容器依赖项(我猜是 tomcat)标记为
provided
简而言之
- 更改
<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>
有关详细信息,请查看 the reference guide .
关于java - spring boot如何使项目在eclipse中的服务器上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39994693/