webservice client operation mode

Before the company arranged for me to interface with two other companies, the calling method was in the form of webservice, I built a springboot project.

There are various ways on the Internet. Due to the inconsistency of the server generated by the two companies, the client cannot use one method when calling. So I found two ways of implementation, the principle is: less code, not cumbersome, let alone degenerate code and the like.

Calling method 1:

This method requires that the server tarnamespace path and the package path must be the same. If they are the same, the following methods are generally successful. If not, an error will occur:

org.apache.cxf.common.i18n.UncheckedException: No operation was found with the name {http://control.webservice.ccwl.com/}xxx

The calling code is as follows:

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web-services</artifactId>
		</dependency>
<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
			<version>3.2.4</version>
		</dependency>
private void sendDataForAnt() throws Exception{
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("wsdl路径");
Object[] objects = new Object[0];
objects = client.invoke("方法名称", "参数");
 

    }

Call method two:

When it comes to debugging the interface of the second company, the above method will not work, and the error is the same as above. Various methods on the Internet have been tried. The httpclient, url method, and even generating local code based on wsdl will not work. At that time, the brain was all cxf, Finally, someone reminded me that I was inspired.

First depend on the package:

<dependency>
			<groupId>org.apache.axis</groupId>
			<artifactId>axis</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>org.apache.axis</groupId>
			<artifactId>axis-jaxrpc</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>wsdl4j</groupId>
			<artifactId>wsdl4j</artifactId>
			<version>1.6.2</version>
		</dependency>
		<dependency>
			<groupId>commons-discovery</groupId>
			<artifactId>commons-discovery</artifactId>
			<version>0.2</version>
		</dependency>

Code:

public static String axisForDongFeng(String jsonStr) throws Exception {
        //url
        String wsdlurl = "xxx/gpsservice?wsdl";
        //targetNamespace
        String targetNamespace = "http://webservice.xx.com/";
        //namespaceUri
        String namespaceUri = "http://control.webservice.xx.com/";
        //调用的具体方法名称
        String method = "addUser";
        //方法参数名称
        String localPart = "list";
        // 创建一个服务(service)调用(call)
        org.apache.axis.client.Service service = new org.apache.axis.client.Service();
        // 通过service创建call对象
        Call call = (Call) service.createCall();
        // 设置service所在URL,wsdld地址
        call.setTargetEndpointAddress(new java.net.URL(wsdlurl));
        //调用接口的targetNamespace和具体方法名称
        call.setOperationName(new QName(targetNamespace, method));
        call.setUseSOAPAction(true);
        //变量最好只是用String类型,其他类型会报错
        call.addParameter(
                new QName(namespaceUri, localPart),
                //设置参数名 state  第二个参数表示String类型,第三个参数表示入参
                org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN
        );
        // 设置返回类型
        call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
        String jsonString = (String) call.invoke(new Object[]{jsonStr});
      
        return jsonString;
    }

Note: targetNamespace and namespaceUri are inconsistent.

Published 23 original articles · Like 3 · Visitors 6858

Guess you like

Origin blog.csdn.net/weixin_41834814/article/details/87879247