加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

调用WebService实例

发布时间:2020-12-17 02:10:50 所属栏目:安全 来源:网络整理
导读:调用WebService实例二,javaEE6 新Feature之wsimport工具 ? 在javaEE6的bin文件夹时,有一个 wsimport .exe,这个工具在5.0之前的版本里是没有的,这个工具依据wsdl文件生成相应的类文件,然后用这些类文件,就可以像调用本地的类一样调用WebService提供的方

调用WebService实例二,javaEE6 新Feature之wsimport工具

?

在javaEE6的bin文件夹时,有一个wsimport.exe,这个工具在5.0之前的版本里是没有的,这个工具依据wsdl文件生成相应的类文件,然后用这些类文件,就可以像调用本地的类一样调用WebService提供的方法了

?

  • wsimport工具详细参数

The wsimport tool generates JAX-WS portable artifacts,such as:

  • Service Endpoint Interface (SEI)
  • Service
  • Exception class mapped from wsdl:fault (if any)
  • Async Reponse Bean derived from response wsdl:message (if any)
  • JAXB generated value types (mapped java classes from schema types)

These artifacts can be packaged in a WAR file with the WSDL and schema documents along with the endpoint implementation to be deployed. The generated Service class can be used to invoke the Web Service endpoint.

?

wsimport [options] <wsdl>

?

?

Option

Description

-d <directory>

Specify where to place generated output files

-b <path>

Specify external JAX-WS or JAXB binding files (Each <file> must have its own -b)

-catalog
Specify catalog file to resolve external entity references,it supports TR9401,XCatalog,and OASIS XML Catalog format. Please read the XML Entity and URI Resolvers document or see wsimport_catalog sample.

-extension

allow vendor extensions (functionality not specified by the specification). Use of extensions may result in applications that are not portable or may not interoperate with other implementations

-help

Display help

-httpproxy:<host>:<port>

Specify an HTTP proxy server (port defaults to 8080)

-keep

Keep generated files

-p Specifying a target package via this command-line option,overrides any wsdl and schema binding customization for package name and the default package name algorithm defined in the specification

-s <directory>

Specify where to place generated source files

-verbose

Output messages about what the compiler is doing

-version

Print version information

-wsdllocation <location>
@WebService.wsdlLocation and @WebServiceClient.wsdlLocation value

?

?

  • wsimport工具用法实例

?

首先,须保证你的jdk为6.0以上版本,可以在command line下试运行这个命令

c:/test> wsimport

?

假设我们有下面的wsdl文件

http://localhost:9080/WebService/TestService/TestService.wsdl

?

我们要把生成的代码放到c:/test/generate目录下,那么我们运行以下命令即可

?

c:/test> wsimport -s generate http://localhost:9080/WebService/TestService/TestService.wsdl

?

然后,你就会看到在c:/test/generate目录下生成了一些java代码文件,wsimport就是如此简单

?

?

  • 编写调用WebService的客户端

假如我们要调用的WebService就是之前举例提到的TestService,它只有下面这样一个方法

?

Java代码

复制代码

  1. public?String?test(String?name)?throws?SOAPException ??
  2. { ??
  3. ?????if?(name?==?null) ??
  4. ????{ ??
  5. ????????throw?new?SOAPException("name?can't?be?null!"); ??
  6. ????} ??
  7. ???????? ??
  8. ????return?"hello?"?+?name;? ??
  9. }??

?

wsimport工具,我们可以得到如下代码文件

?

generate

??? |--com

??????? |--company

??????????? |--ObjectFactory.java

??????????? |--SOAPException.java

??????????? |--SOAPException_Exception.java

??????????? |--Test.java

??????????? |--TestResponse.java

??????????? |--TestService.java

??????????? |--TestService_Service.java

??????????? |--package-info.java

?

把它们编译,然后写下面这样一个application去测试

?

?

Java代码

复制代码

  1. public?static?void?main(String[]?args) ??
  2. { ??
  3. ????TestService_Service?serviceFactory?=?new?TestService_Service(); ??
  4. ????TestService?service?=?serviceFactory.getTestServicePort(); ??
  5. ????try??
  6. ????{ ??
  7. ????????System.out.println(service.test(null)); ??
  8. ????} ??
  9. ????catch?(SOAPException_Exception?ex) ??
  10. ????{ ??
  11. ????????System.out.println(ex.getMessage()); ??
  12. ????} ??
  13. }??

?

运行它,屏幕就会输出

name can't be null

?

因为我们之前传了一个null值进去嘛,其实这也是为了测试SOAPException是否正常运行,如果你传个正确的字符串进去,webservice就可以正常运行,如System.out.println(service.test("javaeye"));,屏幕上就会显示,

Hello javaeye!

?

顺便提一下WebService的异常处理,所有WebService的异常都必须用SOAPException抛出,它是java.xml.soap包中的一个类 (本人试过其它一些Exception,都不能被正常捕捉,只有SOAPException可以正常工作,具体原因不明,如果有哪个人清楚这一点,请评论告之,谢谢)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读