小编典典

部署到Glassfish的Spring Boot应用程序给出了奇怪的结果

spring-boot

如前所述在这里,我有一个时间让我的小弹簧引导项目部署“正确”到GlassFish赫克。使用嵌入式Tomcat可以很好地运行它,但是一旦我尝试将其移入组织的环境(Glassfish
3.1.2)中,就会出现一些奇怪的行为。

以为这是我的代码,我回到了经过时间考验的“ Hello
World”方法,并根据Spring博客上的本教程构建了一个超级基础的应用程序。

进行过程中,我确实做了一些很小的改动,但没有任何事情会像这样影响应用程序。

我唯一的主要偏差是我发现我无法从“ spring-boot-starter-web”中排除“ spring-boot-starter-
tomcat”-尝试这样做时,我在STS“ Markers”中遇到2个错误“-标签:

The project was not built since its build path is incomplete. Cannot find the class file for javax.servlet.ServletContext. Fix the build path then try building this project    
The type javax.servlet.ServletContext cannot be resolved. It is indirectly referenced from required .class files    Application.java

如果我清理了STS项目,然后运行了Maven清理,更新,安装安装目标,则会出现以下错误:

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project test: Compilation failure [ERROR] /Users/brandon_utah/Utah Development/sts_workspaces/NidTools Rebooted/test/src/main/java/test/Application.java:[13,8] cannot access javax.servlet.ServletException [ERROR] class file for javax.servlet.ServletException not found

因此,我要做的是包括此依赖关系(我在其他一些SpringBoot资源中提到了该依赖关系):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId> 
    <scope>provided</scope>     
</dependency>

在这种情况下,嵌入式Tomcat可以很好地部署,并且确实可以部署到我的Glassfish(本地安装)中-但有很多错误(大约六个),与此类似:

2014-04-03T16:23:48.156-0600|SEVERE: Class [ Lorg/springframework/jdbc/datasource/embedded/EmbeddedDatabase; ] not found. Error while loading [ class org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration ]

其中大多数是严重的,但我也会警告一些:

2014-04-04T06:57:35.921-0600|WARNING: Error in annotation processing: java.lang.NoClassDefFoundError: org/springframework/batch/core/configuration/annotation/BatchConfigurer

除了我没有在项目的任何地方引用任何这些缺少的类(Spring Boot本身可能引用的不是)。

另外,该应用程序无法按预期运行。如果我点击了RestController,我的页面就会按照预期的方式渲染-
但是,如果我在控制器的方法中放入任何System.out或Logger.log语句,那行代码似乎永远不会执行;从所有方面来看,它只是被跳过。

为了演示此问题,在示例应用程序的RestController中,我创建了一个静态计数器。然后在GET-
/方法中,我将该计数器和System.out.println的值递增。我还将值作为响应的一部分返回。

再一次,从用户的角度来看,它似乎正在起作用:屏幕呈现“ Hello
World”,并在括号中显示计数器的值。我刷新窗口,计数器增加。但是STS控制台中没有任何内容。而且,如果我导航至该应用程序的Glassfish日志,也没有任何内容。没有。娜达
压缩。据我所知,某种东西正在神秘地吞噬着记录任何东西的企图。

为了增加神秘性,如果我将System.out添加到SpringBootServletInitializer#configure(),则确实会将它添加到控制台。但是,如果我在RestController中声明一个构造函数并在其中包含System.out,则该构造函数不会进入控制台。为了达到良好的效果,我什至尝试在构造函数中包含System.err,在方法中包含Logger.getAnonymousLogger.severe。这些都不会导致任何结果。

我应该注意,这也可以使用外部Tomcat正常部署和运行。

我会非常感激任何输入,因为我不太可能说服我的组织将其部署到Tomcat或使用嵌入式Tomcat方法(由于政治原因和不堪重负的现有Glassfish环境)。

我在Github上的测试项目在这里


阅读 551

收藏
2020-05-30

共1个答案

小编典典

Glassfish 3.1.X中存在一个错误。您需要metadata-complete="true"在web.xml根元素中包含它。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 
     metadata-complete="true"
     xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
</web-app>
2020-05-30