我开发了一个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
以上网址运行良好。
我的应用程序包含不同的包,如控制器(Controller),服务(Service)和存储库(Repo)。我已经添加了项目结构的截图。
问题是我无法访问控制器(Controller)包中的服务。我需要为此配置DispatcherServlet吗?有人可以帮帮我吗?
@SpringBoot
注解时,您不需要再使用@ComponentScan
或@EnableAutoConfiguration
注解,因为它们已经被隐含在其中。您的应用程序将自动检测子包中的所有内容。您在pom.xml
中具体做了什么更改?是否按照步骤中参考指南所述操作?(仍需要spring-boot-maven-plugin
,且spring-boot-starter-tomcat
依赖应该被限定为provided
作用域。) - M. Deinum/snservice
应该被替换为/<name-of-war>
,例如/snservice-443-async
,因为它将(默认情况下)部署在与存档文件名相同的 URL 上。 - M. Deinum