我刚开始使用 Web 服务,我有一个客户端应用程序连接到另一个 wsdl Web 服务,它使用与我不同的前缀。我用处理程序检查了它以打印出肥皂原始消息。他们使用“S”和我的“env”。我使用了 Netbeans 6.5 中的 JAX WS 2.1。
我不确定,但我认为这种差异会导致失败。这是我的堆栈跟踪:
14:01:56,817 ERROR [CommonClient] Exception caught while (preparing for) performing the invocation:
javax.xml.ws.soap.SOAPFaultException: Couldn't create SOAP message due to exception: XML reader error: com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog
at [row,col {unknown-source}]: [1,0]
at org.jboss.ws.core.jaxws.SOAPFaultHelperJAXWS.getSOAPFaultException(SOAPFaultHelperJAXWS.java:84)
at org.jboss.ws.core.jaxws.binding.SOAP11BindingJAXWS.throwFaultException(SOAP11BindingJAXWS.java:107)
at org.jboss.ws.core.CommonSOAPBinding.unbindResponseMessage(CommonSOAPBinding.java:579)
at org.jboss.ws.core.CommonClient.invoke(CommonClient.java:381)
at org.jboss.ws.core.jaxws.client.ClientImpl.invoke(ClientImpl.java:290)
at org.jboss.ws.core.jaxws.client.ClientProxy.invoke(ClientProxy.java:170)
at org.jboss.ws.core.jaxws.client.ClientProxy.invoke(ClientProxy.java:150)
at $Proxy1792.doService(Unknown Source)
at com.pckg.web.services.MyWebServiceImpl.service(MyWebServiceImpl.java:143)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:662)
我试图通过一些我发现的使用处理程序的方法来更改我的前缀肥皂消息。不幸的是,我的肥皂处理程序不工作,或者我可能错过了什么。
这是我的处理程序的样子:
public class CustomSOAPHandler implements SOAPHandler<SOAPMessageContext> {
@Override
public Set<QName> getHeaders() {
return null;
}
@Override
public void close(MessageContext context) {
}
@Override
public boolean handleFault(SOAPMessageContext context) {
WebUtils.log("CustomSOAPHandler.handleFault");
try {
boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outbound) {
WebUtils.log("Direction : outbound (handleFault)");
} else {
WebUtils.log("Direction : inbound (handleFault)");
}
if (!outbound) {
try {
SOAPMessage msg = ((SOAPMessageContext) context).getMessage();
logToSystemOut(context);
if (context.getMessage().getSOAPBody().getFault() != null) {
String detailName = null;
try {
detailName = context.getMessage().getSOAPBody().getFault().getDetail().getFirstChild().getLocalName();
WebUtils.log("detailName : " + detailName);
} catch (Exception e) {
}
}
} catch (SOAPException e) {
WebUtils.log("SOAP Exception : " + e);
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
WebUtils.log("Exception in handler: " + e);
}
return true;
}
@Override
public boolean handleMessage(SOAPMessageContext context) {
WebUtils.log("CustomSOAPHandler.handleMessage");
try {
SOAPMessage message = context.getMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPHeader header = message.getSOAPHeader();
SOAPBody body = message.getSOAPBody();
WebUtils.log("Configure SOAP Message prefix from [" + envelope.getPrefix() + "] into [S]");
envelope.setPrefix("S");
header.setPrefix("S");
body.setPrefix("S");
context.setMessage(message);
Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
// outbound
WebUtils.log("Direction : outbound (handleMessage)");
logToSystemOut(context);
} else {
// inbound
WebUtils.log("Direction : inbound (handleMessage)");
logToSystemOut(context);
}
} catch (Exception e) {
WebUtils.log("Exception in handler: " + e);
}
return true;
}
private void logToSystemOut(SOAPMessageContext smc) {
SOAPMessage msg = smc.getMessage();
if (msg == null) {
WebUtils.log("SOAP Message is null");
return;
}
WebUtils.log("");
WebUtils.log("--------------------");
WebUtils.log("DUMP OF SOAP MESSAGE");
WebUtils.log("--------------------");
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
msg.writeTo(baos);
WebUtils.log(baos.toString(getMessageEncoding(msg)));
} catch (Exception e) {
e.printStackTrace();
}
}
private String getMessageEncoding(SOAPMessage msg) throws SOAPException {
String encoding = "utf-8";
if (msg.getProperty(SOAPMessage.CHARACTER_SET_ENCODING) != null) {
encoding = msg.getProperty(SOAPMessage.CHARACTER_SET_ENCODING).toString();
}
return encoding;
}
}
在我的课堂上,创建服务对象后,我使用以下代码:
com.gateway.ws.WebServiceCustomService service = new
com.gateway.ws.WebServiceCustomService();
service.setHandlerResolver(new HandlerResolver() {
@Override
public List<Handler> getHandlerChain(PortInfo portInfo) {
List<Handler> handlerChain = new ArrayList<Handler>();
Handler soapHandler = new CustomSOAPHandler();
handlerChain.add(soapHandler);
return handlerChain;
}
});
com.gateway.ws.WebServicePort port = service.getWebServicePort();
BindingProvider bindingProvider = (BindingProvider) port;
Binding binding = bindingProvider.getBinding();
List handlerList = binding.getHandlerChain();
System.out.println("LIST OF HANDLER " + handlerList.size());
正如我之前所说,我检查了我的日志并且前缀从未更改为“S”
11:46:06,145 INFO [STDOUT] LIST OF HANDLER 1
11:46:06,147 INFO [STDOUT] SOAPHandler.handleMessage
11:46:06,148 INFO [STDOUT] Configure SOAP Message prefix from [env] into [S]
11:46:06,149 INFO [STDOUT] Outgoing message:
11:46:06,149 INFO [STDOUT] --------------------
11:46:06,149 INFO [STDOUT] DUMP OF SOAP MESSAGE
11:46:06,149 INFO [STDOUT] --------------------
11:46:06,155 INFO [STDOUT] <env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'><env:Header></env:Header><env:Body><ns1:doService xmlns:ns1="http://ws.gateway.xl/"><request>content</request></ns1:doService></env:Body></env:Envelope>
任何建议都会非常有帮助。谢谢你。