使用java6做webservice 的客户端
在上一篇博文介绍了如何使用java 6内置的服务器跑wervice。那么我们紧接着根据已发布的服务生成其服务的客户端。
我们只需要使用wsimport来生成客户端代码。
在命令行中输入 :wsimport -keep -p com.geostar.geoglobe.jaxws.client http://126.33.8.183:9999/GeoGlobeServer?wsdl
点击回车,立马会为我们生成客户端代码。在com.geostar.geoglobe.jaxws目录下生成了GeoGlobeServer.java、ObjectFactory.java、WSDemo.java三个类。
下面我看看这些类是做什么的。
//GeoGlobeServer.java
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
/**
* This class was generated by the JAXWS SI.
* JAX-WS RI 2.0_02-b08-fcs
* Generated source version: 2.0
*
*/
@WebServiceClient(name = "GeoGlobeServer", targetNamespace = "http://www.geostar.com.cn", wsdlLocation = "http://126.33.8.183:9999/GeoGlobeServer?wsdl")
public class GeoGlobeServer
extends Service
{
private final static URL GEOGLOBESERVER_WSDL_LOCATION;
static {
URL url = null;
try {
url = new URL("http://126.33.8.183:9999/GeoGlobeServer?wsdl");
} catch (MalformedURLException e) {
e.printStackTrace();
}
GEOGLOBESERVER_WSDL_LOCATION = url;
}
public GeoGlobeServer(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public GeoGlobeServer() {
super(GEOGLOBESERVER_WSDL_LOCATION, new QName("http://www.geostar.com.cn", "GeoGlobeServer"));
}
/**
*
* @return
* returns WSDemo
*/
@WebEndpoint(name = "WSDemoPort")
public WSDemo getWSDemoPort() {
return (WSDemo)super.getPort(new QName("http://www.geostar.com.cn", "WSDemoPort"), WSDemo.class);
}
}
//WSDemo.java
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
/**
* This class was generated by the JAXWS SI.
* JAX-WS RI 2.0_02-b08-fcs
* Generated source version: 2.0
*
*/
@WebService(name = "WSDemo", targetNamespace = "http://www.geostar.com.cn")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public interface WSDemo {
/**
*
* @param input
* @return
* returns java.lang.String
*/
@WebMethod
@WebResult(name = "doSomethingResponse", targetNamespace = "http://www.geostar.com.cn", partName = "doSomethingResponse")
public String doSomething(
@WebParam(name = "input", targetNamespace = "http://www.geostar.com.cn", partName = "input")
String input);
}
//ObjectFactory.java
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the ClientTC package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {
private final static QName _DoSomethingResponse_QNAME = new QName("http://www.geostar.com.cn", "doSomethingResponse");
private final static QName _Input_QNAME = new QName("http://www.geostar.com.cn", "input");
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: ClientTC
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link String }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://www.geostar.com.cn", name = "doSomethingResponse")
public JAXBElement<String> createDoSomethingResponse(String value) {
return new JAXBElement<String>(_DoSomethingResponse_QNAME, String.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link String }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://www.geostar.com.cn", name = "input")
public JAXBElement<String> createInput(String value) {
return new JAXBElement<String>(_Input_QNAME, String.class, null, value);
}
}
我们可以写一个测试类:WSDemoClient.java
代码如下:
public class WSDemoClient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
GeoGlobeServer server=new GeoGlobeServer();
WSDemo demo=server.getWSDemoPort();
System.out.println(demo.doSomething("李克华"));
}
}
点击Run 运行WSDemoClient.java
在控制台我们可以看到:
hello:李克华