Java Jboss 7 上的泽西岛 2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21173397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jersey 2 on Jboss 7
提问by Minh Nguyen
Has anyone had success deploying Jersey 2.x with JBoss 7.x? I've tried deploying Jersey 2.5 with JBoss 7.1.1 but encountered errors like:
有没有人成功地使用 JBoss 7.x 部署 Jersey 2.x?我曾尝试使用 JBoss 7.1.1 部署 Jersey 2.5,但遇到如下错误:
"java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;"
I believe this issue is because JBoss comes bundled with RestEasy which is a JAX-RS 1.0 implementation while Jersey is a JAX-RS 2.0 implementation. So I took the following steps to disable RestEasy:
我相信这个问题是因为 JBoss 与 RestEasy 捆绑在一起,后者是 JAX-RS 1.0 实现,而 Jersey 是 JAX-RS 2.0 实现。所以我采取了以下步骤来禁用 RestEasy:
1) Added the following to my web.xml:
1) 在我的 web.xml 中添加了以下内容:
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.providers</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.resources</param-name>
<param-value>false</param-value>
</context-param>
2) Followed the discussion here, I modified my JBoss' standalone.xml, module.xml, and domain.xml to remove all references to JAXRS1.1 / RestEasy.
2) 按照这里的讨论,我修改了我的 JBoss 的 standalone.xml、module.xml 和 domain.xml 以删除对 JAXRS1.1/RestEasy 的所有引用。
3) This led to another error: "java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor" which I resolved by adding the following to my pom.xml:
3)这导致了另一个错误:“java.lang.NoClassDefFoundError:org/objectweb/asm/ClassVisitor”,我通过将以下内容添加到我的 pom.xml 中解决了该错误:
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
So finally my app deploys without errors but now I cannot seem to access any of my Jersey resources. Jetty 8 works fine, however. I also was able to run Jersey 1.x without having to take steps #2 and #3 but I would prefer to use Jersey 2.x if possible.
所以最后我的应用程序部署没有错误,但现在我似乎无法访问我的任何 Jersey 资源。但是,Jetty 8 运行良好。我还可以运行 Jersey 1.x,而无需执行步骤 #2 和 #3,但如果可能的话,我更愿意使用 Jersey 2.x。
Additionally I've also tried creating a jboss-deployment-structure.xml file, but then I still encounter the previous errors like "java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;"
此外,我还尝试创建一个 jboss-deployment-structure.xml 文件,但是我仍然遇到了以前的错误,例如“java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/地图;”
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="org.jboss.resteasy.resteasy-atom-provider"/>
<module name="org.jboss.resteasy.resteasy-cdi"/>
<module name="org.jboss.resteasy.resteasy-Hymanson-provider"/>
<module name="org.jboss.resteasy.resteasy-jaxb-provider"/>
<module name="org.jboss.resteasy.resteasy-jaxrs"/>
<module name="org.jboss.resteasy.resteasy-jettison-provider"/>
<module name="org.jboss.resteasy.resteasy-jsapi"/>
<module name="org.jboss.resteasy.resteasy-multipart-provider"/>
<module name="org.jboss.resteasy.resteasy-yaml-provider"/>
<module name="org.apache.log4j"/>
<module name="org.apache.commons.pool"/>
<module name="javax.ws.rs.api"/>
</exclusions>
</deployment>
</jboss-deployment-structure>
Has anyone had any luck with Jboss and Jersey 2.x? Any help would be appreciated.
有没有人对 Jboss 和 Jersey 2.x 有好运?任何帮助,将不胜感激。
采纳答案by Clemens82
You can use Jersey 2 on JBoss 7 if you configure your jboss-deployment-structure.xmlsimilar to this:
如果您像这样配置jboss-deployment-structure.xml,您可以在 JBoss 7 上使用 Jersey 2 :
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<exclude-subsystems>
<subsystem name="resteasy" />
</exclude-subsystems>
<exclusions>
<module name="javaee.api" />
<module name="javax.ws.rs.api"/>
<module name="org.jboss.resteasy.resteasy-jaxrs" />
</exclusions>
<local-last value="true" />
</deployment>
</jboss-deployment-structure>
Because JBoss 7 includes dependencies of modules, it is not sufficient to exclude the resteasy module itself but you need to exclude the whole javaee.api module. Also make sure to not exclude too many modules. This may also break your application - the example above is sufficient to disable resteasy.
由于 JBoss 7 包含模块的依赖关系,因此排除 resteasy 模块本身是不够的,您需要排除整个 javaee.api 模块。还要确保不要排除太多模块。这也可能会破坏您的应用程序 - 上面的示例足以禁用 resteasy。
As you already discovered, you still need to include the following lines in your web.xml
正如您已经发现的,您仍然需要在web.xml 中包含以下几行
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.providers</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>resteasy.scan.resources</param-name>
<param-value>false</param-value>
</context-param>
回答by Tomaz Cerar
This is similar to this question
这类似于这个问题
So bit of copy/paste with just different subsystem at play.
只是在不同的子系统中进行了一些复制/粘贴。
You should exclude jaxrs subsystem from being activated for your deployment add this into META-INF/jboss-deployment-structure.xml
您应该排除 jaxrs 子系统为您的部署激活将其添加到 META-INF/jboss-deployment-structure.xml
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<!-- exclude-subsystem prevents a subsystems deployment unit processors running on a deployment -->
<!-- which gives basically the same effect as removing the subsystem, but it only affects single deployment -->
<exclude-subsystems>
<subsystem name="jaxrs" />
</exclude-subsystems>
<!-- This is needed in any case even if you don't use exclude-subsystem above-->
<exclusions>
<module name="javax.ws.rs.api" />
</exclusions>
<deployment>
</jboss-deployment-structure>
or you can go to standalone.xml and remove subsystem there. To do so, you need to remove
或者您可以转到 standalone.xml 并在那里删除子系统。为此,您需要删除
<subsystem xmlns="urn:jboss:domain:jaxrs:1.x">
...
...
<subsystem>
part of configuration, extension part of on top can stay it wont hurt either way. or you can connect to server with CLI and run
配置的一部分,顶部的扩展部分可以保持它不会受到任何伤害。或者您可以使用 CLI 连接到服务器并运行
/subsystem=jaxrs:remove()
In any case I would recommend to read bit about class loading in AS7 https://docs.jboss.org/author/display/AS72/Class+Loading+in+AS7
在任何情况下,我都会建议阅读有关 AS7 中的类加载https://docs.jboss.org/author/display/AS72/Class+Loading+in+AS7
Just a note, exclude-subsystems functionality and deployment-strucure:1.2 was added in 7.1.2and as such will not work on on 7.1.1.
请注意,排除子系统功能和部署结构:1.2已在 7.1.2 中添加,因此不适用于 7.1.1。
You can still remove jaxrs subsystem but that affects whole server.
您仍然可以删除 jaxrs 子系统,但这会影响整个服务器。
回答by Russ Hymanson
Here's the combination that worked for me (much of it came from the other answers in this post - thanks everyone!).
这是对我有用的组合(其中大部分来自这篇文章中的其他答案 - 谢谢大家!)。
- Jersey 2.22.1 + JBoss EAP 6.4 (based on JBoss AS 7.5)
- Exclude the jaxrs and resteasy subsystems via jboss-deployment-structure.xml
- Did not make changes in any JBoss modules as doing so caused other things not to work in my environment
- Disabled the resteasy scans via the context params in web.xml as others have pointed out
- Took out the Jersey init-param for the provider classes and instead implemented a javax.ws.rs.core.Application to self-register my Jersey resource classes
- Jersey 2.22.1 + JBoss EAP 6.4(基于 JBoss AS 7.5)
- 通过 jboss-deployment-structure.xml 排除 jaxrs 和 resteasy 子系统
- 没有对任何 JBoss 模块进行更改,因为这样做会导致其他事情在我的环境中不起作用
- 正如其他人指出的那样,通过 web.xml 中的上下文参数禁用了 resteasy 扫描
- 取出提供程序类的 Jersey init-param,而是实现了 javax.ws.rs.core.Application 来自注册我的 Jersey 资源类
At this point the web app deployed successfully without error, but I was getting 404 trying to access my REST service.
此时,Web 应用程序成功部署且没有错误,但我在尝试访问我的 REST 服务时遇到 404。
- Last thing I did was in web.xml where I changed Jersey from a servlet to a filter and it started working
- 我做的最后一件事是在 web.xml 中,我将 Jersey 从 servlet 更改为过滤器并开始工作
回答by Zammo Holtenhoffen
Following on from Clemens82's answer, After excluding the javaee.apimodule, I encountered this problem:
继Clemens82 的回答之后,在排除javaee.api模块后,我遇到了这个问题:
java.lang.NoClassDefFoundError: javax/xml/ws/Endpoint
at java.lang.Class.getDeclaredMethods0(Native Method) [rt.jar:1.7.0_72]
at java.lang.Class.privateGetDeclaredMethods(Class.java:2615) [rt.jar:1.7.0_72]
at java.lang.Class.getDeclaredMethods(Class.java:1860) [rt.jar:1.7.0_72]
at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:152) [spring-core-4.2.5.RELEASE.jar:4.2.5.RELEASE]
To fix it, I had to add the javax.xml.ws.apidependency into jboss-deployment-structure.xml. Additionally, I added the javax.servlet.apias this should be provided by container if using servlets.
为了修复它,我必须将javax.xml.ws.api依赖项添加到 jboss-deployment-structure.xml 中。此外,我添加了javax.servlet.api,因为如果使用 servlet,它应该由容器提供。
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<dependencies>
<module name="javax.xml.ws.api" /> <!-- Required due to exclusion of javaee.api below -->
<module name="javax.servlet.api" /> <!-- Required due to exclusion of javaee.api below -->
</dependencies>
<exclude-subsystems>
<subsystem name="resteasy" />
</exclude-subsystems>
<exclusions>
<module name="javaee.api" />
<module name="javax.ws.rs.api"/>
<module name="org.jboss.resteasy.resteasy-jaxrs" />
</exclusions>
<local-last value="true" />
</deployment>
</jboss-deployment-structure>
回答by Reginaldo Santos
I had almost the very same issue (https://stackoverflow.com/a/38273524/4534078):
我遇到了几乎相同的问题(https://stackoverflow.com/a/38273524/4534078):
"Need to run Jersey 2.23.1web app, already running on Tomcat7 in JBoss 7.1.1."
“需要运行Jersey 2.23.1web 应用程序,已经在JBoss 7.1.1 中的Tomcat7 上运行。”
Unfortunately, nothing here worked for me, but some pieces were handy taking me towards a final solution:
不幸的是,这里没有什么对我有用,但有些部分很方便,带我走向最终解决方案:
Turn off Resteasy package scanning in your web.xml:
<context-param> <param-name>resteasy.scan</param-name> <param-value>false</param-value> </context-param> <context-param> <param-name>resteasy.scan.providers</param-name> <param-value>false</param-value> </context-param> <context-param> <param-name>resteasy.scan.resources</param-name> <param-value>false</param-value> </context-param>
Remove all tags with "jaxrs" from standalone.xml. Otherwise you will still face LinkageError because JBoss keeps 1.1 spec "on".
Create yourApp.war!WEB-INF\jboss-deployment-structure.xml just like is pointed out here: https://docs.jboss.org/author/display/AS7/Class+Loading+in+AS7#ClassLoadinginAS7-JBossDeploymentStructureFile
在 web.xml 中关闭 Resteasy 包扫描:
<context-param> <param-name>resteasy.scan</param-name> <param-value>false</param-value> </context-param> <context-param> <param-name>resteasy.scan.providers</param-name> <param-value>false</param-value> </context-param> <context-param> <param-name>resteasy.scan.resources</param-name> <param-value>false</param-value> </context-param>
从standalone.xml 中删除所有带有“jaxrs”的标签。否则你仍然会面临 LinkageError 因为 JBoss 保持 1.1 规范“开启”。
创建 yourApp.war!WEB-INF\jboss-deployment-structure.xml 就像这里指出的那样:https://docs.jboss.org/author/display/AS7/Class+Loading+in+AS7#ClassLoadinginAS7-JBossDeploymentStructureFile
This way, not only:
这样,不仅:
java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;
java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;
Disapears, but also, JAXB works fine (No ClassNotFoundException for javax.xml.bind.JAXBException
once module javax.xml.bind.api
is also activated).
消失了,而且 JAXB 工作正常(没有ClassNotFoundException for javax.xml.bind.JAXBException
一次模块javax.xml.bind.api
也被激活)。
Obs1: The original question is mixing jersey 1.x with jersey 2.x. There is no PojoMappingFeature in jersey 2.x and base package is org.glassfish.jersey
. Take a look at https://jersey.java.net/documentation/latest/migration.html#mig-1-x-json
Obs1:最初的问题是将球衣 1.x 与球衣 2.x 混合。jersey 2.x 中没有 PojoMappingFeature,基础包是org.glassfish.jersey
. 看看https://jersey.java.net/documentation/latest/migration.html#mig-1-x-json
Obs2: I have also tried other approaches like extending ResourceConfig and scanning packages from there or registering classes directly. Nothing worked like the proper documentation in item 3. So kept my servlet untouched:
Obs2:我还尝试了其他方法,例如扩展 ResourceConfig 并从那里扫描包或直接注册类。没有什么比第 3 项中的正确文档更有效。所以保持我的 servlet 不变:
<servlet>
<servlet-name>WebNize REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>br.com.webnize.rest.service</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>WebNize REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
I hope it helps!
我希望它有帮助!