1. 程式人生 > >cxf整合spring釋出webservice

cxf整合spring釋出webservice

1.引入jar包    我是從csdn下載下來的,然後就自己儲存下來了,http://download.csdn.net/detail/kai763253075/9605349

2.編寫介面類,其中總共釋出了兩個介面方法 sayHello 和sayNihao

package com.webservice;

import javax.jws.WebParam;  
import javax.jws.WebService;

@WebService
public interface ITestWebService {

    //@WebParam(name="name")為了增強可讀性可以不寫  
    public String sayHello(@WebParam(name = "name") String name);
    
    
    public User sayNihao(User user);
}
3.編寫實現類
package com.webservice;

import javax.jws.WebService;

@WebService(endpointInterface="com.webservice.ITestWebService")  
public class TestWebServiceImpl implements ITestWebService {

    public String sayHello(String name) {
        System.out.println("你好,"+ name);
        return "How are you " + name;
    }

    
    public User sayNihao(User user) {

        System.out.println("nihao:"+user.getName());
        
        user.setAge(80);
        
        return user;
    }
}

4.由於我自己寫的sayNihao()使用了User類這裡也順便寫一下,不是必要步驟不過一般實際開發中引數是實體類的情況太正常了。

package com.webservice;

public class User {

    private String name;
    
    private int age ;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
}

5.cxf配置檔案applicationContext-cxf.xml為了看得更檔案內容更清楚單獨生成一個xml檔案,

注意新增  

xmlns:jaxws="http://cxf.apache.org/jaxws"     

http://cxf.apache.org/jaxws    
 http://cxf.apache.org/schemas/jaxws.xsd"等內容,同時注意空格等格式 。

<?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"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://cxf.apache.org/jaxws    
    http://cxf.apache.org/schemas/jaxws.xsd">       
         
    <bean id="testWebService" class="com.webservice.TestWebServiceImpl"/>  
        <!--測試Service  -->  
      <jaxws:server serviceClass="com.webservice.ITestWebService" address="/test">  
        <jaxws:serviceBean>  
            <ref bean="testWebService"/>  
        </jaxws:serviceBean>  
    </jaxws:server>

</beans>

6spring配置檔案applicationContext.xml 把 applicationContext-cxf.xml加入進去,也可以一起配置到web.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-2.5.xsd  
                        http://www.springframework.org/schema/aop  
                        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
                        http://www.springframework.org/schema/tx  
                        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
                          
<!--配置CXF掃描,並制定包目錄  -->  
    <context:component-scan base-package="com.webservice"></context:component-scan>  
    <import resource="applicationContext-cxf.xml"/>  
 
</beans>

7.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

        <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>/WEB-INF/applicationContext.xml</param-value>  
    </context-param>  
 
      <!--spring監聽  -->  
    <listener>  
          <listener-class>  
              org.springframework.web.context.ContextLoaderListener  
         </listener-class>  
    </listener>  
 
    <!--CXF配置  -->  
    <servlet>  
        <servlet-name>CXFService</servlet-name>  
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>CXFService</servlet-name>  
        <url-pattern>/*</url-pattern>  
    </servlet-mapping>
    
    <display-name></display-name>    
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
</web-app>

8.最後部署tomcat釋出,訪問http://localhost:8080/b2bweb/檢視釋出的wenservice