当前位置 : 主页 > 网页制作 > Nodejs >

Webservice的cxf开发_使用spring发布服务+ajax调用服务

来源:互联网 收集:自由互联 发布时间:2021-06-24
1.在spring配置文件中配置 cxf.xml ?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"xmlns:jaxrs

1.在spring配置文件中配置

cxf.xml


<?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"
	xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
	
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   
    http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
    http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
   ">
   <!-- 以上是cxf整合spring需要的命名空间 -->
   
   <!-- 引入CXF Bean定义如下 -->
	<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" />
	
	<!-- 发布 -->
	<jaxws:server id="myservice" address="/myservice"
	                     serviceClass="cn.xt.test.IMyService">
		<jaxws:serviceBean>
			<bean class="cn.xt.test.MyService"></bean>
		</jaxws:serviceBean>
		
		<jaxws:inInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
		</jaxws:inInterceptors>
		<jaxws:outInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
		</jaxws:outInterceptors>
	</jaxws:server>

</beans>



2.web.xml加载cxf.xml,并配置访问服务的servlet路径


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <context-param>
 	<param-name>contextConfigLocation</param-name>
 	<param-value>
		classpath:cxf.xml 	
 	</param-value>
 </context-param>
 <listener>
 	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <servlet>
 	<servlet-name>cxf</servlet-name>
 	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 	<servlet-name>cxf</servlet-name>
 	<url-pattern>/ws/*</url-pattern>
 </servlet-mapping>
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

3.将web工程发布到tomcat,访问http://localhost:8080/CxfTest/ws

出现如下信息,代表发布服务成功




4.使用ajax调用


//myxml可以在调用服务端时,LoggingInInterceptor拦截器的打印信息中看到
  			var myxml = '<?xml version="1.0" ?><S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope"><S:Body><sayHi xmlns="http://test.xt.cn/"/></S:Body></S:Envelope>';
  			$.ajax({
				url:'http://localhost:8080/CxfTest/ws/myservice',
				dataType:'xml',
				type:'post',
				contentType:'application/soap+xml;charset="UTF-8"',
				data:myxml,
				success:function(data){
					//返回的data是
					//<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Body><sayHiResponse xmlns="http://test.xt.cn/"/></soap:Body></soap:Envelope>
					alert(data)
					alert($(data).find("sayHiResponse").attr("xmlns"))
				}
		     },"xml");
  		});


调用结果:

网友评论