webservice 客户端操作方式

公司之前安排我去和其他两家公司对接接口,调用方式是webservice 形式的,我 建了个springboot项目。

网上有各种方式,由于两家公司生成服务端不一致,客户端调用的时候不能用一种方式。所以找了两种实现方式,原则是:少些代码,不要繁琐,更不能反生成代码之类的。

调用方式一、:

这种方式要求,服务端tarnamespace路径和包路径得一致,若一致,一般用如下方式都会调用成功。若不是,会出现错误:

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

调用代码如下:

<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("方法名称", "参数");
 

    }

调用方式二:

到了调试第二个公司的接口时,上述方式怎么也不行,报错也如上,网上的各种方式都试了,httpclient ,url方式,甚至根据wsdl 生成本地代码,都不行,当时脑子全是cxf,最后别人提醒了一下,有灵感了。

先依赖包:

<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>

代码:

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;
    }

注意:targetNamespace和namespaceUri ,两个不一致。

发布了23 篇原创文章 · 获赞 3 · 访问量 6858

猜你喜欢

转载自blog.csdn.net/weixin_41834814/article/details/87879247
今日推荐