Webservice之发布

WebService 简单来说就是一种跨编程语言和跨操作系统平台的远程调用技术。

     XML+XSD,SOAP和WSDL是构成WebService平台的三大技术

WebService开发主要分为服务器端开发和客户端开发两个方面

Service:

服务端接口

import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface Service {
    @WebMethod
    public String test(String word) ;
    @WebMethod
    public List<Student> getStudents();
}

接口实现类:

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

@WebService
public class ServiceImpl implements Service {
     
    public List<Student> getStudents() {
        List<Student> students = new ArrayList<Student>();
        students.add(new Student("aa", "aa"));
        students.add(new Student("bb", "bb"));
        students.add(new Student("cc", "cc"));
        students.add(new Student("dd", "dd"));

        return students;
    } 
    public String test(String word) {
        return "hello"+word;
    }

}

发布类(可以采用jdk发布,也可以采用cxf发布)

import javax.xml.ws.Endpoint;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class ServiceTest {
          public static void main(String[] args) {
          Service service = new ServiceImpl();
          String address = "http://localhost:1000/Exercise";
          //Jdk发布
//          Endpoint endpoint = Endpoint.publish(address, service);
         //CXF发布
          JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
          factoryBean.setAddress(address);
          factoryBean.setServiceClass(Service.class);
          factoryBean.setServiceBean(service);
          factoryBean.create();
          System.out.println("publish success");
        }
}

浏览器输入网址 http://localhost:1000/Exercise?wsdl

出现以下则发布成功

<wsdl:definitions
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:tns="http://service.webservice.geek.com/"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:ns1="http://schemas.xmlsoap.org/soap/http"
    name="ServiceImplService"
    targetNamespace="http://service.webservice.geek.com/">
    <wsdl:types>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:tns="http://service.webservice.geek.com/"
            elementFormDefault="unqualified"
            targetNamespace="http://service.webservice.geek.com/" version="1.0">
            <xs:element name="getStudents" type="tns:getStudents" />
            <xs:element name="getStudentsResponse"
                type="tns:getStudentsResponse" />
            <xs:element name="test" type="tns:test" />
            <xs:element name="testResponse" type="tns:testResponse" />
            <xs:complexType name="getStudents">...</xs:complexType>
            <xs:complexType name="getStudentsResponse">...</xs:complexType>
            <xs:complexType name="student">...</xs:complexType>
            <xs:complexType name="test">
                <xs:sequence>
                    <xs:element minOccurs="0" name="arg0" type="xs:string" />
                </xs:sequence>
            </xs:complexType>
            <xs:complexType name="testResponse">...</xs:complexType>
        </xs:schema>
    </wsdl:types>
    <wsdl:message name="testResponse">
        <wsdl:part element="tns:testResponse" name="parameters"></wsdl:part>
    </wsdl:message>
    <wsdl:message name="getStudents">
        <wsdl:part element="tns:getStudents" name="parameters"></wsdl:part>
    </wsdl:message>
    <wsdl:message name="test">
        <wsdl:part element="tns:test" name="parameters"></wsdl:part>
    </wsdl:message>
    <wsdl:message name="getStudentsResponse">
        <wsdl:part element="tns:getStudentsResponse"
            name="parameters"></wsdl:part>
    </wsdl:message>
    <wsdl:portType name="Service">
        <wsdl:operation name="getStudents">
            <wsdl:input message="tns:getStudents" name="getStudents"></wsdl:input>
            <wsdl:output message="tns:getStudentsResponse"
                name="getStudentsResponse"></wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="test">
            <wsdl:input message="tns:test" name="test"></wsdl:input>
            <wsdl:output message="tns:testResponse"
                name="testResponse"></wsdl:output>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="ServiceImplServiceSoapBinding"
        type="tns:Service">
        <soap:binding style="document"
            transport="http://schemas.xmlsoap.org/soap/http" />
        <wsdl:operation name="getStudents">
            <soap:operation soapAction="" style="document" />
            <wsdl:input name="getStudents">
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output name="getStudentsResponse">
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="test">
            <soap:operation soapAction="" style="document" />
            <wsdl:input name="test">
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output name="testResponse">
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="ServiceImplService">
        <wsdl:port binding="tns:ServiceImplServiceSoapBinding"
            name="ServiceImplPort">
            <soap:address location="http://localhost:1000/Exercise" />
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>
原文地址:https://www.cnblogs.com/LIUWEI123/p/9621932.html