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

标签 glassfish-3 spring-boot spring-tool-suite

如前所述 here ,我花了很长时间让我的小型 Spring-Boot 项目“正确”部署到 Glassfish。它使用嵌入式 Tomcat 运行良好,但是一旦我尝试将其移动到我组织的环境(Glassfish 3.1.2)中,我就会出现一些奇怪的行为。

认为这是我的代码,我恢复到耗时考验的“Hello World”方法,并在 this tutorial on Spring's blog 之后构建了一个 super 基本的应用程序.

在进行过程中,我确实做了一些非常小的偏差,但根本不应该像这样影响应用程序。

我所做的唯一主要偏差是我发现我无法从“spring-boot-starter-web”中排除“spring-boot-starter-tomcat”——当我尝试这样做时,我在 STS“标记”中遇到了 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 Clean、Update、Install 安装目标会出现以下错误:
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,我确实会按照我的预期呈现我的页面——但是如果我在 Controller 的方法中放入任何类型的 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 环境)。

My test project on Github is here .

最佳答案

已在此处回答:https://stackoverflow.com/a/29438821/508247

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>

关于glassfish-3 - 部署到 Glassfish 的 Spring Boot 应用程序给出了奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22864509/

相关文章:

java - 什么是让 <jsp-config> 标签工作的标准 web.xml java header ?

java - 如何禁用在 Spring 中使用 @Component 注释创建 bean?

java - STS v3.5.0.RELEASE x64 不会热交换 VM 类

java - Spring MVC、Maven、Pivo​​tal tc 服务器 HTML 404 未找到错误

java - 如何在新的STS 3.7.2中运行旧的STS 2.8.0 GWT SDK项目?

glassfish - 亚马逊 EC2 : How install glassfish in EC2?

java - 查找 EJB 组件的正确方法是什么?

jsf-2 - 我如何使用@ConversationScoped

java - 带有 JWT 的 Spring Security for REST API

postgresql - 使用 jOOQ 更新到 Postgres 表时返回更新行数