Apache CXF 框架介紹,以及實現JAXWS和JSXRS規範的webservice服務

Apache CXF 框架介紹

Apache CXF =  Celtix + XFire, ApacheCXF 的前身叫做Apache CeltiXfire,現在已經正式更名爲Apache CXF了,CXF繼承了Celtix 和XFire兩大開源項目的精華,提供了對JAX-WS 全面的支持,並且提供了多種Binding、DataBing、Tramsport 以及各種對Format 的支持,並且可以根據實際項目的需要,採用代碼有限 (Code First)或者 WSDL 優先(WSDL First) 來輕鬆實現WebServices 的發佈和使用,目前它仍只是Apache 的一個孵化項目。

Apache CXF  是一個開源的 Services 框架,CXF 幫助您利用 Frontend 編程API 來構建和開發 Service,像JAX-WS,這些Services 可以支持多種協議,比如:SOAP、XML/HTTP、RESTful 或者CORBA,並且可以在多種傳輸協議上運行,比如: HTTP、JMS 或者JBI,CXF 大大簡化了Services 的創建,同時它繼承了XFire 傳統,一樣可以天然地和Spring 進行無縫集成。

功能特性

CXF 包含了大量的功能特性,但是主要集中在以下幾個方面:

(1)支持webServices 標準,包含SOAP、Basic Profile 、WS=addressing、WS-Policy、WS-ReableMessaging 和WS-Security。

(2)Frontends:CXF支持多種 Frontend 編程模型,CXF 實現了JAX-WS API (遵循JAX-WS 2.0 TCK版本),它包含一個“simple frontend”允許客戶端和EndPoint 的創建,而不需要Annotation 註解,CXF 即支持WSDL 優先開發,也支持從Java 的代碼優先開發模式。

(3)容易使用:CXF 設計的更加直觀與容易使用,有大量簡單的API 來快速構建代碼優先的Services 各種Maven 的插件也使得集成更加容易,支持JAX-WS API 支持Spring2.0更加簡化的XML 配置方式,等等。

實現JAXWS

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.demo</groupId>
  <artifactId>jaxws_service</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>jaxws_service</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!---jaxws-->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>3.3.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http-jetty</artifactId>
      <version>3.1.6</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.25</version>
    </dependency>
  </dependencies>
  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

 服務端:

package com.demo.service.facede;

import com.demo.service.ProductionService;

public class ProductionServiceFacade implements ProductionService {
    /**
     * 對外發布接口的方法
     *
     * @param name 名稱
     */
    @Override
    public String sayHello(String name) {
        return "Hello~ "+name;
    }
}
package com.demo.service.facede;

import com.demo.service.ProductionService;

public class ProductionServiceFacade implements ProductionService {
    /**
     * 對外發布接口的方法
     *
     * @param name 名稱
     */
    @Override
    public String sayHello(String name) {
        return "Hello~ "+name;
    }
}

發佈服務:

package com.demo.service;

import com.demo.service.facede.ProductionServiceFacade;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

/**
 * 發佈服務
 */
public class RegisterService {
    public static void main(String [] args){
        //發佈服務的工廠
        JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
        //設置webservice 服務地址
        factory.setAddress("http://localhost:8888/ws/hello");
        //設置提供服務的服務類
        factory.setServiceBean(new ProductionServiceFacade());
        //發佈服務
        factory.create();
        System.out.println("服務發成功端口爲:8888");
    }
}

 訪問WSDL 說明書地址

客戶端:

package com.demo.service;

import javax.jws.WebService;

@WebService
public interface ProductionService {
    /**
     * 對外發布接口的方法
     */
    public String sayHello(String name);
}
package com.demo;

import com.demo.service.HelloService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class Client {
    public static void main(String[] args) {
        //創建CXF 代理對象
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
        //設置訪問地址
        jaxWsProxyFactoryBean.setAddress("http://localhost:8888/ws/hello");
        //設置接口類型
        jaxWsProxyFactoryBean.setServiceClass(HelloService.class);
        //生成代理對象
        HelloService helloService = jaxWsProxyFactoryBean.create(HelloService.class);
        System.out.println(helloService.getClass());
       String s = helloService.sayHello("ZSF");
        System.out.println(s);
    }
}

這裏要注意的是客戶端和服務端的實現類的包路徑要一樣,不然就會出現 方法找不到,另外一個需要注意的是客戶端的實現類名可以隨便取。

客戶端包結構:                                                                          服務端包結構:

 

結果:

class com.sun.proxy.$Proxy35
Hello~ ZSF

實現JAXRS

客戶端:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.demo</groupId>
  <artifactId>JaxRsClient</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>JaxRsClient</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!---jaxrs-->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxrs</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http-jetty</artifactId>
      <version>3.1.6</version>
    </dependency>
    <!--客戶端調用restful 風格的依賴-->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-rs-client</artifactId>
      <version>3.0.1</version>
    </dependency>

    <!--規定數據傳輸格式的jar包既可以是xml 也可以是json 依賴開始-->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-rs-extension-providers</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.jettison</groupId>
      <artifactId>jettison</artifactId>
      <version>1.3.7</version>
    </dependency>
    <!--規定數據傳輸格式的jar包既可以是xml 也可以是json 依賴結束-->
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

 實體類

package com.demo.entity;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "User")
public class User {

    private Integer id;
    private String name;
    private String city;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }




}

接口

package com.demo.service;

import com.demo.entity.User;
import jdk.nashorn.internal.objects.annotations.Constructor;

import javax.ws.rs.*;
import java.util.List;

@Path("userService")
@Produces(value = "*/*")
public interface UserServer {
    @POST
    @Path("/user")
    @Consumes({"application/xml","application/json"})
    public void saveUser(User user);

    @PUT
    @Path("/user")
    @Consumes({"application/xml","application/json"})
    public void updateUser(User user);

    @GET
    @Path("/user")
    @Consumes({"application/xml","application/json"})
    public List<User> queryAllUser();

    @GET
    @Path("/user/{id}")
    @Consumes({"application/xml","application/json"})
    public User queryUser(@PathParam("id") Integer id);

    @DELETE
    @Path("/user/{id}")
    @Consumes({"application/xml","application/json"})
    public void deleteUser(@PathParam("id")Integer id);


}

實現類:

package com.demo.service.facade;

import com.demo.entity.User;
import com.demo.service.UserServer;

import java.util.ArrayList;
import java.util.List;

public class UserServerFacade implements UserServer {
    @Override
    public void saveUser(User user) {
        System.out.println("saveUser:"+user);
    }

    @Override
    public void updateUser(User user) {
        System.out.println("updateUser:"+user);
    }

    @Override
    public List<User> queryAllUser() {
        List<User> userList = new ArrayList<User>();
        User user1 = new User();
        user1.setId(1);
        user1.setName("張三丰");
        user1.setCity("北京");
        User user2 = new User();
        user2.setId(1);
        user2.setName("秦始皇");
        user2.setCity("蘭州");
        userList.add(user1);
        userList.add(user2);
        System.out.println("queryAllUser");

        return userList;
    }

    @Override
    public User queryUser(Integer id) {
        System.out.println("queryUser ID:" + id);
        if(id == 1) {
            User user1 = new User();
            user1.setId(1);
            user1.setName("張三丰");
            user1.setCity("北京");
            return user1;
        }else{
            return  null;
        }
    }

    @Override
    public void deleteUser(Integer id) {
        System.out.println("deleteUser ID:"+id);

    }
}

發佈服務類

package com.demo;

import com.demo.service.facade.UserServerFacade;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;

public class PushService {
    public static void main(String[] args) {
        //創建發佈服務工廠
        JAXRSServerFactoryBean jaxrsServerFactoryBean = new JAXRSServerFactoryBean();
        //設置服務地址
        jaxrsServerFactoryBean.setAddress("http://localhost:8001/ws/");
        //設置服務類
        jaxrsServerFactoryBean.setServiceBean(new UserServerFacade());
        //發佈服務
        jaxrsServerFactoryBean.create();
        System.out.println("服務發佈成功 端口爲8001");
    }
}

發佈服務

客戶端:

實體類

package com.demo.entity;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "User")
public class User {

    private Integer id;
    private String name;
    private String city;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

}

客戶端

package com.demo;

import com.demo.entity.User;
import org.apache.cxf.jaxrs.client.WebClient;
import org.junit.Test;

import javax.ws.rs.core.MediaType;


public class Client {
    @Test
    public void testSaveUser(){
        User user  = new User();
        user.setId(1);
        user.setName("張無忌");
        user.setCity("大理");
        WebClient.create("http://localhost:8001/ws/userService/user").type(MediaType.APPLICATION_JSON).post(user);

    }
    @Test
    public void testQueryUser(){
     WebClient.create("http://localhost:8001/ws/userService/user/1").type(MediaType.APPLICATION_JSON).get();
    }

    @Test
    public void testQueryAllUser(){
        WebClient.create("http://localhost:8001/ws/userService/user").type(MediaType.APPLICATION_JSON).get();
    }
    @Test
    public void testDeleteUser(){
        WebClient.create("http://localhost:8001/ws/userService/user/1").type(MediaType.APPLICATION_JSON).delete();
    }
    @Test
    public void testUpdateUser(){
        User user  = new User();
        user.setId(1);
        user.setName("張無忌");
        user.setCity("大理");
        WebClient.create("http://localhost:8001/ws/userService/user").type(MediaType.APPLICATION_JSON).put(user);
    }
}

服務端收到請求:

客戶端包路徑

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章