主要内容:
CXF的介绍、安装和配置
CXF发布SOAP协议的服务
CXF+Spring整合发布SOAP的服务
CXF发布REST服务
什么是REST
CXF+Spring整合发布REST服务
综合案例
CXF介绍:
cxf是一个开源的webservice框架,提供很多完善功能,可以实现快速开发
cxf支持的协议:SOAP1.1/1.2,REST
cxf支持数据格式:XML,JSON(仅在REST方式下支持)
CXF的安装和配置:
下载后解压到文件夹,在环境变量中设置,CXF_HOME,然后在Path路径下设置bin的路径。
在cmd下加入wsdl2java -h
CXF发布SOAP协议的服务
第一步:创建web项目,并且导入jar包。
第二步:创建SEI接口,要加入@WebService
第三步:创建SEI实现类
第四步:配置spring配置文件,applicationContext.xml,用<jaxws:server 标签发布服务,
用<jaxws:server标签发布服务,设置1.服务地址;2.设置服务接口;3设置服务实现类
第五步:配置web.xml,配置spring配置文件地址和加载的listener,配置CXF的servlet
CXF+spring+mybatis实现服务发布:
1.导入相关的jar包
cxf的包+spring包+mybatis包+整合包
还有很多,就不一一截取了
操作中需要用的pojo(customer):
package com.tju.pojo;
/**
* 客户资料的pojo
* @author ***
*
*/
public class Customer {
//序号
private long c_id;
//客户卡号
private String c_use_id;
//小区名称
private String c_district;
//楼
private int c_bulid;
//单元
private int c_floor;
//室
private String c_room;
//客户名称
private String c_name;
//建筑面积
private float c_live_area;
//采暖面积
private float c_warm_area;
//客户类型
private String c_type;
//用热状态
private String c_state;
//分户控制类型
private String c_control;
//换热站
private String c_change_warm;
//手机号码
private String c_phone_num;
public long getC_id() {
return c_id;
}
public void setC_id(long c_id) {
this.c_id = c_id;
}
public String getC_use_id() {
return c_use_id;
}
public void setC_use_id(String c_use_id) {
this.c_use_id = c_use_id;
}
public String getC_district() {
return c_district;
}
public void setC_district(String c_district) {
this.c_district = c_district;
}
public int getC_bulid() {
return c_bulid;
}
public void setC_bulid(int c_bulid) {
this.c_bulid = c_bulid;
}
public int getC_floor() {
return c_floor;
}
public void setC_floor(int c_floor) {
this.c_floor = c_floor;
}
public String getC_room() {
return c_room;
}
public void setC_room(String c_room) {
this.c_room = c_room;
}
public String getC_name() {
return c_name;
}
public void setC_name(String c_name) {
this.c_name = c_name;
}
public float getC_live_area() {
return c_live_area;
}
public void setC_live_area(float c_live_area) {
this.c_live_area = c_live_area;
}
public float getC_warm_area() {
return c_warm_area;
}
public void setC_warm_area(float c_warm_area) {
this.c_warm_area = c_warm_area;
}
public String getC_type() {
return c_type;
}
public void setC_type(String c_type) {
this.c_type = c_type;
}
public String getC_state() {
return c_state;
}
public void setC_state(String c_state) {
this.c_state = c_state;
}
public String getC_control() {
return c_control;
}
public void setC_control(String c_control) {
this.c_control = c_control;
}
public String getC_change_warm() {
return c_change_warm;
}
public void setC_change_warm(String c_change_warm) {
this.c_change_warm = c_change_warm;
}
public String getC_phone_num() {
return c_phone_num;
}
public void setC_phone_num(String c_phone_num) {
this.c_phone_num = c_phone_num;
}
@Override
public String toString() {
return "Customer [c_id=" + c_id + ", c_use_id=" + c_use_id + ", c_district=" + c_district + ", c_bulid="
+ c_bulid + ", c_floor=" + c_floor + ", c_room=" + c_room + ", c_name=" + c_name + ", c_live_area="
+ c_live_area + ", c_warm_area=" + c_warm_area + ", c_type=" + c_type + ", c_state=" + c_state
+ ", c_control=" + c_control + ", c_change_warm=" + c_change_warm + ", c_phone_num=" + c_phone_num
+ "]";
}
public Customer(long c_id, String c_use_id, String c_district, int c_bulid, int c_floor, String c_room, String c_name,
float c_live_area, float c_warm_area, String c_type, String c_state, String c_control, String c_change_warm,
String c_phone_num) {
super();
this.c_id = c_id;
this.c_use_id = c_use_id;
this.c_district = c_district;
this.c_bulid = c_bulid;
this.c_floor = c_floor;
this.c_room = c_room;
this.c_name = c_name;
this.c_live_area = c_live_area;
this.c_warm_area = c_warm_area;
this.c_type = c_type;
this.c_state = c_state;
this.c_control = c_control;
this.c_change_warm = c_change_warm;
this.c_phone_num = c_phone_num;
}
public Customer() {
}
}
实现查询的mapper.java:
package com.tju.mapper;
import java.util.List;
import java.util.Map;
import com.tju.pojo.Customer;
public interface InsertDatabase {
public void insertData(Map customList);
}
其中mapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.tju.mapper.InsertDatabase" >
<insert id="insertData" parameterType="java.util.Map">
INSERT INTO Customer(c_id,c_use_id,c_district,c_bulid,c_floor,c_room,c_name,c_live_area,c_warm_area,c_type,c_state,c_control,c_change_warm,c_phone_num)
VALUES(#{c_id},#{c_use_id},#{c_district},#{c_bulid},#{c_floor} ,#{c_room},#{c_name},#{c_live_area},#{c_warm_area},#{c_type},#{c_state},#{c_control},#{c_change_warm},#{c_phone_num})
</insert>
</mapper>
2.下面是SEI的接口:
package com.tju.service;
import java.util.List;
import javax.jws.WebService;
import com.tju.pojo.Customer;
@WebService
public interface ExcelToData{
public List<Customer> paraseExcel(String fileName) throws Exception;
}
3.下面是SEI的实现类,实现的主要从Excel读取数据到Mysql,下面主要实现从Excel中读取,然后将读到的数据存入到Map中,因为List形式的数据不能插入到mybatis链接的数据库中,只能使用Map的形式。
package com.tju.service.impl;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tju.mapper.InsertDatabase;
import com.tju.pojo.Customer;
import com.tju.service.ExcelToData;
@Service
public class ExcelToDataImpl implements ExcelToData{
@Autowired
private InsertDatabase insertData;
@Override
public List<Customer> paraseExcel(String fileName) throws Exception {
List<Customer> list = new ArrayList<Customer>();
InputStream stream = new FileInputStream(fileName);
/*XSSFWorkbook workbook = new XSSFWorkbook(stream);
XSSFSheet sheet = workbook.getSheet("Sheet1");*/
HSSFWorkbook workbook = new HSSFWorkbook(stream);
HSSFSheet sheet = workbook.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();
for(int i=3;i<rows;i++){
Customer customer = new Customer();
//序号
long c_id = 0;
//客户卡号
String c_use_id = null;
//小区名称
String c_district = null;
//楼
int c_bulid = 0;
//单元
int c_floor = 0;
//室
String c_room = null;
//客户名称
String c_name = null;
//建筑面积
float c_live_area = 0;
//采暖面积
float c_warm_area = 0;
//客户类型
String c_type = null;
//用热状态0:不正常 1:正常
String c_state = "正常";
//分户控制类型
String c_control = null;
//换热站
String c_change_warm = null;
//手机号码
String c_phone_num = null;
//XSSFRow row = sheet.getRow(i);
HSSFRow row = sheet.getRow(i);
if(row!=null){
int cells = row.getPhysicalNumberOfCells();
for(int c=0;c<cells;c++){
//XSSFCell cell = row.getCell(c);
HSSFCell cell = row.getCell(c);
if(cell!=null){
String a = null;
switch (c) {
case 0:
a = cell.getStringCellValue();
c_id = Long.valueOf(a).longValue();
break;
case 1:
c_use_id = cell.getStringCellValue();
break;
case 2:
c_district = cell.getStringCellValue();
break;
case 3:
a = cell.getStringCellValue();
c_bulid = Integer.valueOf(a).intValue() ;
break;
case 4:
a = cell.getStringCellValue();
c_floor = Integer.valueOf(a).intValue() ;
break;
case 5:
c_room = cell.getStringCellValue();
break;
case 6:
c_name = cell.getStringCellValue();
break;
case 7:
a = cell.getStringCellValue();
c_live_area = Float.valueOf(a).floatValue();
break;
case 8:
a = cell.getStringCellValue();
c_warm_area = Float.valueOf(a).floatValue();
break;
case 9:
c_type = cell.getStringCellValue();
break;
case 10:
c_state = cell.getStringCellValue();
break;
case 11:
c_control = cell.getStringCellValue();
break;
case 12:
c_change_warm = cell.getStringCellValue();
break;
case 13:
c_phone_num = cell.getStringCellValue();
break;
default:
break;
}
}
}
//将从excel取出的数据存入到customer的pojo中
customer.setC_id(c_id);
customer.setC_bulid(c_bulid);
customer.setC_change_warm(c_change_warm);
customer.setC_control(c_control);
customer.setC_district(c_district);
customer.setC_floor(c_floor);
customer.setC_live_area(c_live_area);
customer.setC_name(c_name);
customer.setC_phone_num(c_phone_num);
customer.setC_room(c_room);
customer.setC_state(c_state);
customer.setC_type(c_type);
customer.setC_use_id(c_use_id);
customer.setC_warm_area(c_warm_area);
//将customer放到list中
list.add(customer);
}
}
//依次取出list中的customer插入到map中
Map<String, Object> map = null;
for (Customer intest : list) {
map = new HashMap<String, Object>();
map.put("c_bulid", intest.getC_bulid());
map.put("c_change_warm", intest.getC_change_warm());
map.put("c_control", intest.getC_control());
map.put("c_district", intest.getC_district());
map.put("c_floor", intest.getC_floor());
map.put("c_id", intest.getC_id());
map.put("c_live_area", intest.getC_live_area());
map.put("c_name", intest.getC_name());
map.put("c_phone_num", intest.getC_phone_num());
map.put("c_room", intest.getC_room());
map.put("c_state", intest.getC_state());
map.put("c_type", intest.getC_type());
map.put("c_use_id", intest.getC_use_id());
map.put("c_warm_area", intest.getC_warm_area());
//调用mybatis依次插入到数据中
insertData.insertData(map);
}
return list;
}
}
配置spring的文件:application-dao.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置数据源,dbcp -->
<!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置数据源 ,dbcp -->
<!--<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="30" />
<property name="maxIdle" value="5" />
</bean> -->
<!-- 使用c3p0来进行数据库的链接 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="5" />
</bean>
<!-- sqlSessionFacotry -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 使用mapper接口的扫描器,自动生成mapper接口的实例化对象,对象为接口名称的小写,其作用和下面相似,能够自动扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.tju.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 使用MapperFactoryBean能将mapper接口实例化,不用再生成类和对象,直接调用实例化的接口对象就能够调用方法。 -->
<!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="cn.itcast.ssm.mapper.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean> -->
</beans>
其中applicationContext-server.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">
<!-- <jaxws:server发布SOAP协议的服务 ,对JaxWsServerFactoryBean类封装-->
<jaxws:server address="/data" serviceClass="com.tju.service.ExcelToData">
<jaxws:serviceBean>
<ref bean="weatherInterface"/>
</jaxws:serviceBean>
</jaxws:server>
<!-- 配置服务实现类 -->
<bean name="weatherInterface" class="com.tju.service.impl.ExcelToDataImpl"/>
</beans>
其中applicationContext-transaction.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="txadvice" pointcut="execution(* cn.itcast.ssm.service.impl.*.*(..))"/>
</aop:config>
</beans>
设置mybatis/SqlMapConfig.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置setting全局,根据需要添加 -->
<!-- 配置别名 -->
<typeAliases>
<package name="com.tju.pojo"/>
</typeAliases>
<!-- mapper不用配置 ,使用spring和mybatis的整合包进行mapper的扫描,必须遵循一定的规则-->
<!-- <mappers>
<mapper resource="sqlmap/User.xml"/>
<package name="cn.itcast.ssm.mapper"/>
</mappers>
-->
</configuration>
下面是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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>cxf_spring</display-name>
<!-- 设置spring的环境 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置CXF的servlet -->
<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>/*</url-pattern>
</servlet-mapping>
<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>
</web-app>
先验证服务是否发布成功:http://127.0.0.1:8080/project_cxf/data?wsdl
然后是客户端的开发:
建立客户端java项目:
导入相关的jar包:
生成客户端的代码:wsdl2java -p com.tju.client -d . http://127.0.0.1:8080/project_cxf/data
然后编写spring/applicationContext.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">
<!-- <jaxws:client发布SOAP协议的服务 ,对JaxWsServerFactoryBean类封装-->
<jaxws:client id="excelToData" address="http://127.0.0.1:8080/project_cxf/data" serviceClass="com.tju.client.ExcelToData"></jaxws:client>
</beans>
然后编写main中的函数:
package com.tju.client.main;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tju.client.Customer;
import com.tju.client.ExcelToData;
import com.tju.client.Exception_Exception;
public class dataClient {
public static void main(String[] args) throws Exception {
//初始化spring的上下文
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ExcelToData data = (ExcelToData) context.getBean("excelToData");
data.paraseExcel("C:\\客户资料管理_20170310092009.xls");
}
}