将Spring Boot Jar转换为War

9

我开发了一个Spring Boot项目。现在我的需求是将Jar包转换为war包。 1. 我已经将Jar包转换为war包。转换后,我部署了这个war包到Tomcat服务器上,并尝试访问Web服务。

@EnableAsync
@RestController
@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan(basePackages="com.app.sensiple")
public class SensipleApplication extends SpringBootServletInitializer {

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



    public static void main(String[] args) {
        SpringApplication.run(SensipleApplication.class, args);
    }
    @RequestMapping("/test")
    String test() {
        return "This is test and it's Working fine!";
    }

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>com.app</groupId>
    <artifactId>snservice</artifactId>
    <version>443-async</version>
    <packaging>war</packaging>

    <name>sensiple</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</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-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20160810</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>               
            </plugin>
        </plugins>
    </build>


</project>

网址: localhost:8080/snservice/test

以上网址运行良好。

  1. 我的应用程序包含不同的包,如控制器(Controller),服务(Service)和存储库(Repo)。我已经添加了项目结构的截图。 Scrren shot Project structure

  2. 问题是我无法访问控制器(Controller)包中的服务。我需要为此配置DispatcherServlet吗?有人可以帮帮我吗?


3
在使用@SpringBoot注解时,您不需要再使用@ComponentScan@EnableAutoConfiguration注解,因为它们已经被隐含在其中。您的应用程序将自动检测子包中的所有内容。您在 pom.xml 中具体做了什么更改?是否按照步骤中参考指南所述操作?(仍需要spring-boot-maven-plugin,且spring-boot-starter-tomcat依赖应该被限定为provided作用域。) - M. Deinum
谢谢您的回复,但我已经完成了所有步骤,仍然面临着同样的问题。 - VijayS
我也附上了我的pom.xml,请检查一下。 - VijayS
1
当应用程序部署后,您如何调用它?URL 与本地运行时不同,除非您已将其配置为在相同的 URL 上运行。但是,/snservice 应该被替换为 /<name-of-war>,例如 /snservice-443-async,因为它将(默认情况下)部署在与存档文件名相同的 URL 上。 - M. Deinum
非常感谢,我在URL中使用了那个战争名称,现在没问题了。它现在可以工作了。 - VijayS
2个回答

7
如果您正在使用Maven,您可以在pom.xml中指定它的打包方式。
<packaging>war</packaging>
        <!-- ... -->
        <dependencies>
            <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>
            <!-- ... -->
        </dependencies>

接下来,您需要提供一个SpringBootServletInitializer子类并覆盖其configure方法。

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

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

}

1
感谢您的支持,但我仍然遇到相同的问题,404错误。无法访问控制器包中的服务。我已经添加了pom.xml,请检查一下。 - VijayS
嗨@VijayS,尝试添加SpringBootServletInitializer。 - Eugen
我也遇到了同样的问题。我尝试了SO中的所有解决方案,但什么都没有改变。控制权已经传递到类,但是只有.jsp文件没有渲染。请任何人帮助我。 - chandru

0

@Rupendra Sharma的回答有什么不同之处?抄袭 - Valijon
该链接显示404错误页面。 - Stephen C

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接