我已經開發了一個用於從JQuery調用soap web服務的示例應用程序,但是我正面臨一些查找服務的問題。我正在使用apache cxf 2.2.6罐子。如何從JQuery調用soap web服務
我用這個
創建一個web服務CAL
http://www.local-guru.net/blog/2010/1/24/calling-webservices-from-mootools-jquery-or-dojo 從JQuery的
這是我的接口
package guru.service;
import javax.jws.WebService;
import javax.jws.WebParam;
import javax.xml.ws.RequestWrapper;
@WebService(targetNamespace = "http://www.local-guru.net/helloService")
public interface HelloService {
@RequestWrapper(targetNamespace="http://www.local-guru.net/helloService")
public String hello(@WebParam(name="givenname", targetNamespace="http://www.local-guru.net/helloService" ) String givenname, @WebParam(name="name", targetNamespace="http://www.local-guru.net/helloService") String name);
}
我的實現類是
package guru.service;
import javax.jws.WebService;
@WebService(endpointInterface="guru.service.HelloService", serviceName="HelloService", targetNamespace = "http://www.local-guru.net/helloService")
public class HelloServiceImpl implements HelloService{
public String hello(String givenname, String name) {
return "Hello " + givenname + " " + name + "!";
}
}
和我的應用程序上下文
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="helloServiceImpl" class="guru.service.HelloServiceImpl" />
<jaxws:endpoint id="helloService"
implementor="#helloServiceImpl" address="/helloService"/>
</beans>
我可以用這個URL
http://192.168.1.209:8080/sampleWebService/helloService?wsdl
使用的soapUI 3.6.1看到我的WSDL定義軟件我創建了這個服務的SOAP請求
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hel="http://www.local-guru.net/helloService">
<soapenv:Header/>
<soapenv:Body>
<hel:hello>
<hel:givenname>MAYA</hel:givenname>
<hel:name>MANI</hel:name>
</hel:hello>
</soapenv:Body>
</soapenv:Envelope>
我的服務回覆爲
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:helloResponse xmlns:ns2="http://www.local-guru.net/helloService">
<return>Hello MAYA MANI!</return>
</ns2:helloResponse>
</soap:Body>
</soap:Envelope>
但是當我嘗試使用jQuery我的服務調用,我得到這個錯誤
警告:攔截器 {http://www.local-guru.net/helloService}HelloService有拋出 異常,現在展開org.apache.cxf.binding.soap.SoapFault:錯誤 讀取XMLStreamReader。在 org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.java:230) 在 org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.java:60) 在 org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:243) 在 org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:109) 在 org.apache.cxf。 transport.servlet.ServletDestination.invoke(ServletDestination.java:98) at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:406) at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:178) 在 org.apache.cxf.transport.servlet.AbstractCXFServlet.invoke(AbstractCXFServlet.java:142) 在 org.apache .cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179) 在 org.apache.cxf.transport.servlet.AbstractHTTPServlet.doOptions(AbstractHTTPServlet.java:132) 在javax.servlet.http.HttpServlet。服務(HttpServlet.java:718)在 org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:159) 在 org.apache.catalina.core.ApplicationFilterChain。internalDoFilter(ApplicationFilterChain.java:252) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) 在 org.apache.catalina .valves.ErrorReportValve.invoke(ErrorReportValve.java:105) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) 在 org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) 在 org.apache.coyote.http11 .Http11Protocol $ Http11ConnectionHandler.processConnection(Http11Protocol.java:744) 在 org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527) 在 org.apache.tomcat.util.net.LeaderFollowerWorkerThread .runIt(LeaderFollowerWorkerThread.java:80) at org.apache.tomcat.util.threads.ThreadPool $ ControlRunnable.run(ThreadPool.java:684) at java.lang.Thread.run(Thread.java:619)造成者: c om.ctc.wstx.exc.WstxEOFException:在序言中的意外EOF [row,col {unknown-source}]:[1,0] com.ctc.wstx.sr.StreamScanner.throwUnexpectedEOF(StreamScanner.java: 686) 在 com.ctc.wstx.sr.BasicStreamReader.handleEOF(BasicStreamReader.java:2134) 在 com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2040) 在 com.ctc .wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1069) 在 com.ctc.wstx.sr.BasicStreamReader.nextTag(BasicStreamReader.java:1095) 在 org.apache.cxf.binding.soap.interceptor .ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.java:122) ... 25多個
而我的HTML頁面
<html>
<head>
<script src="jquery.js"></script>
<script>
function callservice() {
var givenname = $("#givenName").val();
var name = $("#name").val();
var q ='<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hel="http://www.local-guru.net/helloService"><soapenv:Header/><soapenv:Body><hel:hello><hel:givenname>'+givenname+'</hel:givenname><hel:name>'+name+'</hel:name></hel:hello></soapenv:Body></soapenv:Envelope>'
$.ajax({
url: 'http://192.168.1.209:8080/sampleWebService/helloService/hello',
data: q,
type: 'post',
contentType: 'text/xml; charset="utf-8"',
success: function(res) {
$('#result').html(
res.getElementsByTagNameNS("http://www.local-guru.net/helloService",
"helloResponse")[0].childNodes[0].childNodes[0].nodeValue);
},
});
}
</script>
</head>
<body>
<form>
<input type="text" id="givenName"/>
<input type="text" id="name"/>
<input type="button" onClick="callservice()"/>
</form>
<div id="result">result</div>
</body>
</html>
請幫我解決這個問題。
你找到解決方案? – surlac 2013-08-07 07:05:23