在做CXF练习时运行webservice客户端如下代码Test02报错:
package org.konghao.ws.test;
import java.util.ArrayList;
import java.util.List;
import javax.xml.ws.handler.Handler;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.junit.Test;
import org.konghao.ws.IMyService;
import org.konghao.ws.MyServiceService;
import org.konghao.ws.handler.LicenseHandler;
public class TestWs {
@Test
public void Test01(){
/**
* 基于JAX-WS的方式访问服务端
*/
IMyService ms=new MyServiceService().getMyServicePort();
System.out.println(ms.sayHello("张三"));
}
@Test
public void Test02(){
JaxWsProxyFactoryBean fac=new JaxWsProxyFactoryBean();
fac.setServiceClass(IMyService.class);
fac.setAddress("http://localhost:8878/ms");
//可以通过CXF为访问增加相应的Interceptor来处理进去和出来的消息
fac.getInInterceptors().add(new LoggingInInterceptor());
fac.getInInterceptors().add(new LoggingOutInterceptor());
List<Handler> hans=new ArrayList<Handler>();
hans.add(new LicenseHandler());
fac.setHandlers(hans);
IMyService ms=(IMyService)fac.create();
System.out.println(ms.sayHello("李四"));
}
}
报错内容如下:
javax.xml.ws.soap.SOAPFaultException: Unexpected wrapper element {http://ws.konghao.org/}sayHello found. Expected {http://ws.konghao.org/}sayHelloResponse.
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:156)
at com.sun.proxy.$Proxy35.sayHello(Unknown Source)
at org.konghao.ws.test.TestWs.Test02(TestWs.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
经断点debug跟踪检查发现webservice服务器端的handler最后面没有设置成true.
public class LicenseHandler implements SOAPHandler<SOAPMessageContext> {
@Override
public boolean handleMessage(SOAPMessageContext ctx) {
try {
Boolean out=(Boolean)ctx.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY);//服务端消息是往这里传输的。
if(!out){
SOAPEnvelope envelope=ctx.getMessage().getSOAPPart().getEnvelope();
SOAPHeader header=envelope.getHeader();
if(header==null) return true;
Iterator<SOAPHeaderElement> iterator=header.getChildElements();
while(iterator.hasNext()){
SOAPHeaderElement element=iterator.next();
if(element.getLocalName().equals("licenseInfo")){
System.out.println("Server端:"+element.getTextContent());
}
}
}
} catch (SOAPException e) {
e.printStackTrace();
}
return false;
}
......
......
将
handleMessage类里的return false;改为return true;