Spring Boot 应用程序在 IDE 中运行时工作,但当部署为 war 文件时

好吧,我在这里感觉有点傻,因为我想不通,所以就开始吧。

我从 Spring Initializer 下载了最基本的 Hello World spring boot web 应用程序。我可以使用内部 tomcat 让它正常运行。但是当我打包到一个 .war 文件并部署到我的本地 tomcat 实例时,它模拟了生产环境。该应用程序失败。我已经尝试系统地通过文档更改配置以确保上下文正确,并且基本上我能找到的任何其他内容都可以解决这个问题。到目前为止没有骰子,所以我伸出手来看看这里是否有人有一些见识。

该应用程序应从 URL http://localhost:8084/clariovista/返回“TEST CLARIOVISTA”

当我从我的 IDE (Eclipse) 运行它时没有问题。但是当我将 .war 部署到本地 tomcat 实例时,它会抛出 500 错误消息:

消息请求处理失败;嵌套异常是 org.thymeleaf.exceptions.TemplateInputException:解析模板时出错 [错误],模板可能不存在或可能无法被任何已配置的模板解析器访问

目录树已正确设置。我没有更改 Spring Initializer 输出给出的目录结构。所以我不确定为什么模板位置有问题。除非那是一条红鲱鱼,还有其他事情正在发生。

非常感谢任何帮助或见解。

基础知识:SpringBoot 2.1.6.Release,JDK 1.8,Tomcat 8.5.27。

目录树图片。

http://img4.mukewang.com/64141f8f0001b9c102380398.jpg

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>

<parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>2.1.6.RELEASE</version>

    <relativePath /> <!-- lookup parent from repository -->

</parent>

<groupId>com.inferworks</groupId>

<artifactId>clariovista</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>war</packaging>

<name>clariovista</name>

<description>Clario Vista Web</description>

<organization>

    <name>Inferworks</name>

    <url>Inferworks.com</url>

</organization>


<properties>

    <java.version>1.8</java.version>

</properties>


<dependencies>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-thymeleaf</artifactId>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

    </dependency>


饮歌长啸
浏览 123回答 3
3回答

元芳怎么了

使用 jar 文件很容易,命令:maven install<plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <mainClass>com.dh.Application</mainClass>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <jvmArguments>-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </jvmArguments>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>repackage</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>

慕桂英546537

根据您的问题描述,我希望不需要百里香。您可以通过 2 种方式进行。如果确实不需要,则从 pom.xml 中删除 thymeleaf 模板文件夹和 thymeleaf 依赖项并继续。你想继续使用 thymeleaf,请添加下面的类并继续部署并尝试。@Configurationpublic class ThymeleafConfiguration {&nbsp; &nbsp; private static final String HTML_5 = "HTML5";&nbsp; &nbsp; @Bean&nbsp; &nbsp; public SpringTemplateEngine templateEngine() {&nbsp; &nbsp; &nbsp; &nbsp; final SpringTemplateEngine templateEngine = new SpringTemplateEngine();&nbsp; &nbsp; &nbsp; &nbsp; templateEngine.addTemplateResolver(templateResolver());&nbsp; &nbsp; &nbsp; &nbsp; return templateEngine;&nbsp; &nbsp; }&nbsp; &nbsp; @Bean&nbsp; &nbsp; public ThymeleafViewResolver viewResolver(){&nbsp; &nbsp; &nbsp; &nbsp; final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();&nbsp; &nbsp; &nbsp; &nbsp; viewResolver.setTemplateEngine(templateEngine());&nbsp; &nbsp; &nbsp; &nbsp; viewResolver.setViewNames(new String[]{"*.html,*.xhtml,*.txt"});&nbsp; &nbsp; &nbsp; &nbsp; return viewResolver;&nbsp; &nbsp; }&nbsp; &nbsp; @Bean&nbsp; &nbsp; public SpringResourceTemplateResolver templateResolver() {&nbsp; &nbsp; &nbsp; &nbsp; // SpringResourceTemplateResolver automatically integrates with Spring's&nbsp; &nbsp; &nbsp; &nbsp; // own resource resolution infrastructure, which is highly recommended.&nbsp; &nbsp; &nbsp; &nbsp; final SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();&nbsp; &nbsp; &nbsp; &nbsp; // templateResolver.setApplicationContext(this.applicationContext);&nbsp; &nbsp; &nbsp; &nbsp; templateResolver.setPrefix("classpath:templates/");&nbsp; &nbsp; &nbsp; &nbsp; templateResolver.setSuffix(".html");&nbsp; &nbsp; &nbsp; &nbsp; // HTML is the default value, added here for the sake of clarity.&nbsp; &nbsp; &nbsp; &nbsp; templateResolver.setTemplateMode(HTML_5);&nbsp; &nbsp; &nbsp; &nbsp; templateResolver.setCharacterEncoding(CharEncoding.UTF_8);&nbsp; &nbsp; &nbsp; &nbsp; // Template cache is true by default. Set to false if you want&nbsp; &nbsp; &nbsp; &nbsp; // templates to be automatically updated when modified.&nbsp; &nbsp; &nbsp; &nbsp; templateResolver.setCacheable(true);&nbsp; &nbsp; &nbsp; &nbsp; return templateResolver;&nbsp; &nbsp; }}

POPMUISE

因为默认spring-boot-starter-web包含 Tomcat 。 如果你想在其他 web 容器中运行你的应用程序,你应该为 Spring MVC 排除 Tomcat,如下所示: spring-boot-starter-tomcat<dependency>&nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; <artifactId>spring-boot-starter-web</artifactId>&nbsp; &nbsp; <exclusions>&nbsp; &nbsp; &nbsp; &nbsp; <!-- Exclude the Tomcat dependency -->&nbsp; &nbsp; &nbsp; &nbsp; <exclusion>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-tomcat</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </exclusion>&nbsp; &nbsp; </exclusions></dependency>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java