简体   繁体   English

将弹簧靴罐转换为战争

[英]converting spring boot jar to war

I developed a spring boot project. 我开发了一个春季启动项目。 Now my requirement is to convert the jar package into war. 现在我的要求是将jar包转换为war。 1. I converted the jar into war. 我把罐子变成了战争。 After converting, I deployed the war into the tomcat server and tried to hit the web service. 转换后,我将战争部署到了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 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 UrL:本地主机:8080 / snservice / test

The above url is working fine. 上面的网址工作正常。

  1. My application contains different packages like Controller, Service and Repo. 我的应用程序包含不同的程序包,例如Controller,Service和Repo。 I have added the screenshot of my project structure. 我添加了我的项目结构的屏幕截图。 Scrren Shot项目结构

  2. The issue is i can't able to hit the services in controller package. 问题是我无法访问控制器程序包中的服务。 Do i need to configure dispatcher Servlet for this???? 我是否需要为此配置调度程序Servlet ???? . Anybody please help me out? 有人请帮帮我吗?

If you are using maven you can indicate it in pom.xml packaging 如果您使用的是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>

Next you need to provide a SpringBootServletInitializer subclass and override its configure method. 接下来,您需要提供一个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);
    }

}

We need 3 steps for convert JAR to WAR in Spring Boot: 在Spring Boot中,我们需要3个步骤将JAR转换为WAR:

  1. Change packaging to war 改变包装走向战争
  2. Marked the embedded tomcat as provided. 将嵌入式tomcat标记为已提供。
  3. Extends SpringBootServletInitializer and override the configure() method. 扩展SpringBootServletInitializer并覆盖configure()方法。

I have converted jar to war in spring boot by using given below website (Explained step by step). 我已经通过使用下面给出的网站(逐步解释)将jar在春季启动时转换为战争。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM