[Pro-test available] Examples of several ways springBoot calls the other party's webService interface

table of Contents

Preface

1. Maven that needs to be used

Two, how to call the webservice interface

Call method one:

Calling method two:

Examples generated by myEclipse:

Example generated by idea:


Preface

We usually use several data formats to develop call interfaces. For example, there is restful, which is currently the most popular and the easiest to develop. There is also a webservice data format. This should be used in some projects a long time ago. Of this

What is a webservice? A web service is a platform-independent, low-coupling, self-contained, programmable web-based application, which can be described by the open XML (a subset of the standard Universal Markup Language) standard , Publish, discover, coordinate and configure these applications for the development of distributed interoperable applications

When calling a webservice written by someone else, the other party will give you a string of schema files (xsd files) or an address ending in wsdl. Your access to the wsdl address is the same as the xsd file, such as the following example of xsd format

Of course, there are many other xsd examples, click to view: https://my.oschina.net/CraneHe/blog/183471

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://xxx.zygxsq.cn/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="PowerAlarmImplService" targetNamespace="http://xxx.zygxsq.cn/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://xxx.zygxsq.cn/" elementFormDefault="unqualified" targetNamespace="http://xxx.zygxsq.cn/" version="1.0">
<xs:element name="queryPowerAlarm" type="tns:queryPowerAlarm"/>
<xs:element name="queryPowerAlarmResponse" type="tns:queryPowerAlarmResponse"/>
<xs:complexType name="queryPowerAlarm">
<xs:sequence>
<xs:element minOccurs="0" name="alarmId" type="xs:string"/>
<xs:element minOccurs="0" name="eventTime" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="queryPowerAlarmResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="tns:powerAlarmRsp"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="powerAlarmRsp">
<xs:complexContent>
<xs:extension base="tns:baseRsp">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="responseList" nillable="true" type="tns:powerAlarm"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="baseRsp">
<xs:sequence>
<xs:element minOccurs="0" name="errorInfo" type="xs:string"/>
<xs:element name="resultCode" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="powerAlarm">
<xs:sequence>
<xs:element minOccurs="0" name="alarmId" type="xs:string"/>
<xs:element minOccurs="0" name="alarmStatus" type="xs:string"/>
<xs:element minOccurs="0" name="canelTime" type="xs:string"/>
<xs:element minOccurs="0" name="eventTime" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="queryPowerAlarmResponse">
<wsdl:part element="tns:queryPowerAlarmResponse" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:message name="queryPowerAlarm">
<wsdl:part element="tns:queryPowerAlarm" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:portType name="IPowerAlarm">
<wsdl:operation name="queryPowerAlarm">
<wsdl:input message="tns:queryPowerAlarm" name="queryPowerAlarm"> </wsdl:input>
<wsdl:output message="tns:queryPowerAlarmResponse" name="queryPowerAlarmResponse"> </wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="PowerAlarmImplServiceSoapBinding" type="tns:IPowerAlarm">
<soap12:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="queryPowerAlarm">
<soap12:operation soapAction="" style="document"/>
<wsdl:input name="queryPowerAlarm">
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output name="queryPowerAlarmResponse">
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="PowerAlarmImplService">
<wsdl:port binding="tns:PowerAlarmImplServiceSoapBinding" name="PowerAlarmImplPort">
<soap12:address location="http://11.111.11.111:9556/xxx/ws/powerAlarmWs"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

 

1. Maven that needs to be used

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-ws</artifactId>
            <version>1.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
        </dependency>

Two, how to call the webservice interface

Call method one:

The easiest way is to use this method, you can directly adjust the other party's webService interface

/**
* 调用webservice接口
* 原文章链接:https://blog.csdn.net/qq_27471405/article/details/105275657
* 其他均为盗版,公众号:灵儿的笔记(zygxsq)
*/
public String sendWsdl(Object obj) {
        logger.info("--------调用webservice接口begin-------");
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();

        //对方的wsdl地址
        Client client = dcf.createClient("http://xx
.xxx.xx.xx:9556/xxx/ws/getAlarmWs?wsdl");
        String json = null;
        try {

            QName qName = new QName("http://xx.zygxsq.cn/", "getAlarmWs");                                                //*原文章链接:https://blog.csdn.net/qq_27471405/article/details/105275657     * 其他均为盗版,公众号:灵儿的笔记(zygxsq)
            Object[] objects1= client.invoke(qName, "aaa","bbb"); //参数1,参数2,参数3......按顺序放就看可以

            json = JSONObject.toJSONString(objects1[0]);
            System.out.println("返回数据:" + json.toString());

        } catch (Exception e) {
            e.printStackTrace();
            logger.info("服务器断开连接,请稍后再试");
        }
        logger.info("--------调用webservice接口end-------");
        return json;


    }

 

Calling method two:

Have to use development tools to generate code, such as myEclipse and idea tools

The tutorial generated by myEclipse can be seen in this article: https://www.cnblogs.com/demojie/archive/2017/08/24/java_webservice.html

The tutorial generated by idea can be seen in this article: https://blog.csdn.net/weixin_40699910/article/details/103399292

 

Examples generated by myEclipse:

myEclipse generates webservice code tutorial based on xsd file

 

 

1. If you choose the local wsdl file, it will be such a bunch of codes after generation, as shown in the figure

 

Look at the file shown in my screenshot, because I put the wsdl file in the D drive directory and generated it. If you use the other party’s url directly, this should be the other party’s url address. Of course, you can follow me Similarly, it is possible to generate it locally and change it to the address of the other party. This wise man sees wisdom.

 

After generating the above code through myeclipse, it is not necessary to develop on myeclipse. You can copy the above 9 codes to any project place, such as idea, and then you can call each other through the following code

/**
*调用webservice接口
*原文章链接:https://blog.csdn.net/qq_27471405/article/details/105275657
* 其他均为盗版,公众号:灵儿的笔记(zygxsq)
*/
public String sendWsdlWebService(String aaa,String bbb) {
        logger.info("--------调用webservice查询接口begin-------");
        QueryPowerAlarmResponse queryPowerAlarmResponse=null;
        URL url;
        String json="";
        try {

            url = new URL("http://11.111.111.111:9556/xxx/ws/powerAlarmWs?wsdl");
            //Qnameqname是qualified name 的简写
            //2.构成:由名字空间(namespace)前缀(prefix)以及冒号(:),还有一个元素名称构成
            QName qname=new QName("http://xxx.zygxsq.cn/","PowerAlarmImplService");
            javax.xml.ws.Service service= javax.xml.ws.Service.create(url, qname);                                 //*原文章链接:https://blog.csdn.net/qq_27471405/article/details/105275657     * 其他均为盗版,公众号:灵儿的笔记(zygxsq)
            IPowerAlarm port = service.getPort(IPowerAlarm.class);
            PowerAlarmRsp powerAlarmRsp = port.queryPowerAlarm(aaa, bbb);

            json = JSONObject.toJSONString(powerAlarmRsp);
           // System.out.println("111返回数据:" + json.toString());

        }catch (Exception e){
            e.printStackTrace();
        }
        logger.info("--------调用webservice查询接口end-------");
        return json;

    }

 

Example generated by idea:

Of course, idea can also generate code, but the generation of myeclipse is more troublesome. A bunch of maven must be introduced before it can be generated.

For details, please refer to this article: https://blog.csdn.net/weixin_40699910/article/details/103399292

 

I won’t write it here, I’ll just write a note here: The maven to be imported is the following pile, and after generating the code, you must comment out these mavens, or remove these mavens, or you will compile it every time , It will regenerate a webSocket code.

  <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <version>0.12.3</version>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <schemaLanguage>WSDL</schemaLanguage>
          <generatePackage>com.dexcoder.ws</generatePackage>
          <generateDirectory>${basedir}/src/main/java</generateDirectory>
          <schemas>
            <schema>
              <fileset>
                <directory>${basedir}/src/main/resources</directory>
                <includes>
                <include>*.wsdl</include>
              </fileset>
            </schema>
          </schemas>
        </configuration>
      </plugin>
    </plugins>

Reference article 

https://www.cnblogs.com/demojie/archive/2017/08/24/java_webservice.html

https://blog.csdn.net/weixin_40699910/article/details/103399292

Thanks to the original author for sharing, so that technical people can solve the problem faster 

Guess you like

Origin blog.csdn.net/qq_27471405/article/details/105275657