我已经实现了网络服务:
@WebServiceClient(//parameters//)
@HandlerChain(file = "handlers.xml")
public class MyWebServiceImpl {...}
我还使用类列表实现了 ObjectFactory,用于创建我的请求和响应。例如类测试。
我需要得到响应的xml。
我尝试使用 JAX-WS SOAP 处理程序,所以我添加了这个@HandlerChain(file = "handlers.xml")
注释。我的 handlers.xml 看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<handler-chains xmlns="http://java.sun.com/xml/ns/javaee">
<handler-chain>
<handler>
<handler-class>java.com.webservice.service.LoggingHandler</handler-class>
</handler>
</handler-chain>
</handler-chains>
我的 LoggingHandler 类是:
import java.io.PrintWriter;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPMessageContext;
public class LoggingHandler implements javax.xml.ws.handler.soap.SOAPHandler<SOAPMessageContext> {
public void close(MessageContext messagecontext) {
}
public Set<QName> getHeaders() {
return null;
}
public boolean handleFault(SOAPMessageContext messagecontext) {
return true;
}
public boolean handleMessage(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean) smc.get (MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
System.out.println("\nOutbound message:");
} else {
System.out.println("\nInbound message:");
}
SOAPMessage message = smc.getMessage();
try {
PrintWriter writer = new PrintWriter("soap_responce" + System.currentTimeMillis(), "UTF-8");
writer.println(message);
writer.close();
message.writeTo(System.out);
System.out.println(""); // just to add a newline
} catch (Exception e) {
System.out.println("Exception in handler: " + e);
}
return outboundProperty;
}
}
我有创建请求的测试类,这里是代码的一部分:
MyWebServiceImpl impl = new MyWebServiceImpl(url, qName);
ws = impl.getMyWebServicePort();
Test req = new Test();
我想在文件“soap_responce”+ System.currentTimeMillis() 中获得 xml 响应。但是甚至没有创建这样的文件。请建议如何获取 xml 响应,我是 Web 服务的新手,可能会做错事。谢谢