通过使用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。
java -jar adminpage.war
http://localhost:8080/table
adminpage.war
webapps
https://myserver/adminpage/table
在catelina.log和localhost.log文件包含在Tomcat服务器上没有什么帮助。
catelina.log
localhost.log
谁能建议我在哪里配置错误?过去使用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); } }
src/main/java/com/.../MainController.java
@Controller public class MainController { @RequestMapping("/table") public String greeting(Model model) { model.addAttribute("name", "Fooballs"); return "table"; } }
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>
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>
我忘了调整Application.java文件以扩展SpringBootServletInitializer和覆盖该configure方法。
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); } }
提示我https://mtdevuk.com/2015/07/16/how-to-make-a-spring-boot-jar-into-a-war- to-deploy-on-tomcat/的提示。
在Spring Boot Official docs 中创建可部署的war文件中的更多信息。