gpt4 book ai didi

java - 如何从 JQuery 调用 soap web 服务

转载 作者:行者123 更新时间:2023-11-28 02:08:49 27 4
gpt4 key购买 nike

我开发了一个从 JQuery 调用 soap web 服务的示例应用程序,但我在寻找服务时遇到了一些问题。我正在使用 apache cxf 2.2.6 jar 。

I used this
http://www.local-guru.net/blog/2010/1/24/calling-webservices-from-mootools-jquery-or-dojo to create a webservice cal from 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 查看我的 WSDl 定义

http://192.168.1.209:8080/sampleWebService/helloService?wsdl

我使用 soapUI 3.6.1 软件为此服务创建了 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 调用我的服务时,出现此错误

WARNING: Interceptor for {http://www.local-guru.net/helloService}HelloService has thrown exception, unwinding now org.apache.cxf.binding.soap.SoapFault: Error reading XMLStreamReader. at org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.java:230) at org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.java:60) at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:243) at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:109) at 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) at org.apache.cxf.transport.servlet.AbstractCXFServlet.invoke(AbstractCXFServlet.java:142) at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179) at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doOptions(AbstractHTTPServlet.java:132) at javax.servlet.http.HttpServlet.service(HttpServlet.java:718) at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:159) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) at 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) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527) at 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) Caused by: com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog at [row,col {unknown-source}]: [1,0] at com.ctc.wstx.sr.StreamScanner.throwUnexpectedEOF(StreamScanner.java:686) at com.ctc.wstx.sr.BasicStreamReader.handleEOF(BasicStreamReader.java:2134) at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2040) at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1069) at com.ctc.wstx.sr.BasicStreamReader.nextTag(BasicStreamReader.java:1095) at org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.java:122) ... 25 more

我的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>

请帮我解决这个问题。

最佳答案

根据您发布的错误,在我看来,您的 Ajax 请求中的 XML 负载格式不正确或不是服务器所期望的,您是否检查了浏览器中的请求和响应?在这种情况下(通常会出现 IME),了解预期的 SOAP 请求与实际发送的内容之间的区别是非常宝贵的。

我建议,如果上述情况属实,那么您很可能遇到行尾问题或 xml 的“q”字符串编码问题。要进行测试,请尝试这样的操作:http://www.w3schools.com/dom/dom_loadxmldoc.asp

它从磁盘解析一个“格式良好”的 xml 文件,然后发送它:-)

关于java - 如何从 JQuery 调用 soap web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9586709/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com