JAVA的WebService規范JAX-WS


JAX-WS的服務端、客戶端雙方傳輸數據使用的SOAP消息格式封裝數據。

 

一、下載apache-cxf-3.1.4.zip。

二、編寫服務端

1、編寫一個Web Service用來傳輸參數的類

package com.ws.services.entity;

import java.util.Date;

import javax.xml.bind.annotation.XmlRootElement;
/**
 * 該類為Web Service中的參數、返回值類型,故需要使用JAXB注解告訴CXF如何在XML和Java Object之間處理,
 * 因為,SOAP消息格式包裝的是一段XML代碼,無論是服務器端,還是客戶端,
 * 在接收到SOAP消息時,都需要將XML轉化為Java Object,
 * 在發送SOAP消息時,需要將Java Object轉化為XML。
 * */
@XmlRootElement(name = "People")
public class People {
    private Long id;
    private String name;
    private Date birthday;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

 

2、編寫Web Service接口

package com.ws.services;

import java.util.List;
import javax.jws.WebService;
import com.ws.services.entity.People;

@WebService
public interface PeopleService {
    
    public String add(People people);
    
    public String del(People people);
    
    public String modify(People people);
    
    public People getOne(Long id);
    
    public List<People> getList(String name);
}

 

4、實現Web Service

 

package com.ws.services;

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

import javax.jws.WebService;

import com.ws.services.entity.People;

@WebService(endpointInterface="com.ws.services.PeopleService")
public class PeopleServiceImpl implements PeopleService {

    @Override
    public String add(People people) {
        // TODO Auto-generated method stub
        System.out.println("ADD:"+people.getId()+","+people.getName()+","+people.getBirthday());
        return "ADD SUCCESS";
    }

    @Override
    public String del(People people) {
        // TODO Auto-generated method stub
        System.out.println("DEL:"+people.getId()+","+people.getName());
        return "DEL SUCCESS";
    }

    @Override
    public String modify(People people) {
        // TODO Auto-generated method stub
        System.out.println("MODIFY:"+people.getId()+","+people.getName());
        return "MODIFY SUCCESS";
    }

    @Override
    public People getOne(Long id){
        // TODO Auto-generated method stubSystem.out.println("QRY BEGIN");
        People people=new People();
        people.setId(4L);
        people.setName("Name-004");
        people.setBirthday(new Date());
        return people;
        
    }
    @Override
    public List<People> getList(String name){
        // TODO Auto-generated method stub
        List<People> list=new ArrayList<People>();
        People people0=new People();
        People people1=new People();
        people0.setId(5L);
        people0.setName(name+"-005");
        people0.setBirthday(new Date());
        people1.setId(6L);
        people1.setName(name+"-006");
        people1.setBirthday(new Date());
        list.add(people0);
        list.add(people1);
        return list;
    }
}

 

5、發布Web Service服務

 

package com.ws.services;

import javax.xml.ws.Endpoint;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class SoapServer {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        /**
         * 方法一,使用javax.xml.ws.*包中的EndPoint的靜態方法publish()發布Web服務
         * */
        //Endpoint.publish("http://127.0.0.1:80/peopleService", new PeopleServiceImpl());
        
        /**
         * 方法二,使用CXF特有的API---JaxWsServerFactoryBean發布Web服務,
         * 並且我們對服務端工廠Bean的輸入攔截器集合、輸出攔截器集合中分別添加了日志攔截器,
         * 可以在Web服務端發送和接收消息時輸出信息。
         */
        JaxWsServerFactoryBean soapFactoryBean = new JaxWsServerFactoryBean();
        soapFactoryBean.getInInterceptors().add(new LoggingInInterceptor());
        soapFactoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
        // 注意這里是實現類不是接口
        soapFactoryBean.setServiceClass(PeopleServiceImpl.class);
        soapFactoryBean.setAddress("http://127.0.0.1:80/peopleService");
        soapFactoryBean.create();
                
        
        System.out.println("published...");
    }
}

 

 

 

6、測試服務發布情況

運行Java Application,訪問http://127.0.0.1/peopleService?wsdl

  <?xml version="1.0" encoding="UTF-8" ?> 
- <!--  Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. 
  --> 
- <!--  Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. 
  --> 
- <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://services.ws.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://services.ws.com/" name="PeopleServiceImplService">
- <types>
- <xsd:schema>
  <xsd:import namespace="http://services.ws.com/" schemaLocation="http://127.0.0.1/peopleService?xsd=1" /> 
  </xsd:schema>
  </types>
- <message name="add">
  <part name="parameters" element="tns:add" /> 
  </message>
- <message name="addResponse">
  <part name="parameters" element="tns:addResponse" /> 
  </message>
- <message name="modify">
  <part name="parameters" element="tns:modify" /> 
  </message>
- <message name="modifyResponse">
  <part name="parameters" element="tns:modifyResponse" /> 
  </message>
- <message name="getOne">
  <part name="parameters" element="tns:getOne" /> 
  </message>
- <message name="getOneResponse">
  <part name="parameters" element="tns:getOneResponse" /> 
  </message>
- <message name="getList">
  <part name="parameters" element="tns:getList" /> 
  </message>
- <message name="getListResponse">
  <part name="parameters" element="tns:getListResponse" /> 
  </message>
- <message name="del">
  <part name="parameters" element="tns:del" /> 
  </message>
- <message name="delResponse">
  <part name="parameters" element="tns:delResponse" /> 
  </message>
- <portType name="PeopleService">
- <operation name="add">
  <input wsam:Action="http://services.ws.com/PeopleService/addRequest" message="tns:add" /> 
  <output wsam:Action="http://services.ws.com/PeopleService/addResponse" message="tns:addResponse" /> 
  </operation>
- <operation name="modify">
  <input wsam:Action="http://services.ws.com/PeopleService/modifyRequest" message="tns:modify" /> 
  <output wsam:Action="http://services.ws.com/PeopleService/modifyResponse" message="tns:modifyResponse" /> 
  </operation>
- <operation name="getOne">
  <input wsam:Action="http://services.ws.com/PeopleService/getOneRequest" message="tns:getOne" /> 
  <output wsam:Action="http://services.ws.com/PeopleService/getOneResponse" message="tns:getOneResponse" /> 
  </operation>
- <operation name="getList">
  <input wsam:Action="http://services.ws.com/PeopleService/getListRequest" message="tns:getList" /> 
  <output wsam:Action="http://services.ws.com/PeopleService/getListResponse" message="tns:getListResponse" /> 
  </operation>
- <operation name="del">
  <input wsam:Action="http://services.ws.com/PeopleService/delRequest" message="tns:del" /> 
  <output wsam:Action="http://services.ws.com/PeopleService/delResponse" message="tns:delResponse" /> 
  </operation>
  </portType>
- <binding name="PeopleServiceImplPortBinding" type="tns:PeopleService">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 
- <operation name="add">
  <soap:operation soapAction="" /> 
- <input>
  <soap:body use="literal" /> 
  </input>
- <output>
  <soap:body use="literal" /> 
  </output>
  </operation>
- <operation name="modify">
  <soap:operation soapAction="" /> 
- <input>
  <soap:body use="literal" /> 
  </input>
- <output>
  <soap:body use="literal" /> 
  </output>
  </operation>
- <operation name="getOne">
  <soap:operation soapAction="" /> 
- <input>
  <soap:body use="literal" /> 
  </input>
- <output>
  <soap:body use="literal" /> 
  </output>
  </operation>
- <operation name="getList">
  <soap:operation soapAction="" /> 
- <input>
  <soap:body use="literal" /> 
  </input>
- <output>
  <soap:body use="literal" /> 
  </output>
  </operation>
- <operation name="del">
  <soap:operation soapAction="" /> 
- <input>
  <soap:body use="literal" /> 
  </input>
- <output>
  <soap:body use="literal" /> 
  </output>
  </operation>
  </binding>
- <service name="PeopleServiceImplService">
- <port name="PeopleServiceImplPort" binding="tns:PeopleServiceImplPortBinding">
  <soap:address location="http://127.0.0.1/peopleService" /> 
  </port>
  </service>
  </definitions>

 

三、編寫客戶端代碼

1、使用WSDL2Java生成Web Service客戶端代碼

(1)配置CXF環境變量

path中加入apache-cxf-3.1.4\bin的絕對路徑。

 

(2)使用WSDL2Java生成Web Service客戶端代碼,命令如下:

wsdl2java -p com.ws.client -d D:\\src -client http://127.0.0.1/peopleService?wsdl

 

將生成的類拷入Web Service客戶端工程中。

2、編寫客戶端測試方法

package com.ws;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.List;

import javax.xml.namespace.QName;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl;
import com.ws.client.People;
import com.ws.client.PeopleService;
import com.ws.client.PeopleServiceImplService;

public class SoapClient {

    public static void main(String[] args) throws ParseException, MalformedURLException {
        // TODO Auto-generated method stub
        /**
         * 使用准的JAX-WS的API完成客戶端調用
         * */
        //使用Web服務的WSDL中的targetNamespace和<wsdl:service …中的name屬性構建了javax.xml.namespace.QName接口
        QName qName = new QName("http://services.ws.com/", "PeopleServiceImplService");
        PeopleServiceImplService peopleServiceImplService = new PeopleServiceImplService(new URL("http://127.0.0.1/peopleService?wsdl"), qName);
        PeopleService ps = (PeopleService) peopleServiceImplService.getPort(PeopleService.class);
        
        /**
         * 使用CXF 的JaxWsProxyFactoryBean來完成客戶端調用
         * */
        /*JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean();
        soapFactoryBean.setAddress("http://127.0.0.1:80/peopleService");
        soapFactoryBean.setServiceClass(PeopleService.class);
        Object o = soapFactoryBean.create();
        PeopleService ps = (PeopleService) o;*/
        
        
        People p1 = new People();
        p1.setId(1L);
        p1.setName("陳一");
        GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance();
        calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse("1989-01-28"));
        p1.setBirthday(new XMLGregorianCalendarImpl(calendar));
        ps.add(p1);

        People p4 = ps.getOne(4L);
        System.out.println("4:" + p4.getId() + "," + p4.getName() + "," + p4.getBirthday());

        List<People> p5 = ps.getList("王五");
        for (People p : p5) {
            System.out.println("5:" + p.getId() + "," + p.getName() + "," + p.getBirthday());
        }
    }

}

 

最后,進行測試。

 

備注:

webservice服務端啟動時,報錯:prefix wsdp is not bound to a namespace,去掉下列四個jar包,

cxf-services-ws-discovery-api-3.1.4.jar

cxf-services-ws-discovery-service-3.1.4.jar

cxf-services-wsn-api-3.1.4.jar

cxf-services-wsn-core-3.1.4.jar


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM