cxf整合spring练习

一、环境搭建。

(a)最基本的Java环境
JAVA_HOME
(b)CXF_HOME
(c)ANT_HOME

(d)CATALINA_HOME
(e)PATH
(f)

CLASSPATH=.;%CXF_HOME%\lib\cxf-manifest.jar;.\build\classes

二、编写服务端代码

1.新建动态web项目cxf-spring并导入相关jar包

2.配置文件

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ws_web_server</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 修改配置文件加载路径第二种方式 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath*:cxf.xml,classpath*:beans.xml</param-value>
  </context-param>  
  
  <!-- 配置cxf -->
  <servlet>
  	<servlet-name>cxf</servlet-name>
  	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
   <!-- <init-param>
  		<param-name>config-location</param-name>
  		<param-value>classpath:cxf.xml</param-value>
  	</init-param>  --> 
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>cxf</servlet-name>
  	<url-pattern>/service/*</url-pattern>
  </servlet-mapping>
</web-app>

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.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 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:endpoint id="hello" address="/hello" implementor="cn.sp.web.HelloService">
		<jaxws:outInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
		</jaxws:outInterceptors>
		<jaxws:inInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
		</jaxws:inInterceptors>
	</jaxws:endpoint> -->
	<!-- 使用接口发布服务 -->
	<jaxws:server   address="/person"  serviceClass="cn.sp.wsService.PersonWsService">
		<jaxws:serviceBean>
			<bean class="cn.sp.wsService.PersonWsServiceImpl"></bean>
		</jaxws:serviceBean>
		<jaxws:outInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
		</jaxws:outInterceptors>
		<jaxws:inInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
		</jaxws:inInterceptors>
	</jaxws:server>
</beans>


beans.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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    
	<context:component-scan base-package="cn.sp"/>
</beans>

3.项目结构如图

package cn.sp.wsService;

import java.util.List;

import javax.jws.WebService;

import cn.sp.model.Person;
@WebService//注解要打在接口上
public interface PersonWsService {
public void add(Person person);
	
	public List<Person> getAllPerson();
}


package cn.sp.wsService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.sp.dao.PersonDao;
import cn.sp.model.Person;
@Service
public class PersonWsServiceImpl implements PersonWsService {
	@Autowired
	PersonDao personDao;

	@Override
	public void add(Person person) {
		personDao.add(person);
	}

	@Override
	public List<Person> getAllPerson() {
		return personDao.getAllPerson();
	}

}


就发布了两个方法,一个添加一个查询。

package cn.sp.dao;

import java.util.List;

import cn.sp.model.Person;

public interface PersonDao {
	public void add(Person person);
	
	public List<Person> getAllPerson();
}
package cn.sp.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import cn.sp.model.Person;
@Repository
public class PersonDaoImpl implements PersonDao {
	
	List<Person> list = new ArrayList<Person>();
	@Override
	public void add(Person person) {
		list.add(person);
	}

	@Override
	public List<Person> getAllPerson() {
		return list;
	}

}


package cn.sp.model;

public class Person {
	private String name;
	private String gender;
	private String address;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	
	
}

这里没有弄数据库,就用一个List来模拟的。
最后添加项目并启动tomcat。
打开doc命令窗口使用wsdl2java -d . http://localhost:8080/cxf-spring/service/person?wsdl命令获得Java代码,然后将其导入客户端中。
三、测试
补充一点儿使用jQuery异步调用webService的。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
	$(function(){
		
		$("#mybutton").click(function(){
			//获得文本的值
			var mytext = $("#mytext").val();
			//组装消息体
			var data = '<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">'
					+'<S:Body><ns2:sayBye xmlns:ns2="http://inter.server.web.rl.com/">'
					+'<arg0>'+mytext+'</arg0>'
					+'</ns2:sayBye>'
					+'</S:Body>'
					+'</S:Envelope>';
			$.ajax({
				url:'http://localhost:8080/cxf-web-server/service/bye',
				type:'post',
				dataType:'xml',//返回值的数据类型
				contentType:'text/xml;charset=UTF-8',//指定发送的数据类型
				data:data,//发送的消息体
				success:function(responseText){
					//解析消息体
					var returnObj = $(responseText).find("return");
					alert(returnObj.text());
				},
				error:function(){
					alert('system error');
				}
				
			});
		});
	});
</script>
</head>
<body>
<input type="text" id="mytext">
<input type="button" id="mybutton" value="jquery_invoke_ws">
</body>
</html>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值