簡單的Spring+CXF例子

服務端:java

步驟web

  1. 新建web工程
  2. 在此測試導入全部的cxf 的jar包
  3. 新建接口
  4. 新建實現
  5. 在實現類裏暴露(聲- 明)對應的業務接口
  6. 配置spring-cxf文件
  7. 配置web.xml文件
  8. 發佈查看wsdl文檔是否成功

 服務端的項目結構spring

//User類是一個bean對象apache

public class User {
    
    private String name;
    
    private int age;瀏覽器

    public String getName() {
        return name;
    }服務器

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

    public int getAge() {
        return age;
    }測試

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

    public User(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }url

    public User() {
        super();
    }
    
    
}

//HelloWorldI是一個接口

@WebService
public interface HelloWorldI {

    User getUserInfo(int id);
}

//注意須要加上@WebService註解

 

//HelloWorldImpl是一個實現類,實現類裏對應的方法裏有處理業務的邏輯

@WebService(endpointInterface = "com.sp.cxf.HelloWorldI")
public class HelloWorldImpl implements HelloWorldI {

    public User getUserInfo(int id) {
        System.out.println("service is start");
        if (id == 1) {
            return new User("aiwei",1);
        }
        return null;
    }

}

 

spring-cxf.xml是一個spring 的bean配置文件

<!-- cxf對外接口 -->
    <jaxws:endpoint id="hello" implementor="com.sp.cxf.HelloWorldImpl" address="/helloService" />

 

web.xml是整個web工程的配置文件

<!-- 配置spring-cxf.xml -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-cxf.xml</param-value>
    </context-param>

    <!-- 應用啓動的監聽器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- cxf WebService服務 -->
    <servlet>
        <servlet-name>cxfService</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxfService</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

 

把程序發佈到瀏覽器上,結果

http://localhost:8080/spring-cxf/helloService?wsdl 

測試:用Eclipse的內置瀏覽器來測試

輸入參數  1  ,結果是有的 。。 輸入參數2 ,結果是無的,但都請求到了服務器

請求的xml源文件格式

響應的源文件格式

 

 

新建客戶端:客戶端採用java原生的的打包和請求方式來測試

步驟:

  1. cmd窗口進入到client端項目的src文件路徑下
  2. 經過命令生成wsdl對應的文件
  3. 新建測試類,請求webservice,查看返回結果

進入到src 目錄後:wsimport - p com.ws.client  -keep http://localhost:8080/spring-cxf/helloService?wsdl

-p表示包名,無關緊要。

刷新項目:

 

新建main方法測試請求服務器

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HelloWorldImplService hw = new HelloWorldImplService();
        HelloWorldI hi = hw.getHelloWorldImplPort();
        User user = hi.getUserInfo(1);
        if(user!=null){
            System.out.println(user.getAge()+"  "+user.getName());
        }else{
            System.out.println("無返回結果");
        }
        
    }
}

控制檯:

更改請求參數爲  2

 

至此,一個簡單的webservice的服務器和客戶端就是這樣,後面再來實現下cxf自帶的攔截器和咱們自定義的攔截器功能。

最後附上一個關於WebService各個節點詳解的文章:

http://blog.csdn.net/sd0902/article/details/8606046

相關文章
相關標籤/搜索