【问题标题】:converting spring boot jar to war将 spring boot jar 转换为 war
【发布时间】:2018-10-31 22:06:31
【问题描述】:

我开发了一个 Spring Boot 项目。现在我的要求是将jar包转换成war。 1.我把罐子变成了战争。转换后,我将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。我添加了我的项目结构的屏幕截图。

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

【问题讨论】:

  • 您不需要@ComponentScan@EnableAutoConfiguration,因为@SpringBoot 已经暗示了这一点。您的应用程序将自动检测子包中的所有内容。您在pom.xml 中到底做了什么更改,并且您是否按照参考指南中的说明关注了the steps。 (您仍然需要 spring-boot-maven-plugin 并且 spring-boot-starter-tomcat 依赖项的范围应为 `provided。
  • 感谢您的回复,但我已经完成了所有步骤,但我仍然面临同样的问题。
  • 我的pom.xml也附上了,请检查一次。
  • 部署时如何调用应用程序?在本地运行时,url 会有所不同。除非您将其配置为在相同的 url 上运行。但否则/snservice 应替换为/&lt;name-of-war&gt; 之类的/snservice-443-async,因为它将(默认情况下)部署在与存档同名的url上)。
  • 非常感谢,我在 Url 中使用了那个战争名称,现在可以了。现在可以使用了。

标签: java spring spring-boot


【解决方案1】:

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

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

我已经使用下面给出的网站(逐步解释)将 jar 转换为 spring boot。

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

【讨论】:

  • 与@Rupendra Sharma 的回答有什么不同?抄袭
【解决方案2】:

如果你使用的是 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);
    }

}

【讨论】:

  • 感谢您的支持,但我仍然有同样的问题,404。无法访问控制器包中的服务。我还添加了 pom.xml。请检查一次。
  • 嗨@VijayS,尝试添加 SpringBootServletInitializer
  • 我也有同样的问题。我已经尝试了 SO 中的所有解决方案,但没有任何改变。控制权进入班级,但只有 .jsp 文件未呈现。请任何人帮助我。
猜你喜欢
  • 1970-01-01
  • 2012-01-16
  • 1970-01-01
  • 2015-09-16
  • 2017-05-17
  • 2017-12-23
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
相关资源
最近更新 更多