java Web 服务的 Web 客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2093889/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:24:19  来源:igfitidea点击:

web client for web service

javaweb-servicesjaxb

提问by Zaur_M

I've a web-service that works fine when I access them from a J2SE (desktop) application. To access this service I do follow:

我有一个 Web 服务,当我从 J2SE(桌面)应用程序访问它们时,它可以正常工作。要访问此服务,我确实遵循:

  1. generate stub classes by wsdl link using java wsimport tool
  2. then I create service using generated classes and run one of wsdl operations.It looks like this:

    MyWebServiceService webService = new MyWebServiceService();

    MyWebService port = webService.getMyWebServicePort();

    webService.run("XYZ");

  1. 使用 java wsimport 工具通过 wsdl 链接生成存根类
  2. 然后我使用生成的类创建服务并运行 wsdl 操作之一。它看起来像这样:

    MyWebServiceService webService = new MyWebServiceService();

    MyWebService 端口 = webService.getMyWebServicePort();

    webService.run("XYZ");

As I sad it work fine when I use it in a standalone application. But...when I try to access web-service in the same way but from servlet-client, using generated stubs I get following error:

遗憾的是,当我在独立应用程序中使用它时,它可以正常工作。但是...当我尝试以相同的方式访问 Web 服务但从 servlet-client 使用生成的存根时,我收到以下错误:

java.lang.ClassCastException: com.sun.xml.bind.v2.runtime.JAXBContextImpl cannot be cast to com.sun.xml.bind.api.JAXBRIContext
org.jboss.ws.metadata.umdm.EndpointMetaData.eagerInitializeAccessors(EndpointMetaData.java:686)
org.jboss.ws.metadata.umdm.EndpointMetaData.initializeInternal(EndpointMetaData.java:567)
org.jboss.ws.metadata.umdm.EndpointMetaData.eagerInitialize(EndpointMetaData.java:553)
org.jboss.ws.metadata.builder.jaxws.JAXWSClientMetaDataBuilder.rebuildEndpointMetaData(JAXWSClientMetaDataBuilder.java:314)
org.jboss.ws.core.jaxws.spi.ServiceDelegateImpl.getPortInternal(ServiceDelegateImpl.java:271)
org.jboss.ws.core.jaxws.spi.ServiceDelegateImpl.getPort(ServiceDelegateImpl.java:202)
javax.xml.ws.Service.getPort(Service.java:143...

I've searched google long time but found nothing helpful topics. Some topics show examples accessing web-services from servlet, but unfortunately I can't do this...( And don't know what is cause of trouble.

我在谷歌上搜索了很长时间,但没有找到任何有用的主题。一些主题展示了从 servlet 访问 Web 服务的示例,但不幸的是我不能这样做......(并且不知道是什么导致了问题。

Application server: jboss 4.2.3GA

应用服务器:jboss 4.2.3GA

Is it possible to connect web-service from servlet? How?

是否可以从 servlet 连接 Web 服务?如何?

I've tried use @WebServiceRef annotation, but it seem web-container can't inject web-service stub. And I think that container must not do this itself, because stub classes have already been generated by wsimport tool, and its enouph to use this classes for accessing of web-service.

我试过使用 @WebServiceRef 注释,但似乎 web-container 不能注入 web-service 存根。而且我认为容器本身不能这样做,因为 wsimport 工具已经生成了存根类,并且可以使用这些类来访问 web 服务。

Stub classes were generated using the following command:

使用以下命令生成存根类:

wsimport -keep -p com.myhost.ws http://www.myhost.com/services/MyWebService?wsdl

回答by JavaGuy

Did you make sure your classpath does not contain multiple JAX-B Jars with differing versions ? The exception looks like a version conflict to me. Application servers usually have some kind of "endorsed" lib directory that holds JARS that are always added in front of web application classpaths. Maybe your app server has a conflicting JAX-B implementation there ?

您是否确保您的类路径不包含多个不同版本的 JAX-B 罐?异常对我来说看起来像是版本冲突。应用程序服务器通常有某种“认可”的 lib 目录,其中包含始终添加在 Web 应用程序类路径前面的 JARS。也许你的应用服务器有一个冲突的 JAX-B 实现?

If you use Maven to package your application, make sure transitive dependencies don't pull in unwanted JAX-B Jars (use 'mvn dependency:tree' to check this).

如果您使用 Maven 打包您的应用程序,请确保传递依赖项不会引入不需要的 JAX-B Jars(使用“mvn dependency:tree”来检查这一点)。

回答by elduff

This definitely sounds like a JAXB conflict to me. Check out the jaxb versions that you have in your war and make sure that they are not conflicting with a jaxb jar that Jboss may have in its lib directory.

这对我来说绝对听起来像是 JAXB 冲突。检查您在 war 中拥有的 jaxb 版本,并确保它们与 Jboss 可能在其 lib 目录中的 jaxb jar 没有冲突。

回答by Zaur_M

Addytionally if jbossws-native library was installed correctly the following packages should be deleted from jboss_home/lib/endorsed directory:

另外,如果 jbossws-native 库安装正确,以下包应该从 jboss_home/lib/endorsed 目录中删除:

  • jboss-jaxrpc.jar
  • jboss-jaxws-ext.jar
  • jboss-jaxws.jar
  • jboss-saaj.jar
  • jboss-jaxrpc.jar
  • jboss-jaxws-ext.jar
  • jboss-jaxws.jar
  • jboss-saaj.jar

Otherwise you don't have ability to connect to web service through EJB or servlet.

否则,您将无法通过 EJB 或 servlet 连接到 Web 服务。