英文:
Deploy Spring App to Tomcat without web.xml
问题
我已经成功地使用嵌入的TOMCAT测试了我的war文件,并且它正常工作。现在我想将其部署到TOMCAT,但我一直得到404错误。
我在浏览器中导航到的URL是
http://localhost:9999/springrest/demo/users/all
我的TOMCAT使用端口9999
我的JRE_HOME版本是1.8.0_261
我的pom.xml中有**<java.version>1.8</java.version>**
我的控制器代码如下
@RestController
@RequestMapping(path="/demo")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping(path="/users/all")
public @ResponseBody Iterable<User> getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();
}
}
我的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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.eptc</groupId>
<artifactId>springrest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springrest</name>
<description>Rest API for mobile app</description>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
<start-class>com.eptc.springrest.SpringrestApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
英文:
I have successfully tested my war file with embedded TOMCAT and it works fine. Now I want to deploy it to TOMCAT but I keep getting a 404 error.
The URL I navigate to on the browser is
http://localhost:9999/springrest/demo/users/all
My TOMCAT uses the port 9999
My JRE_HOME version is 1.8.0_261
My pom.xml has <java.version>1.8</java.version>
TOMCAT manager shows the app as running
My Controller code is as follows
@RestController
@RequestMapping(path="/demo")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping(path="/users/all")
public @ResponseBody Iterable<User> getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();
}
}
My 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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.eptc</groupId>
<artifactId>springrest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springrest</name>
<description>Rest API for mobile app</description>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
<start-class>com.eptc.springrest.SpringrestApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
答案1
得分: 0
你是否通过实现SpringBootServletInitializer接口来初始化Tomcat所需的Servlet上下文?
@SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
}
请参考此链接获取详细信息:将Spring Boot WAR部署到Tomcat服务器
英文:
Have you initialized the Servlet context required by Tomcat by implementing the SpringBootServletInitializer interface?
@SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
}
Please refer this link for the details :Deploy a Spring Boot WAR into a Tomcat Server
答案2
得分: 0
将我的 Apache Tomcat 版本 更改为 Apache Tomcat 8.5.58,然后它就能正常运行。
在运行 catalina.bat run 时,甚至在 cmd 上显示了 Springboot 横幅。
英文:
Changed my Apache Tomcat version to Apache Tomcat 8.5.58 and it worked like a charm.
The Springboot banner even shows on cmd on running catalina.bat run
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论