如何转换此spring boot示例应用程序,以便构建可在外部Tomcat服务器中运行的war文件

时间:2017-07-14 20:59:07

标签: spring-boot

我试图找出如何转换此示例Spring启动应用程序,以便生成的war文件可以在外部Tomcat服务器中运行:enter link description here

这是我的modiefied pom.xml,它确实生成了war文件:

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd“&GT;     4.0.0

<groupId>org.springframework</groupId>
<artifactId>gs-securing-web</artifactId>
<version>0.1.0</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
</parent>

<packaging>war</packaging>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

</dependencies>

<properties>
    <java.version>1.8</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>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>

这是我的新的Application类,它扩展了SpringBootServletInitializer:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(ApplicationOLD.class);
}

public static void main(String[] args) throws Exception {
    SpringApplication.run(ApplicationOLD.class, args);
}

}

在控制台中,我可以看到该应用程序已由Tomcat启动:

  

2017-07-14 16:53:10.736 INFO 12020 --- [main] hello.Application:使用PID 12020在LAPTOP-I1Q04POH上启动应用程序v0.1.0(C:\ apache-tomcat-9.0.0.M22 \ webapps \ gs-secured-web-0.1.0 \ WEB-INF \ classes由akova在C:\ apache-tomcat-9.0.0.M22 \ bin中启动   2017-07-14 16:53:10.740 INFO 12020 --- [main] hello.Application:没有活动的配置文件集,回退到默认配置文件:默认   2017-07-14 16:53:11.012 INFO 12020 --- [main] ationConfigEmbeddedWebApplicationContext:刷新org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5f4f198c:启动日期[Fri Jul 14 16:53:11 EDT 2017];上下文层次结构的根   2017-07-14 16:53:11.209 INFO 12020 --- [main] o.s.web.context.ContextLoader:Root WebApplicationContext:初始化在197 ms完成   2017-07-14 16:53:11.256 INFO 12020 --- [main] o.s.b.w.servlet.FilterRegistrationBean:映射过滤器:'errorPageFilter'为:[/ *]   2017-07-14 16:53:11.295 INFO 12020 --- [main] hello.Application:1.118秒启动应用程序(JVM运行3.925)

但是当我在浏览器中访问网址时,我无法访问该应用。我使用网址http://localhost:8080/gs-securing-web-0.1.0/收到HTTP 404错误。

我缺少什么?感谢

2 个答案:

答案 0 :(得分:0)

the porject本身所述,以及构建此项目时要遵循的教程,您应该只使用此URI:http://localhost:8080/来使用您的服务。

http://localhost:8080/hellohttp://localhost:8080/login用于其他资源。

答案 1 :(得分:0)

在依赖项部分

下添加此依赖项
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>