在
Java6 WebServices (一)服务端这篇文章中和大家分享了Java6 WebServices 服务端的写法,光有服务端还不行,还要有客户端才行啊。看了一些网友的相关文章,都只给出了服务端的写法,没有说客户端怎么说。经过一番研究,终于搞定了客户端,今天偶就和大家分享下。
首先启动 Java6 WebServices (一)服务端一文中写好的服务端。
新建个项目。命令行到src目录执行,
wsimport -keep http://localhost:8080/HelloService?wsdl
会自动生成一些代码。
package org.hermit.study.jdk.client;
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 = " Hello " , targetNamespace = " http://jdk.study.hermit.org/client " )
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface Hello {
/**
*
* @param arg0
* @return
* returns java.lang.String
*/
@WebMethod
@WebResult(partName = " return " )
public String sayHello(
@WebParam(name = " arg0 " , partName = " arg0 " )
String arg0);
}
package org.hermit.study.jdk.client;
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 = " HelloService " , targetNamespace = " http://jdk.study.hermit.org/client " , wsdlLocation = " http://localhost:8080/HelloService?wsdl " )
public class HelloService
extends Service
{
private final static URL HELLOSERVICE_WSDL_LOCATION;
static {
URL url = null ;
try {
url = new URL( " http://localhost:8080/HelloService?wsdl " );
} catch (MalformedURLException e) {
e.printStackTrace();
}
HELLOSERVICE_WSDL_LOCATION = url;
}
public HelloService(URL wsdlLocation, QName serviceName) {
super (wsdlLocation, serviceName);
}
public HelloService() {
super (HELLOSERVICE_WSDL_LOCATION, new QName( " http://jdk.study.hermit.org/client " , " HelloService " ));
}
/**
*
* @return
* returns Hello
*/
@WebEndpoint(name = " HelloPort " )
public Hello getHelloPort() {
return (Hello) super .getPort( new QName( " http://jdk.study.hermit.org/client " , " HelloPort " ), Hello. class );
}
}
下面我们再写个测试,看看能不能用:
执行。
控制台输出:
hello:hermit
ok!搞定。
java6搞定web service就这么简单。。。。。。。。。。。。。。。。。。。。。。。。。。
xfire危险了。。。。。。。。。。。。。。。。。。。。。。。。
首先启动 Java6 WebServices (一)服务端一文中写好的服务端。
新建个项目。命令行到src目录执行,
wsimport -keep http://localhost:8080/HelloService?wsdl
会自动生成一些代码。
package org.hermit.study.jdk.client;
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 = " Hello " , targetNamespace = " http://jdk.study.hermit.org/client " )
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface Hello {
/**
*
* @param arg0
* @return
* returns java.lang.String
*/
@WebMethod
@WebResult(partName = " return " )
public String sayHello(
@WebParam(name = " arg0 " , partName = " arg0 " )
String arg0);
}
package org.hermit.study.jdk.client;
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 = " HelloService " , targetNamespace = " http://jdk.study.hermit.org/client " , wsdlLocation = " http://localhost:8080/HelloService?wsdl " )
public class HelloService
extends Service
{
private final static URL HELLOSERVICE_WSDL_LOCATION;
static {
URL url = null ;
try {
url = new URL( " http://localhost:8080/HelloService?wsdl " );
} catch (MalformedURLException e) {
e.printStackTrace();
}
HELLOSERVICE_WSDL_LOCATION = url;
}
public HelloService(URL wsdlLocation, QName serviceName) {
super (wsdlLocation, serviceName);
}
public HelloService() {
super (HELLOSERVICE_WSDL_LOCATION, new QName( " http://jdk.study.hermit.org/client " , " HelloService " ));
}
/**
*
* @return
* returns Hello
*/
@WebEndpoint(name = " HelloPort " )
public Hello getHelloPort() {
return (Hello) super .getPort( new QName( " http://jdk.study.hermit.org/client " , " HelloPort " ), Hello. class );
}
}
下面我们再写个测试,看看能不能用:
package
org.hermit.study.jdk.client.test;
import org.hermit.study.jdk.client.Hello;
import org.hermit.study.jdk.client.HelloService;
public class TestClient {
public static void main(String[] args) {
HelloService service = new HelloService();
Hello _hello = service.getHelloPort();
System.out.println(_hello.sayHello( " hermit " ));
}
}
import org.hermit.study.jdk.client.Hello;
import org.hermit.study.jdk.client.HelloService;
public class TestClient {
public static void main(String[] args) {
HelloService service = new HelloService();
Hello _hello = service.getHelloPort();
System.out.println(_hello.sayHello( " hermit " ));
}
}
控制台输出:
hello:hermit
ok!搞定。
java6搞定web service就这么简单。。。。。。。。。。。。。。。。。。。。。。。。。。
xfire危险了。。。。。。。。。。。。。。。。。。。。。。。。