Use idea to generate webservice client--detailed steps--(use of wsdl file)

Table of contents

1. Idea installation webservice

1. Click on file in the upper left corner and select settings to edit.

2. Download Web Service

3. Add webservice to this project

4. Add webservice dependencies

2. Use idea to automatically generate the webService client code based on the wsdl file (and then call the interface or method based on the generated test class)

1. Open tools -> WebServices -> Generate Java Code From Wsdl, and proceed in the order shown in the figure.

2. Follow the order in the picture, and finally click OK

3. Generate TestCase must be checked

4.wsdl file conversion to generate Java code: successful

5. How to use the generated code

3. Directly use Axis2 to call the wsdl type interface:


1. Idea installation webservice

1. Click file on the upper left and select settings

2. Download Web Service

3. Add webservice to this project

 

4. Add webservice dependencies

   <!--webservice: the jar package required by TestCase-->     
        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>4.12</version>

        </dependency>

2. Use idea to automatically generate the webService client code based on the wsdl file (and then call the interface or method based on the generated test class)

Steps: 1. Use idea to generate code based on the wsdl file. You need to download the wsdl file to the local first.

    2. There are no necessary requirements when downloading. Select Apache Axis. You need to search on Baidu for dependencies.

    ​ 3. After downloading, you need to compare the test class code and write the code to call the interface or method

Advantages: 1. The input parameters do not need to be encapsulated by yourself, which is particularly easy to write.

     ​​ 2. The return values ​​are all encapsulated and can be easily obtained without writing the parsing code yourself.

1. Open tools -> WebServices -> Generate Java Code From Wsdl, and proceed in the order shown in the figure.

2. Follow the order in the picture, and finally click OK

3. Generate TestCase must be checked


(An example of calling the interface will be given. If you follow it, you can call the specified interface)

4.2 Click OK to report an error, then go to Baidu to download the dependencies required by Axis. You need to download an additional plug-in for the first time (just download it directly)

As long as no wsdl file error is reported, and other errors are reported, as long as the code can be generated, it can be used

4.wsdl file conversion to generate Java code: successful

Example 1:

  

 Example 2:

5. How to use the generated code

Click to open the class with Test in the name, find the method you need, copy it directly and use it

3. Directly use Axis2 to call the wsdl type interface:

Advantages: You don’t need to use idea to automatically generate webService client code based on wsdl, and then call the required method according to the test class

Disadvantages: 1. You need to encapsulate the parameters yourself when entering parameters. It is not easy to encapsulate the parameters yourself with examples.

2. If you need to get the return value of the calling interface, the returned data will not be parsed, and the required parameters cannot be obtained (the example of not parsing the message, I will not write it myself)

Axis2 calling interface example:

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;


public class Test {
    public static void test() {
        String userId = "123";
        String bindAccount = "123";
        RPCServiceClient serviceClient = null;

        OMFactory factory = OMAbstractFactory.getOMFactory();
        OMNamespace omDiag = factory.createOMNamespace("http://diagnosis.interfaces.axis2.osf.nort hbound.neal.cpehg.ums.zte.com", "diag");
        OMNamespace omXSD = factory.createOMNamespace("http://model.common.northbound.neal.cpehg. ums.zte.com/xsd", "xsd");

        try {
            serviceClient = new RPCServiceClient();
            Options options = serviceClient.getOptions();
// 指定调用WebService的URL
            EndpointReference targetEPR = new EndpointReference("http://10.46.60.200:9094/axis2/services/Cpe112Diag nosisWebServices?wsdl");
            options.setTo(targetEPR);
            options.setTimeOutInMilliSeconds(30000);
            options.setManageSession(true);

// 指定方法的参数值
            OMElement paramRequest = factory.createOMElement("request", omDiag);
            OMElement paramUserId = factory.createOMElement("userID", omXSD);

            paramUserId.setText(userId);
            OMElement paramBindAccount = factory.createOMElement("bindAccount", omXSD);
            paramBindAccount.setText(bindAccount);
            paramRequest.addChild(paramBindAccount);
            paramRequest.addChild(paramUserId);

            OMElement paramItemName = factory.createOMElement("itemName", omDiag);
            paramItemName.setText("cpehg.diagnosis.CpeBasicInfo");

            String method = "getParameterValuesFromDbAndCpeByItemName";

            OMElement data = factory.createOMElement(method, omXSD);
            data.setNamespace(omDiag);
            data.addChild(paramRequest);
            data.addChild(paramItemName);

            OMElement re = serviceClient.sendReceive(data);
// 处理返回数据

        } catch (AxisFault e) {
// 异常处理
            e.printStackTrace();
        } finally {

            try {
                if (serviceClient != null) serviceClient.cleanupTransport();
            } catch (AxisFault e) {

            }
        }
    }
}

Guess you like

Origin blog.csdn.net/m0_71202849/article/details/132224847