這里不介紹原理,只是記錄自己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 id="reimBillHandler" class="com.ufgov.lp.xml.sax.handler.ReimBillHandler" /> <bean id="reciveBillServiceImpl" 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 定義接口
import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; /** * * <p>接收報文接口</p> * @author shangcg * @since 2017年11月1日 */ @WebService public interface ReciveBillService { @WebMethod public String request(@WebParam(name = "xmlStr") String xmlStr); }
5 創建實現類
import javax.annotation.Resource; import javax.jws.WebService; import com.ufgov.lp.bill.webservice.ReciveBillService; import com.ufgov.lp.bill.webservice.bean.LpBizBillDataCollect; import com.ufgov.lp.xml.sax.handler.ReimBillHandler; @WebService public class ReciveBillServiceImpl implements ReciveBillService { @Resource ReimBillHandler reimBillHandler; @Override public String request(String xmlStr) { try { /**與數據庫結構一致的javaBean對象*/ LpBizBillDataCollect lpBizBillDataCollect = new LpBizBillDataCollect(); //把傳過來的報文直接裝到對象中(實際上業務系統一般得對XML解析,這里不說解析) lpBizBillDataCollect.setField01(xmlStr); //插入數據庫(reimBillHandler注入進來才能調用方法) reimBillHandler.excuteBillInsert(lpBizBillDataCollect); } catch (Exception e) { e.printStackTrace(); } return "這里是返回的報文"; } public ReimBillHandler getReimBillHandler() { return reimBillHandler; } //注意這里的set方法,不然稍后配置bean后會注入不進來 public void setReimBillHandler(ReimBillHandler reimBillHandler) { this.reimBillHandler = reimBillHandler; } }
6看ReimBillHandler (這個類就是本來業務系統已經存在的用@service標注的類,成功注入該類后,如果該類在注入其他類咱們就可以不用管了)
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ufgov.lp.bill.dao.LpBizBillDao; import com.ufgov.lp.bill.webservice.bean.LpBizBillDataCollect; /** * * <p>注入LpBizBillDao插入數據</p> * @author shangcg * @since 2017年11月3日 */ @Service public class ReimBillHandler{ @Autowired LpBizBillDao lpBizBillDao; /**插入數據庫表數據*/ public int excuteBillInsert(LpBizBillDataCollect lpBizBillDataCollect){ return lpBizBillDao.insert(lpBizBillDataCollect);//插入數據庫數據 } }
說一下這些過程中我遇到的錯誤:
1 spring的bean注入不進來 解決:以@WebService注解的類,引用spring中bean事需要干兩件事,第一是用@Resource注解注入,不是@autowire; 第二必須有set方法 ; 第三是ApplicationContext.xm必須配置bean和引用