首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Boot应用程序部署到Glassfish会产生奇怪的结果

Spring Boot应用程序部署到Glassfish会产生奇怪的结果
EN

Stack Overflow用户
提问于 2014-04-04 21:40:25
回答 2查看 10.3K关注 0票数 6

正如前面提到的here,让我的小Spring-Boot项目“正确”地部署到Glassfish上,我正在经历一段艰难的时间。它使用嵌入式Tomcat运行得很好,但是当我尝试将它移到我组织的环境(Glassfish 3.1.2)中时,我得到了一些奇怪的行为。

考虑到这是我的代码,我恢复到了久经考验的"Hello World"-approach,并按照this tutorial on Spring's blog构建了一个超级基础的应用程序。

我确实做了一些非常小的偏差,但没有什么应该像这样影响到应用程序。

我唯一的主要偏差是我发现我不能从"spring-boot-starter-web“中排除"spring-boot-starter-tomcat”--当我尝试这样做时,我在STS“标记”-tab中得到了两个错误:

代码语言:javascript
复制
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,则Install目标会显示以下错误:

代码语言:javascript
复制
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资源中提到了这个依赖项):

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId> 
    <scope>provided</scope>     
</dependency>

在这个示例中,可以很好地部署到嵌入式Tomcat,并且它确实部署到了我的Glassfish (本地安装)上--但有一大堆(大约半打)错误,类似于下面的一个:

代码语言:javascript
复制
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 ]

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

代码语言:javascript
复制
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日志,也什么都没有。没什么。什么都没有。拉链。据我所知,有某种东西神秘地吞噬了任何试图记录任何东西的行为。

更令人费解的是,如果我在SpringBootServletInitializer#configure()中添加了一个System.out,那么它就会进入控制台。但是,如果我在RestController中声明了一个构造函数并在其中包含了一个System.out,那么这个构造函数就不能到达控制台。另外,我甚至尝试在构造函数中包含一个System.err,在方法中包含一个Logger.getAnonymousLogger.severe;但这两种方法都没有任何结果。

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

我非常感谢任何意见,因为我不太可能说服我的组织将其部署到Tomcat,也不会使用embedded-Tomcat方法(由于政治和压倒性的现有Glassfish环境)。

My test project on Github is here

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-05 07:46:45

答案在这里:https://stackoverflow.com/a/29438821/508247

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

代码语言:javascript
复制
<?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>
票数 4
EN

Stack Overflow用户

发布于 2019-07-19 00:13:30

我在Payara 5上遇到了这个问题,我知道这个问题是由Glassfish引起的。

版本:

  1. Payara 5.192
  2. Spring Boot 2.1.6

这个解决方案对我很有效:

我在pom.xml中添加了这个依赖项。

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-batch</artifactId>
    <version>2.1.4.RELEASE</version>
</dependency>

我的glassfish-web配置:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC ...>
<glassfish-web-app error-url="">
    <class-loader delegate="true"/>
    <jsp-config>
        <property name="keepgenerated" value="true">
          <description>Keep a copy of the generated servlet class' java code.</description>
        </property>
      </jsp-config>
    <!-- set a friendly context root -->
    <context-root>/micuenta-api</context-root>
    <!-- Change the default character encoding from ISO-8859-1 to UTF-8 -->
    <parameter-encoding default-charset="UTF-8"/>
</glassfish-web-app>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22864509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档