这里不介绍原理,只是记录自己spring+cxf的开发过程和遇到的问题
场景:第三方公司需要调用我们的业务系统,以xml报文的形式传递数据,之后我们解析报文存储到我们数据库生成业务单据;
WebService的框架由多种,这里选cxf,与Spring的集成比较好;
直接看代码
1项目用的maven,首先添加依赖(这个依赖啊 ,教程引用几个的都有,这个看需要吧,我是用了四个)
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf</artifactId> <version>2.7.11</version> <type>pom</type> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.11</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.7.11</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>2.7.11</version> </dependency>
2 配置web.xml,在原来基础上添加(这里我只是添加cxf配置,spring中基础的配置之前项目中肯定有,像什么context-param什么的我想你们肯定早就有了)
<!-- cxf --> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/webservice/*</url-pattern> </servlet-mapping>
3 配置applicationContext.xml(还是在原来的基础上添加如下)
3.1 首先在该xml添加命名空间,这是我在原来基础上添加的(红色部分是我自己添加的)
3.2 在原来基础上添加
<!-- cxf配置 --> <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 class="com.ufgov.lp.xml.sax.handler.ReimBillHandler" /> <bean class="com.ufgov.lp.bill.webservice.impl.ReciveBillServiceImpl"> <property name="reimBillHandler" ref="reimBillHandler"></property> </bean> <jaxws:endpoint id="reciveBillService"implementor="#reciveBillServiceImpl" address="/reciveBillService" /> <!-- cxf配置结束 -->
接下来开始写代码
4 定义接口
importjavax.jws.WebMethod; importjavax.jws.WebParam; importjavax.jws.WebService; /*** * <p>接收报文接口</p> * @authorshangcg * @since2017年11月1日 */ @WebService public interfaceReciveBillService { @WebMethod public String request(@WebParam(name = "xmlStr") String xmlStr); }
5 创建实现类
importjavax.annotation.Resource; importjavax.jws.WebService; importcom.ufgov.lp.bill.webservice.ReciveBillService; importcom.ufgov.lp.bill.webservice.bean.LpBizBillDataCollect; importcom.ufgov.lp.xml.sax.handler.ReimBillHandler; @WebService public class ReciveBillServiceImpl implementsReciveBillService { @Resource ReimBillHandler reimBillHandler; @Override publicString request(String xmlStr) { try{ /**与数据库结构一致的javaBean对象*/LpBizBillDataCollect lpBizBillDataCollect = newLpBizBillDataCollect(); //把传过来的报文直接装到对象中(实际上业务系统一般得对XML解析,这里不说解析) lpBizBillDataCollect.setField01(xmlStr); //插入数据库(reimBillHandler注入进来才能调用方法) reimBillHandler.excuteBillInsert(lpBizBillDataCollect); } catch(Exception e) { e.printStackTrace(); } return "这里是返回的报文"; } publicReimBillHandler getReimBillHandler() { returnreimBillHandler; } //注意这里的set方法,不然稍后配置bean后会注入不进来 public voidsetReimBillHandler(ReimBillHandler reimBillHandler) { this.reimBillHandler =reimBillHandler; } }
6看ReimBillHandler (这个类就是本来业务系统已经存在的用@service标注的类,成功注入该类后,如果该类在注入其他类咱们就可以不用管了)
importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.stereotype.Service; importcom.ufgov.lp.bill.dao.LpBizBillDao; importcom.ufgov.lp.bill.webservice.bean.LpBizBillDataCollect; /*** * <p>注入LpBizBillDao插入数据</p> * @authorshangcg * @since2017年11月3日 */@Service public classReimBillHandler{ @Autowired LpBizBillDao lpBizBillDao; /**插入数据库表数据*/ public intexcuteBillInsert(LpBizBillDataCollect lpBizBillDataCollect){ return lpBizBillDao.insert(lpBizBillDataCollect);//插入数据库数据 } }
说一下这些过程中我遇到的错误:
1 spring的bean注入不进来 解决:以@WebService注解的类,引用spring中bean事需要干两件事,第一是用@Resource注解注入,不是@autowire; 第二必须有set方法 ; 第三是ApplicationContext.xm必须配置bean和引用