1. 程式人生 > >基於spring+hibernate+cxf寫的webservice服務,詳細配置

基於spring+hibernate+cxf寫的webservice服務,詳細配置

1.首先下載Apache-cxf的jar包

2.搭建好spring+hibernate環境

搭建好spring+hibernate環境,確保可以正常執行!這裡具體省略!

3.引入cxf的jar包到專案

這裡我們選擇將所有jar包匯入

4.完成WebService服務端程式碼

IService介面程式碼

@WebService  
public interface IService {  

    public void hello(@WebParam(name="username") String username);
}  

ServiceImpl實現介面程式碼:


@Repository
@Transactional @WebService public class ServiceImp implements IService{ @Override public void hello(@WebParam(name = "username") String username) { System.out.println("hello " + username + " now is " + new Date()); } }

5.完善cxf和spring結合部分配置檔案

除過spring-hibernate.xml,spring-servlet.xml配置外,增加
spirng-cxf.xml

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:aop="http://www.springframework.org/schema/aop"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:cache
="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <bean id="service1" class="com.sgcc.mcsas.fbsservice.webservice.HelloWorldImpl"></bean> <jaxws:endpoint id="hello" implementor="#service1" address="/HelloWorldService"/> <bean id="service2" class="com.sgcc.mcsas.fbsservice.openWebService.AbnormalSignImpl"></bean> <jaxws:endpoint id="hello2" implementor="#service2" address="/AbnormalSignImpl"/> 這裡可以寫多個service... </beans>

6.設定cxf的servlet,配置web.xml

除過spring-servlet外增加cxf-servlet

    <!-- cxf-servlet -->
    <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

7.生成客戶端程式碼測試

開啟apache-cxf的bin包,下面可以發現很多可執行的bat檔案,

這裡我們首先啟動tomcat確保專案不會報錯,
瀏覽器進入這個地址:
localhost:8080/專案名/services

就會看到有連結,點選,會發現一條末尾是wsdl的地址,我們複製瀏覽器的地址,

電腦輸入執行cmd命令

將路徑轉到apache-cxf bin目錄

執行 :

wsdl2java -client localhsot:8080/專案名/services/...?wsdl

自動生成客戶端。

會生成很多檔案,我們只關注結尾是client的java中的main方法即可!