java使用soap方式简单实现webservice

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1074

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

推荐:Java - Webservice调用方式:axis,soap详解

[调用webservice,可以首先根据wsdl文件生成客户端,或者直接根据地址调用,下面讨论直接调用地址的两种不同方式:axis和Soap,soap方式主要是用在websphere下 axis方式调

java类:


import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;

import org.w3c.dom.Document;

public class Receive {

    /**
     * @param args
     */
    @SuppressWarnings("unused")
    public static void main(String[] args) throws Exception {
        String ns = "http://axisversion.sample";
        String wsdlUrl = "http://127.0.0.1:8080/axis2/services/Version?wsdl";
        //1、创建服务(Service)
        URL url = new URL(wsdlUrl);
        QName sname = new QName(ns,"Version");
        Service service = Service.create(url,sname);
                    
        //2、创建Dispatch
        Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns,"VersionHttpSoap11Endpoint"),SOAPMessage.class,Service.Mode.MESSAGE);
                    
        //3、创建SOAPMessage
        SOAPMessage msg = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL).createMessage();
        SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
        SOAPBody body = envelope.getBody();
                    
        //4、创建QName来指定消息中传递数据
        QName ename = new QName(ns,"getVersion","axis");//<nn:add xmlns="xx"/>
        SOAPBodyElement ele = body.addBodyElement(ename);
        // 传递参数
//        ele.addChildElement("a").setValue("22");  
//        ele.addChildElement("b").setValue("33");  
        msg.writeTo(System.out);
        System.out.println("\n invoking.....");
                            
        //5、通过Dispatch传递消息,会返回响应消息
        SOAPMessage response = dispatch.invoke(msg);
        response.writeTo(System.out);
        System.out.println();
                    
        //6、响应消息处理,将响应的消息转换为dom对象
        Document doc = response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
        String str = doc.getElementsByTagName("ns:return").item(0).getTextContent();
        System.out.println(str);

    }

}


推荐:java使用soap方式简单实现webservice (二)

[上一篇从 xml 中找对应的参数比较麻烦,这里介绍用 SOAPUI 的方式填写参数这里为实际调用 .net的 短信发送服务测试import java.net.URL;import javax.xml.namespace.QName


wsdl文件:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://axisversion.sample" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://axisversion.sample">
    <wsdl:documentation>Version</wsdl:documentation>
    <wsdl:types>
        <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://axisversion.sample">
            <xs:element name="VersionException">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="VersionException" nillable="true" type="ns:Exception"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:complexType name="Exception">
                <xs:sequence>
                    <xs:element minOccurs="0" name="Message" nillable="true" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
            <xs:element name="getVersion">
                <xs:complexType>
                    <xs:sequence/>
                </xs:complexType>
            </xs:element>
            <xs:element name="getVersionResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:schema>
    </wsdl:types>
    <wsdl:message name="getVersionRequest">
        <wsdl:part name="parameters" element="ns:getVersion"/>
    </wsdl:message>
    <wsdl:message name="getVersionResponse">
        <wsdl:part name="parameters" element="ns:getVersionResponse"/>
    </wsdl:message>
    <wsdl:message name="VersionException">
        <wsdl:part name="parameters" element="ns:VersionException"/>
    </wsdl:message>
    <wsdl:portType name="VersionPortType">
        <wsdl:operation name="getVersion">
            <wsdl:input message="ns:getVersionRequest" wsaw:Action="urn:getVersion"/>
            <wsdl:output message="ns:getVersionResponse" wsaw:Action="urn:getVersionResponse"/>
            <wsdl:fault message="ns:VersionException" name="VersionException" wsaw:Action="urn:getVersionVersionException"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="VersionSoap11Binding" type="ns:VersionPortType">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="getVersion">
            <soap:operation soapAction="urn:getVersion" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
            <wsdl:fault name="VersionException">
                <soap:fault use="literal" name="VersionException"/>
            </wsdl:fault>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:binding name="VersionSoap12Binding" type="ns:VersionPortType">
        <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="getVersion">
            <soap12:operation soapAction="urn:getVersion" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
            <wsdl:fault name="VersionException">
                <soap12:fault use="literal" name="VersionException"/>
            </wsdl:fault>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:binding name="VersionHttpBinding" type="ns:VersionPortType">
        <http:binding verb="POST"/>
        <wsdl:operation name="getVersion">
            <http:operation location="getVersion"/>
            <wsdl:input>
                <mime:content type="application/xml" part="parameters"/>
            </wsdl:input>
            <wsdl:output>
                <mime:content type="application/xml" part="parameters"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="Version">
        <wsdl:port name="VersionHttpSoap11Endpoint" binding="ns:VersionSoap11Binding">
            <soap:address location="http://127.0.0.1:8080/axis2/services/Version.VersionHttpSoap11Endpoint/"/>
        </wsdl:port>
        <wsdl:port name="VersionHttpSoap12Endpoint" binding="ns:VersionSoap12Binding">
            <soap12:address location="http://127.0.0.1:8080/axis2/services/Version.VersionHttpSoap12Endpoint/"/>
        </wsdl:port>
        <wsdl:port name="VersionHttpEndpoint" binding="ns:VersionHttpBinding">
            <http:address location="http://127.0.0.1:8080/axis2/services/Version.VersionHttpEndpoint/"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>



控制台输出:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><axis:getVersion xmlns:axis="http://axisversion.sample"/></SOAP-ENV:Body></SOAP-ENV:Envelope>
 invoking.....
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header/><soapenv:Body><ns:getVersionResponse xmlns:ns="http://axisversion.sample"><ns:return>Hi - the Axis2 version is 1.6.2</ns:return></ns:getVersionResponse></soapenv:Body></soapenv:Envelope>
Hi - the Axis2 version is 1.6.2

java使用soap方式简单实现webservice (二)

推荐:Java WebService简单使用

[一直在写java但从来没有使用webservice,在网上查了下资料写个简单的使用放这里做备份 具体步骤: 1.新建一个java工程在里面写一个类(服务端)如下: 1 package com.web

相关推荐