gpt4 book ai didi

java - 将 spring boot jar 转换为 war

转载 作者:搜寻专家 更新时间:2023-11-01 03:31:44 24 4
gpt4 key购买 nike

我开发了一个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 包中的服务。我需要为此配置调度程序 Servlet 吗???? .有人可以帮帮我吗?

最佳答案

如果你使用的是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 子类并覆盖其配置方法。

@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);
}

}

关于java - 将 spring boot jar 转换为 war,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50462585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com