Найти

Вы здесь:

Что такое SOAP?
Применение SOAP при интеграции систем
Пример https://github.com/cherepakhin/soap-producer-web-service

(note:/home/vasi/prog/java/soap)

SOAP и Spring Boot

Создание SOAP Web-сервиса
Пример работы с SOAP (java, github)
(note:/home/vasi/prog/java/soap/soap-producer-web-service)

WSDL

WSDL - Web Services Description Language. Это стандарт, который описывает веб-службы с помощью XML. По сути, это «паспорт» сервиса, который чётко фиксирует его интерфейс: какие методы доступны, какие параметры они принимают, какой формат ответа ожидается и где находится сам сервис.

Получить файл WSDL
http http://localhost:8080/ws/countries.wsdl

или

http http://localhost:8080/ws/countries.wsdl > test1_result.xml

Результат:
test1_result.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?><wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:sch="http://spring.io/guides/gs-producing-web-service" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://spring.io/guides/gs-producing-web-service" targetNamespace="http://spring.io/guides/gs-producing-web-service">
    <wsdl:types>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://spring.io/guides/gs-producing-web-service">

            <xs:element name="getCountryRequest">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="name" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>

            <xs:element name="getCountryResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="country" type="tns:country"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>

            <xs:complexType name="country">
                <xs:sequence>
                    <xs:element name="name" type="xs:string"/>
                    <xs:element name="population" type="xs:int"/>
                    <xs:element name="capital" type="xs:string"/>
                    <xs:element name="currency" type="tns:currency"/>
                </xs:sequence>
            </xs:complexType>

            <xs:simpleType name="currency">
                <xs:restriction base="xs:string">
                    <xs:enumeration value="GBP"/>
                    <xs:enumeration value="EUR"/>
                    <xs:enumeration value="PLN"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:schema>
    </wsdl:types>
    <wsdl:message name="getCountryResponse">
        <wsdl:part element="tns:getCountryResponse" name="getCountryResponse">
        </wsdl:part>
    </wsdl:message>
    <wsdl:message name="getCountryRequest">
        <wsdl:part element="tns:getCountryRequest" name="getCountryRequest">
        </wsdl:part>
    </wsdl:message>
    <wsdl:portType name="CountriesPort">
        <wsdl:operation name="getCountry">
            <wsdl:input message="tns:getCountryRequest" name="getCountryRequest">
            </wsdl:input>
            <wsdl:output message="tns:getCountryResponse" name="getCountryResponse">
            </wsdl:output>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="CountriesPortSoap11" type="tns:CountriesPort">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="getCountry">
            <soap:operation soapAction=""/>
            <wsdl:input name="getCountryRequest">
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output name="getCountryResponse">
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="CountriesPortService">
        <wsdl:port binding="tns:CountriesPortSoap11" name="CountriesPortSoap11">
            <soap:address location="http://localhost:8080/ws"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

 

Примеры использования WSDL (для проекта из примера выше)


Параметры запроса request.xml(получить информацию об Испании):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:gs="http://spring.io/guides/gs-producing-web-service">
   <soapenv:Header/>
   <soapenv:Body>
      <gs:getCountryRequest>
         <gs:name>Spain</gs:name>
      </gs:getCountryRequest>
   </soapenv:Body>
</soapenv:Envelope>

Выполнение запроса с параметром request.xml к SOAP сервису http://localhost:8080/ws:

curl --header "content-type: text/xml" -d @request.xml http://localhost:8080/ws

Или с форматированием результата:

curl --header "content-type: text/xml" -d @request.xml http://localhost:8080/ws  | xmllint --format - | source-highlight -s xml -f esc

Ответ:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   721  100   416  100   305  61647  45198 --:--:-- --:--:-- --:--:--  117k
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header/>
  <SOAP-ENV:Body>
    <ns2:getCountryResponse xmlns:ns2="http://spring.io/guides/gs-producing-web-service">
      <ns2:country>
        <ns2:name>Spain</ns2:name>
        <ns2:population>46704314</ns2:population>
        <ns2:capital>Madrid</ns2:capital>
        <ns2:currency>EUR</ns2:currency>
      </ns2:country>
    </ns2:getCountryResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

 

Tydy

Tidy - программа для форматирования HTML, XML и других форматов кода.

curl --header "content-type: text/xml" -d @request.xml http://localhost:8080/ws  | tidy -xml -iq

 

Xmlstarlet

Xmlstarlet - программа для форматирования XML кода.

curl --header "content-type: text/xml" -d @request.xml http://localhost:8080/ws  | xmlstarlet fo

 

Подсвечивание синтаксиса XML

С форматированием (xmllint) и выделение цветом (source-highlight):

curl --header "content-type: text/xml" -d @request.xml http://localhost:8080/ws  | xmllint --format - | source-highlight -s xml -f esc