WebServiceインスタンス2、javaEE 6の新しいFeatureのwsimportツールを呼び出す


JavaEE 6の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]
     
    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:testgenerateディレクトリの下でjavaコードファイルが生成されていることがわかります.wsimportは簡単です.
     
     
  • WebServiceを呼び出すクライアント
  • を記述する
    もし私たちが呼び出すWebServiceが前に例を挙げたTestServiceであれば、次のような方法しかありません.
     
    public String test(String name) throws SOAPException
    {
         if (name == null)
        {
            throw new SOAPException("name can't be null!");
        }
    		
        return "hello " + name; 
    }

     
    wsimportツールを使用すると、次のコードファイルが得られます.
     
    generate
        |--com
            |--company
                |--ObjectFactory.java
                |--SOAPException.java
                |--SOAPException_Exception.java
                |--Test.java
                |--TestResponse.java
                |--TestService.java
                |--TestService_Service.java
                |--package-info.java
     
    それらをコンパイルして、次のようなアプリケーションを書いてテストします.
     
     
        public static void main(String[] args)
        {
            TestService_Service serviceFactory = new TestService_Service();
            TestService service = serviceFactory.getTestServicePort();
            try
            {
                System.out.println(service.test(null));
            }
            catch (SOAPException_Exception ex)
            {
                System.out.println(ex.getMessage());
            }
        }

     
    実行すると画面が出力されます
    name can't be null
     
    私たちは前にnull値を入力したので、実はこれもSOAPExceptionが正常に動作しているかどうかをテストするためで、もしあなたが正しい文字列を入力すれば、webserviceはシステムのように正常に動作することができます.out.println(service.test("javaeye"));,画面に表示され、
    Hello javaeye!
     
    ちなみにWebServiceの異常処理は、すべてのWebServiceの異常をSOAPExceptionで投げ出さなければなりません.javaです.xml.SOapパッケージの1つのクラス(本人は他のExceptionを試したことがありますが、正常にキャプチャできません.SOAPExceptionだけが正常に動作します.具体的な原因は不明ですが、誰か知っている人がいたら、コメントしてください.ありがとうございます)