`

jee6 学习笔记 7.2 - use "wsimport" to create a command line web service client

    博客分类:
  • JEE
阅读更多
"wsimport" is a tool from the JAX-WS module. Sine from version 2.0, JAX-WS is a standard part of the JDK, it should be installed already and available to you if you have JDK1.6 installed on your box.

"wsimport" helps to generate the classes to consume a web service. the only required information is the wsdl file. it would generate the java classes for "marshalling/unmarshalling" the SOAP request/response payload. That is, from xml to Java classes and vice versa.

Here's its usage:
Usage: wsimport [options] <WSDL_URI>

Examples:
  wsimport stock.wsdl -b stock.xml -b stock.xjb
  wsimport -d generated http://example.org/stock?wsdl


There're two options that are quite useful:

-p: this option let you define the package name for the generated stuff.
-keep: this option signals the tool to keep the generated source code.

Here's the command line used to invoke the tool:
wsimport -p test.jxee.ws.gen -keep http://localhost:8180/ProJee6/StudentDAOImp?wsdl


Screen shot of invoking "wsimport"



The generated java classes can then be used to create web service client:

StudentWsClient.java
package test.jxee.ws.client;

import java.util.List;

import test.jxee.ws.gen.Student;
import test.jxee.ws.gen.StudentDAOImp;
import test.jxee.ws.gen.StudentDAOImpService;

/**
 *  command line tester client for the StudentDAOImpService 
 */
public class StudentWsClient {

  public static void main(String[] a) {
    System.out.println(">>> testing the StudentDAOImpService......");
    
    // get the service port
    StudentDAOImpService service = new StudentDAOImpService();
    StudentDAOImp port = service.getStudentDAOImpPort();
    
    System.out.println(">>> calling StudentDAOImp service...");
    
    // call the service operation find
    String namefilter = "jason";
    List<Student> studentList = port.find(namefilter, 10);
    
    int listsize = (studentList != null ? studentList.size() : 0);
    System.out.println(">>> printing returned student list, size: " + listsize);
    for(Student s : studentList) {
      System.out.println("-------------------------------");
      System.out.println("name: " + s.getName());
      System.out.println("age: " + s.getAge());
      System.out.println("mobile: " + s.getMobile());
      System.out.println("created date: " + s.getCreatedDate());
    }
    
    System.out.println(">>> testing the StudentDAOImpService..done");
  }
}



Here's the result running the web service client:

C:\tmp\wstest\jwtest2>java -cp C:\JwangDev\ProJee6\build\classes test.jxee.ws.client.StudentWsClient
>>> testing the StudentDAOImpService......
>>> calling StudentDAOImp service...
>>> printing returned student list, size: 1
-------------------------------
name: jason
age: 11
mobile: 02189898
created date: 2012-07-19T15:39:03+12:00
>>> testing the StudentDAOImpService..done

C:\tmp\wstest\jwtest2>
C:\tmp\wstest\jwtest2>

  • 大小: 26.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics