gpt4 book ai didi

java - 无法使用嵌入式 Tomcat 浏览到 Spring Boot 应用程序

转载 作者:行者123 更新时间:2023-12-02 01:52:49 35 4
gpt4 key购买 nike

我正在开发一个相当大的 MVC 项目,该项目过去只使用 spring MVC 而不是 spring boot 运行(完美)。我已将其转换为 Spring Boot,并且它在 Netbeans 中完美运行,并且在我的开发计算机上作为 Netbeans 之外的独立 JAR 运行时。

我将 JAR 文件传输到其目标服务器,一切都根据控制台正常启动,我收到以下消息:Tomcat 在端口上启动:8081 (HTTP),上下文路径为“/MDHIS”。但是,我无法像在我的计算机上使用以下 URL 一样浏览它:http://localhost:8081/MDHIS/

我将 tomcat 设置为 Debug模式,它记录了一些有趣的事情:

[WEBCLIENT] [DEBUG] [2018-10-15 14:44:26.269] o.a.j.s.JspServlet                       
: JspEngine --> /WEB-INF/views/login.jsp
[WEBCLIENT] [DEBUG] [2018-10-15 14:44:26.270] o.a.j.s.JspServlet
: ServletPath: /WEB-INF/views/login.jsp
[WEBCLIENT] [DEBUG] [2018-10-15 14:44:26.270] o.a.j.s.JspServlet
: PathInfo: null
[WEBCLIENT] [DEBUG] [2018-10-15 14:44:26.272] o.a.j.s.JspServlet
: RealPath: C:\Users\Administrator\AppData\Local\Temp\tomcat-
docbase.4901819852507265388.8080\WEB-INF\views\login.jsp
[WEBCLIENT] [DEBUG] [2018-10-15 14:44:26.273] o.a.j.s.JspServlet
: RequestURI: /MDHIS/WEB-INF/views/login.jsp
[WEBCLIENT] [DEBUG] [2018-10-15 14:44:26.274] o.a.j.s.JspServlet
: QueryString: null
[WEBCLIENT] [DEBUG] [2018-10-15 14:44:26.275] o.a.c.c.C.[.[.[.[jsp]
: Disabling the response for further output
[WEBCLIENT] [DEBUG] [2018-10-15 14:44:26.277] o.a.c.c.C.[.[localhost]
: Processing ErrorPage[errorCode=0, location=/error]
[WEBCLIENT] [DEBUG] [2018-10-15 14:44:26.315] o.a.j.s.JspServlet
: JspEngine --> /WEB-INF/views/error.jsp
[WEBCLIENT] [DEBUG] [2018-10-15 14:44:26.315] o.a.j.s.JspServlet
: ServletPath: /WEB-INF/views/error.jsp
[WEBCLIENT] [DEBUG] [2018-10-15 14:44:26.316] o.a.j.s.JspServlet
: PathInfo: null
[WEBCLIENT] [DEBUG] [2018-10-15 14:44:26.317] o.a.j.s.JspServlet
: RealPath: C:\Users\Administrator\AppData\Local\Temp\tomcat-
docbase.4901819852507265388.8080\WEB-INF\views\error.jsp
[WEBCLIENT] [DEBUG] [2018-10-15 14:44:26.318] o.a.j.s.JspServlet
: RequestURI: /MDHIS/WEB-INF/views/error.jsp
[WEBCLIENT] [DEBUG] [2018-10-15 14:44:26.319] o.a.j.s.JspServlet
: QueryString: null
[WEBCLIENT] [DEBUG] [2018-10-15 14:44:26.319] o.a.c.c.C.[.[.[.[jsp]
: Disabling the response for further output
[WEBCLIENT] [DEBUG] [2018-10-15 14:44:26.320] o.a.c.c.C.[.[.[.
[dispatcherServlet] : Disabling the response for further output

我观察了 docbase 文件夹,并且没有向其中添加任何 JSP 或类。在不同的环境/操作系统/网络上,我的工作结果参差不齐。有人见过这种行为吗?

*****编辑*****

根据要求的 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.mdenis</groupId>
<artifactId>MDHIS_Common</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>

<dependencies>

<!--JAVA-->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>

<!--SPRING-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

<!--MICROSOFT-->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>

<!--HIBERNATE-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>

<!--HAPI-->
<dependency>
<groupId>ca.uhn.hapi</groupId>
<artifactId>hapi-base</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>ca.uhn.hapi</groupId>
<artifactId>hapi-structures-v26</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>ca.uhn.hapi</groupId>
<artifactId>hapi-structures-v25</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>ca.uhn.hapi</groupId>
<artifactId>hapi-structures-v24</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>ca.uhn.hapi</groupId>
<artifactId>hapi-structures-v231</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>ca.uhn.hapi</groupId>
<artifactId>hapi-structures-v23</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>ca.uhn.hapi</groupId>
<artifactId>hapi-structures-v22</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>ca.uhn.hapi</groupId>
<artifactId>hapi-structures-v21</artifactId>
<version>2.3</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>MDHIS_Common</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</plugin>
</plugins>
</build>
</project>

网络客户端:

<?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.mdenis</groupId>
<artifactId>MDHIS_WebClient</artifactId>
<version>1.0-SNAPSHOT</version>
<!--<packaging>war</packaging>-->

<name>MDHIS_WebClient</name>

<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>

<dependencies>

<!--JAVA-->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.json.bind</groupId>
<artifactId>javax.json.bind-api</artifactId>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

<!--GLASSFISH-->
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.3</version>
</dependency>

<!--TOMCAT-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>

<!--JASPER REPORTS-->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.7.0</version>
</dependency>

<!--SIGAR-->
<dependency>
<groupId>org.fusesource</groupId>
<artifactId>sigar</artifactId>
<version>1.6.4</version>
</dependency>

<!--LOG4J2-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jul</artifactId>
</dependency>

<!--YASSON-->
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>

<!--MDHIS_COMMON-->
<dependency>
<groupId>com.mdenis</groupId>
<artifactId>MDHIS_Common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

</dependencies>

<repositories>

<repository>
<id>java.net-Public</id>
<name>Maven Java Net Snapshots and Releases</name>
<url>https://maven.java.net/content/groups/public/</url>
</repository>
<repository>
<id>yasson-snapshots</id>
<name>Yasson Snapshots repository</name>
<url>https://repo.eclipse.org/content/repositories/yasson-snapshots</url>
</repository>

</repositories>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>

结论

感谢所有提供帮助的人!虽然当打包为 JAR 文件时它在某些系统上运行良好,但对 WAR 文件的更改才是修复它的原因。遗憾的是,我无法再在 Netbeans 中运行该项目,因为它无法识别 Tomcat 服务器嵌入在其自身中,但在之前无法工作的 2 个系统(Windows Server 2016 和 Ubuntu LTS 18.04)上使用 java -jar 时,一切都运行良好。

谢谢

最佳答案

面对此类问题,我会探究本地计算机(应用程序运行良好)和服务器(同一应用程序出现问题)之间可能存在环境差异。我首先要检查的几件事如下:

  1. 检查 JSP 是否已编译到 Tomcat 的工作目录。
  2. 检查 Tomcat 拾取的 JAVA_HOME 变量。
  3. 检查计算机上是否确实安装了 JDK(而不仅仅是 JRE)。
  4. 检查为 Tomcat 配置的 Java 版本是否受使用的 Spring Boot 版本支持。
  5. 检查有效pom中具有范围的依赖关系“提供”、“运行时”或“系统”。

但首先,请参阅这些 JSP Limitations with Spring Boot并注意,您需要使用 war 打包——使用可执行 jar 时不支持 JSP。此可执行 war 将在使用 java -jar 启动时运行。 ,并且还可以部署到任何标准容器。

要创建 war 包装,请包含 <packaging>war</packaging>在声明工件 ID 的 pom 中。

您可能会找到这个Spring Boot JSP sample有用。这个another sample也可能有帮助。

希望以上意见能带来解决方案。

关于java - 无法使用嵌入式 Tomcat 浏览到 Spring Boot 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52703965/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com