我是Spring Boot的新手,我在Eclipse中有示例Spring启动代码 IDE。
现在运行该项目。在项目中有类"应用程序",我运行 使用Run As Java Application然后在给定端口上运行它。
但我想让它使用Eclipse的Run on Server选项运行,所以 每当我尝试在服务器上运行它时,它就会给我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));
}
}
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
<?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>
注意:项目运行时运行Application as Java Application, 问题是我希望它使用Run on Server选项运行。
答案 0 :(得分:8)
如果要将其部署到容器,请参阅参考指南中的this section,而不是使用嵌入式容器。
简而言之,步骤是
war
包装代替jar
包装Application
课程延伸SpringBootServletInitializer
并实施configure
方法。 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。
答案 1 :(得分:0)
你的意思是日食上的tomcat实例吗?日志说什么?没有部署应用程序?
然后你需要将战争部署到tomcat -
改变 -
<packaging>jar</packaging>
到
<packaging>war</packaging>
它应该有用。