1. 程式人生 > >Webserver 基於ws超級簡單入門

Webserver 基於ws超級簡單入門

 

基於mavn 

專案目錄結構

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>web_server</groupId>
    <artifactId>web_server</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- 要進行jaxws 服務開發 -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.0.1</version>
        </dependency>

        <!-- 內建jetty web伺服器  -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.0.1</version>
        </dependency>

        <!-- 日誌實現 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.12</version>
        </dependency>
    </dependencies>

</project>

 

domain包裡就是一些實體類而已,沒什麼好說的

package cxf.domain;

public class Car {
   private Integer id;
   private String carName;
   private Double price;

   public Integer getId() {
      return id;
   }

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

   public String getCarName() {
      return carName;
   }

   public void setCarName(String carName) {
      this.carName = carName;
   }

   public Double getPrice() {
      return price;
   }

   public void setPrice(Double price) {
      this.price = price;
   }

   @Override
   public String toString() {
      return "Car [id=" + id + ", carName=" + carName + ", price=" + price + "]";
   }

}

 

package cxf.domain;

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

public class User {
   private Integer id;
   private String username;
   private String city;

   private List<Car> cars = new ArrayList<Car>();

   public Integer getId() {
      return id;
   }

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

   public String getUsername() {
      return username;
   }

   public void setUsername(String username) {
      this.username = username;
   }

   public String getCity() {
      return city;
   }

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

   public List<Car> getCars() {
      return cars;
   }

   public void setCars(List<Car> cars) {
      this.cars = cars;
   }

}

 

介面包

package cxf.server;


import cxf.domain.Car;
import cxf.domain.User;

import javax.jws.WebMethod;
import javax.jws.WebService;
import java.util.List;

@WebService//宣告這個介面是webserver介面
public interface IUserServer {

    @WebMethod//@WebMethod用於向外公佈,它修飾的方法是webservice方法,去掉也沒影響的,類似一個註釋資訊
    public String sayHello(String name);

    @WebMethod
    public List<Car> findCarsByUser(User user);
}

 

package cxf.server.serverimpl;

import cxf.domain.Car;
import cxf.domain.User;
import cxf.server.IUserServer;

import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List;

@WebService(endpointInterface = "cxf.server.IUserServer",serviceName = "userService")//endpointInterfacewebserver介面的全類名,serviceName 是訪問的名稱
public class UserserverIMpl implements IUserServer {
    // 簡單引數傳遞
    public String sayHello(String name) {
        return "say"+name;
    }

    // 複雜引數傳遞
    public List<Car> findCarsByUser(User user) {
        if ("xiaoming".equals(user.getUsername())) {
            List<Car> cars = new ArrayList<Car>();
            Car car1 = new Car();
            car1.setId(1);
            car1.setCarName("大眾途觀");
            car1.setPrice(200000d);
            cars.add(car1);

            Car car2 = new Car();
            car2.setId(2);
            car2.setCarName("現代ix35");
            car2.setPrice(170000d);
            cars.add(car2);

            return cars;
        } else {
            return null;
        }
    }
}

接下來就是啟動webserver了編寫啟動類

package cxf.ws;

import cxf.server.IUserServer;
import cxf.server.serverimpl.UserserverIMpl;

import javax.xml.ws.Endpoint;

public class WS_server {
    public static void main(String[] args) {
        // 使用CXF 將 UserService服務 註冊到網路上
        // 1 、 服務實現物件
        IUserServer userServer=new UserserverIMpl();
        // 2、 釋出服務地址
        String address="http://localhost:9989/userServer";
        // 3、 釋出服務
        Endpoint.publish(address,userServer);
        System.out.println("服務已經啟動...");


    }

}

 

執行可以看見控制檯

在遊覽器訪問地址

這裡意思說沒有呼叫

在路徑上加上?wsdl可以看到此webserver所提供的方法及其各種引數

接下來寫客戶端,我這裡為了方便把客戶端寫在一個專案裡面,你也可以重新新建一個專案來寫

package cxf.ws.client;


import cxf.domain.User;
import cxf.server.IUserServer;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class WebClient {
    public static void main(String[] args) {
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean=new JaxWsProxyFactoryBean();
        jaxWsProxyFactoryBean.setServiceClass(IUserServer.class);
        jaxWsProxyFactoryBean.setAddress("http://localhost:9989/userServer");
        // 建立呼叫遠端服務代理物件
        IUserServer proxy= (IUserServer) jaxWsProxyFactoryBean.create();
        // 呼叫代理物件 任何一個方法,都將通過網路 呼叫web服務
        String helloName = proxy.sayHello("張三");
        System.out.print(helloName);
        System.out.print("\r\n");
        User user = new User();
        user.setUsername("xiaoming");
        System.out.println(proxy.findCarsByUser(user));
    }
}

我們通過JaxWsProxyFactoryBean 來構建一個IUserServer的代理物件

傳入兩個基本引數

jaxWsProxyFactoryBean.setServiceClass(IUserServer.class);
jaxWsProxyFactoryBean.setAddress("http://localhost:9989/userServer");

通過create()方法獲得代理物件

這樣就可以呼叫IUserServer裡面的方法了

喜歡的小夥伴可以去主頁下載全部程式碼。