?
接着上一篇文章,这里举一个client端handler的例子。
先把webservice服务端的代码整个贴一下
webservice:
view plaincopy to clipboardprint?
01.import javax.jws.HandlerChain;??
02.import javax.jws.WebMethod;??
03.import javax.jws.WebService;??
04.?
05.?
06.@WebService?
07.@HandlerChain(file="handlers.xml")??
08.public class HelloWS {??
09.?
10.??? @WebMethod?
11.??? public String sayHello(String name) {??
12.??????? return "Hello " + name + ".";??
13.??? }??
14.}?
import javax.jws.HandlerChain;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
@HandlerChain(file="handlers.xml")
public class HelloWS {
??? @WebMethod
??? public String sayHello(String name) {
??????? return "Hello " + name + ".";
??? }
}
handler:
view plaincopy to clipboardprint?
01.import java.util.Set;??
02.?
03.import javax.xml.namespace.QName;??
04.import javax.xml.ws.handler.MessageContext;??
05.import javax.xml.ws.handler.soap.SOAPHandler;??
06.import javax.xml.ws.handler.soap.SOAPMessageContext;??
07.?
08.public class HelloHandler implements SOAPHandler<SOAPMessageContext> {??
09.?
10.?
11.??? @Override?
12.??? public boolean handleMessage(SOAPMessageContext context) {??
13.??????? System.out.println(context.get(MessageContext.WSDL_SERVICE).toString());??
14.??????? return true;??
15.??? }??
16.?
17.??? @Override?
18.??? public boolean handleFault(SOAPMessageContext context) {??
19.??????? // TODO Auto-generated method stub??
20.??????? return true;??
21.??? }??
22.?
23.??? @Override?
24.??? public void close(MessageContext context) {??
25.??????? // TODO Auto-generated method stub??
26.??????????
27.??? }??
28.?
29.??? @Override?
30.??? public Set<QName> getHeaders()??
31.??? {??
32.??????? // TODO Auto-generated method stub??
33.??????? return null;??
34.??? }??
35.?
36.}?
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
public class HelloHandler implements SOAPHandler<SOAPMessageContext> {
?@Override
?public boolean handleMessage(SOAPMessageContext context) {
??System.out.println(context.get(MessageContext.WSDL_SERVICE).toString());
??return true;
?}
?@Override
?public boolean handleFault(SOAPMessageContext context) {
??// TODO Auto-generated method stub
??return true;
?}
?@Override
?public void close(MessageContext context) {
??// TODO Auto-generated method stub
??
?}
?@Override
?public Set<QName> getHeaders()
?{
??// TODO Auto-generated method stub
??return null;
?}
}
?
这里用j2se来发布webservice,主函数为:
view plaincopy to clipboardprint?
01.import javax.xml.ws.Endpoint;??
02.?
03.public class ServerMain {??
04.?
05.??? /**?
06.???? * @param args?
07.???? */?
08.??? public static void main(String[] args)??
09.??? {??
10.??????? Endpoint.publish("http://localhost:8000/HelloWS3/HelloWSService",new HelloWS());??
11.?
12.??? }??
13.?
14.}?
import javax.xml.ws.Endpoint;
public class ServerMain {
?/**
? * @param args
? */
?public static void main(String[] args)
?{
??Endpoint.publish(" HelloWS());
?}
}
运行主函数,可以发布webservice,wsdl地址为:http://localhost:8000/HelloWS3/HelloWSService?wsdl
生成客户端代码
用wsimport命令生成客户端代码
命令为:。。。workspacehelloWS3Clientbin>wsimport -keep? http://localhost:8000/HelloWS3/HelloWSService?wsdl -p com.ws.client
其中keep表示保存源文件,-p表示代码保存到指定包下。
然后在com.ws.client下新建一个客户端主函数,其中在客户端的webservice代理对象中插入handler,为了方便使用上面的同一个handler类,client端代码如下:
view plaincopy to clipboardprint?
01.import java.util.List;??
02.?
03.import javax.xml.ws.Binding;??
04.import javax.xml.ws.BindingProvider;??
05.import javax.xml.ws.handler.Handler;??
06.?
07.import com.ws.handler.HelloHandler;??
08.?
09.public class ClientMain {??
10.?
11.??? /**?
12.???? * @param args?
13.???? */?
14.??? public static void main(String[] args)??
15.??? {??
16.??????? HelloWSService hs=new HelloWSService();??
17.??????????
18.??????? HelloWS hello= hs.getHelloWSPort();??
19.??????????
20.??????? Binding binding=((BindingProvider)hello).getBinding();??
21.??????? List<Handler> handlerChain=binding.getHandlerChain();??
22.??????? handlerChain.add(new HelloHandler());??
23.??????? binding.setHandlerChain(handlerChain);??
24.??????????
25.??????? System.out.println(hello.sayHello("xxxxxxx"));??
26.?
27.??? }??
28.?
29.}?
import java.util.List;
import javax.xml.ws.Binding;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.Handler;
import com.ws.handler.HelloHandler;
public class ClientMain {
?/**
? * @param args
? */
?public static void main(String[] args)
?{
??HelloWSService hs=new HelloWSService();
??
??HelloWS hello= hs.getHelloWSPort();
??
??Binding binding=((BindingProvider)hello).getBinding();
??List<Handler> handlerChain=binding.getHandlerChain();
??handlerChain.add(new HelloHandler());
??binding.setHandlerChain(handlerChain);
??
??System.out.println(hello.sayHello("xxxxxxx"));
?}
}
代码的作用是在bindingprovider对象的handlerchain中添加一个handler。
保持之前的服务端webservice运行着,然后运行这个客户端,可以在两个进程的控制台中同时打印被调用的webservice名字。
server:
2011-5-4 18:12:08 com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
信息: Dynamically creating request wrapper Class com.ws.service.jaxws.SayHello
2011-5-4 18:12:08 com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
信息: Dynamically creating response wrapper bean Class com.ws.service.jaxws.SayHelloResponse
{http://service.ws.com/}HelloWSService
{http://service.ws.com/}HelloWSService
client:
{http://service.ws.com/}HelloWSService {http://service.ws.com/}HelloWSService Hello xxxxxxx.s