将春季启动罐转换为战争

时间:2018-05-22 07:58:17

标签: java spring spring-boot

我开发了一个春季启动项目。现在我的要求是将jar包转换成战争。  我把罐子变成了战争。转换后,我将战争部署到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>

UrL:localhost:8080 / snservice / test

上面的网址工作正常。

  1. 我的应用程序包含不同的软件包,如Controller,Service和Repo。我添加了项目结构的屏幕截图。 Scrren shot Project structure

  2. 问题是我无法点击控制器包中的服务。我是否需要为此配置调度程序Servlet? 。有人请帮帮我吗?

2 个答案:

答案 0 :(得分:1)

如果您正在使用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 :(得分:-1)

在Spring Boot中,我们需要3个步骤将JAR转换为WAR:

  1. 将包装转变为战争
  2. 将嵌入式tomcat标记为已提供。
  3. 扩展SpringBootServletInitializer并覆盖configure() 方法。

我已经通过使用下面给出的网站(逐步介绍)将jar在春季启动时转换为战争。

https://tecmentor.in/spring-boot/convert-jar-to-war-in-spring-boot/