Dun*_*nes 29 java tomcat spring-mvc maven spring-boot
在使用Spring MVC提供Web内容的指导下,我正在创建一个Spring Boot Web应用程序,我可以使用嵌入式Tomcat实例以及独立的Tomcat 8服务器运行它.
执行时应用程序按预期工作java -jar adminpage.war,我在访问时看到了预期的结果http://localhost:8080/table.但是,当我部署到Tomcat 8服务器(通过adminpage.war放入webapps目录)时,我访问时出现404错误https://myserver/adminpage/table.
在catelina.log和localhost.log文件包含在Tomcat服务器上没有什么帮助.
任何人都可以建议我在哪里做了错误的配置?我在过去使用Spring Boot部署RESTful服务时遇到了同样的问题,但这是我第一次涉足Web应用程序.
我的申请文件:
src/main/java/com/.../Application.java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
src/main/java/com/.../MainController.java
@Controller
public class MainController {
@RequestMapping("/table")
public String greeting(Model model) {
model.addAttribute("name", "Fooballs");
return "table";
}
}
Run Code Online (Sandbox Code Playgroud)
src/main/resources/templates/table.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
pom.xml
<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.foo</groupId>
<artifactId>adminpage</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<finalName>adminpage</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
Dun*_*nes 64
我忘了调整我的Application.java文件来扩展SpringBootServletInitializer和覆盖configure方法.
更正文件:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
}
Run Code Online (Sandbox Code Playgroud)
帽子提示https://mtdevuk.com/2015/07/16/how-to-make-a-spring-boot-jar-into-a-war-to-deploy-on-tomcat/指出我的错误.
有关在Spring Boot官方文档中创建可部署war文件的更多信息.
小智 18
截至 2021 年 8 月。如果有人在 Tomcat 10 服务器中遇到此问题,请阅读下面的文章,Spring boot 无法在 Tomcat 10 中工作。 https://github.com/spring-projects/spring-boot/issues/22414
我遇到过这个问题并切换到以前的版本,这解决了问题。
| 归档时间: |
|
| 查看次数: |
21218 次 |
| 最近记录: |