部署到Glassfish的Spring Boot应用程序给出了奇怪的结果 [英] Spring Boot app deployed to Glassfish is giving strange results

查看:125
本文介绍了部署到Glassfish的Spring Boot应用程序给出了奇怪的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处所述,我花了一点时间让我的小型Spring-Boot项目正确"部署到Glassfish.使用嵌入式Tomcat可以很好地运行它,但是一旦我尝试将其移入组织的环境(Glassfish 3.1.2)中,就会出现一些奇怪的行为.

As mentioned here, I am having a heck of a time getting my small Spring-Boot project to deploy "correctly" to Glassfish. It runs fine using the embedded Tomcat, but once I try and move it into my organization's environment (Glassfish 3.1.2) I get some strange behavior.

以为这是我的代码,我恢复了经过时间考验的"Hello World"方法,并按照

Thinking it was my code, I reverted to the time-tested "Hello World"-approach and built a super-basic app following this tutorial on Spring's blog.

在进行过程中,我确实做了一些很小的改动,但丝毫没有影响到此应用程序的东西.

I did make a few very minor deviations as I went along but nothing that should have affected the app like this at all.

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

The only major deviation I made was that I found I could not exclude "spring-boot-starter-tomcat" from "spring-boot-starter-web" -- when I tried to that, I got 2 errors in the STS "Markers"-tab:

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清理,更新,安装安装目标,则会出现以下错误:

If I cleaned the STS project, then ran Maven Clean, Update, Install the Install goal gave the following error:

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资源中发现了这种依赖关系):

So what I did instead was include this dependency (which I found mentioned in several other SpringBoot resources):

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

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

In this deployed fine to the embedded Tomcat and it did deploy to my Glassfish (local install) -- but with a whole bunch (about a half-dozen) errors similar to this one:

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 ]

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

Most of them are SEVERE, but I am also getting a few with a WARNING:

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

除了我没有在项目中的任何地方引用这些丢失的类中的任何一个(除了Spring Boot本身可能引用的那些之外).

Except that I'm not referencing any of these missing classes anywhere in my project (other than what might be referenced by Spring Boot itself).

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

Also, the app doesn't quite work as expected. If I hit the RestController, I do get my page rendered as I expect -- but if I put any sort of System.out or Logger.log statement in the controller's method, that line of code seemingly never gets executed; by all appearances, it just gets skipped.

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

To demonstrate this problem, in my sample app's RestController I created a static counter. Then in the GET-/ method I increment that counter and System.out.println it's value. I also return the value as part of the Response.

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

And again, from a user's perspective, it seems to be working: the screen renders "Hello World" and in parentheses it shows the counter's value. I refresh the window, the counter increments. But nothing in the STS Console. And if I navigate to the Glassfish log for the app, nothing there either. Nothing. Nada. Zip. From what I can tell, something is mysteriously eating any attempt to log anything.

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

To add to the mystery, if I add a System.out to the SpringBootServletInitializer#configure(), that does make it to the Console. But if I declare a constructor in my RestController and include a System.out there, that one does not make it to the Console. For good measure, I even tried including a System.err in the constructor and a Logger.getAnonymousLogger.severe in the method; neither of those result in anything.

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

I should note that this also deploys and runs as expected using an external Tomcat.

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

I would very much appreciate any input, as it's not likely that I can convince my organization to deploy this to Tomcat nor use the embedded-Tomcat approach (due to politics and an overwhelming existing Glassfish environment).

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

推荐答案

此问题已在此处得到解答: https://stackoverflow.com /a/29438821/508247

This has been answered here: https://stackoverflow.com/a/29438821/508247

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

There is a bug in Glassfish 3.1.X. You need to include metadata-complete="true" in your web.xml root element.

<?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的Spring Boot应用程序给出了奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆