WebService客户端几种实现方式

WebService客户端几种实现方式1。发布一个webservice服务。2。jdk原生调用方式和import生成代码3。cxf两种调用方式。4。axis和axis2调用方式5。httpClient调用方式。httpclient4.5.2httpmime4.5.2

大家好,又见面了,我是你们的朋友全栈君。

一、发布一个webservice服务(jdk原生)

1.编写服务接口

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * 
 * 基于soap协议(http+xml)的服务
 */
@WebService(name = "Login",// 定义Port名称
serviceName = "MyService", // 修改WebService服务名称
targetNamespace = "http://com.soft.ws/my" // 定义命名空间,默认为倒置的包名
)
public interface MyService {
	// 提供一个对外公开的服务
	@WebMethod(operationName = "authorization")
	// 修改方法名
	String authorization(@WebParam(name = "userId") String userId,
			@WebParam(name = "password") String password);
}

2.服务实现类

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * 服务实现类
 */
@WebService(endpointInterface = "com.soft.platform.webservice.server.MyService",
name = "Login",// 定义Port名称
serviceName = "MyService", // 修改WebService服务名称
targetNamespace = "http://com.soft.ws/my" // 定义命名空间,默认为倒置的包名
//服务实现类和接口的注解要一样全
)
public class MyServiceImpl implements MyService {

	@WebMethod(operationName = "authorization" // 修改方法名
	)
	@Override
	public String authorization(
			@WebParam(name = "userId") String userId,
			@WebParam(name = "password") String password) {
		if ("admin".equals(userId) && "123456".equals(password)) {
			return "success";
		}
		return "error";
	}

}

3.发布服务

运行下面的代码 发布WebService服务

/**
 * 发布服务
 *
 */
public class MyPublisher {
	
	public static void main(String[] args) {
		//指定服务url
		String url = "http://192.168.0.101:8089/myservice";
		//指定服务实现类
		MyService server = new MyServiceImpl();
		//采用命令行发布者Endpoint发布服务
		Endpoint.publish(url, server);
	}

}

4.浏览器查看是否发布成功

打开浏览器地址栏输入:http://192.168.0.101:8089/myservice?wsdl
结果如下:
在这里插入图片描述

二、几种客户端调用方式

上面发布的服务不要关闭,编写另外一个客户端类来调用上面发布服务,有以下几种方法来调用服务

1、jdk原生调用(需要获取服务接口文件)

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import com.soft.platform.webservice.server.MyService;

public class WsClient {

	public static void main(String[] args) throws Exception {
		URL url = new URL("http://192.168.0.101:8089/myservice?wsdl");
		// 指定命名空间和服务名称
		QName qName = new QName("http://com.soft.ws/my", "MyService");
		Service service = Service.create(url, qName);
		// 通过getPort方法返回指定接口
		MyService myServer = service.getPort(new QName("http://com.soft.ws/my",
				"LoginPort"), MyService.class);
		// 调用方法 获取返回值
		String result = myServer.authorization("admin", "123456");
		System.out.println(result);
	}

}
返回结果: success

2、用import命令生成客户端代码

wsimport -d d:/webservice -keep -p com.soft.test.wsimportClient -verbose http://192.168.0.101:8089/myservice?wsdl
这里写图片描述

public static void main(String[] args) {
		MyService_Service service = new MyService_Service();
		MyService login = service.getLoginPort();
		String result = login.authorization("admin", "123456");
		System.out.println(result);
	}

wsimport是jdk自带的,可以根据wsdl文档生成客户端调用代码的工具。
无论服务器端的WebService是用什么语言写的,都将在客户端生成Java代码。

wsimport.exe位于JAVA_HOME\bin目录下.

3、cxf类库 两种调用方式。

Apache CXF 是开源的WebService框架,CXF帮助您使用前端编程api(如JAX-WS和JAX-RS)构建和开发服务。这些服务可以使用多种协议,如SOAP、XML/HTTP、RESTful HTTP或CORBA,并在多种传输协议(如HTTP、JMS或JBI)上工作。
官网地址:http://cxf.apache.org/

public static void main(String[] args) {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(MyService.class);
		factory.setAddress("http://192.168.0.101:8089/myservice?wsdl");
		// 需要服务接口文件
		MyService client = (MyService) factory.create();
		String result = client.authorization("admin", "123456");
		System.out.println(result);
	}
public static void main(String[] args) throws Exception {
		//采用动态工厂方式 不需要指定服务接口
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		Client client = dcf
				.createClient("http://192.168.0.101:8089/myservice?wsdl");
		QName qName = new QName("http://com.soft.ws/my", "authorization");
		Object[] result = client.invoke(qName,
				new Object[] { "admin", "123456" });
		System.out.println(result[0]);
	}

注意事项:
apache-cxf-3.2.XX 开始JDK最低要求要JDK1.8
apache-cxf-3.1.XX还是支持JDK1.7
cxf的WebServices客户端代码maven依赖如下:

<dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.xx.xx</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.xx.xx</version>
        </dependency>
    </dependencies>

如果是独立的jar部署的,cxf客户端需要的jar包如下:
在这里插入图片描述

4、axis调用方式

这个例子是比较老的axis版本作为客户端了,最新版官网

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

public class WsAClient {
     /**
	 * 跨平台调用Web Service出现
	 *  faultString: 服务器未能识别 HTTP 头 SOAPAction 的值: 
	 * JAX-WS规范不需要SoapAction,但是.NET需要,所以产生了这个错误。 
	 * options.setAction("目标的TargetNameSpace"+"调用的方法名");
	 */
	public static void main(String[] args) {
		String url = "http://192.168.0.101:8089/myservice?wsdl";
		Service service = new Service();
		try {
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(url));
			// WSDL里面描述的接口名称(要调用的方法)
			call.setOperationName(new QName("http://com.soft.ws/my",
					"authorization"));
			//跨平台调用加上这个
		   call.setUseSOAPAction(true);
		   call.setSOAPActionURI("http://com.soft.ws/my/authorization");
			// 接口方法的参数名, 参数类型,参数模式 IN(输入), OUT(输出) or INOUT(输入输出)
			call.addParameter("userId", XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter("password", XMLType.XSD_STRING, ParameterMode.IN);
			// 设置被调用方法的返回值类型
			call.setReturnType(XMLType.XSD_STRING);
			// 设置方法中参数的值
			Object result = call.invoke(new Object[] { "admin", "123456" });

			System.out.println(result.toString());
		} catch (ServiceException | RemoteException | MalformedURLException e) {
			e.printStackTrace();
		}
	}

}

axis方式依赖的相关jar包如下:
在这里插入图片描述

5、httpClient调用方式。

(1)maven依赖如下

  <properties>
   <httpclient.version>4.5.6</httpclient.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>${httpclient.version}</version>
    </dependency>
  </dependencies>

(2)httpclient作为客户端调用webservice。代码如下

/*
 * Copyright (c)
 */
package test;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.nio.charset.Charset;

/**
 * webservice客户端
 *
 * @author David Lin
 * @version: 1.0
 * @date 2018-09-09 12:16
 */
public class SoapClient {

    public static void main(String args[]) throws Exception {
        //soap服务地址
        String url = "http://localhost:8888/ssm/Services/UserService?wsdl";
        StringBuilder soapBuilder = new StringBuilder(64);
        soapBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        soapBuilder.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.soft.com/\">");
        soapBuilder.append("   <soapenv:Header/>");
        soapBuilder.append("       <soapenv:Body>");
        soapBuilder.append("             <web:authorization>");
        soapBuilder.append("                     <userId>").append("admin").append("</userId>");
        soapBuilder.append("                     <password>").append("123456").append("</password>");
        soapBuilder.append("               </web:authorization>");
        soapBuilder.append("    </soapenv:Body>");
        soapBuilder.append("</soapenv:Envelope>");

        //创建httpcleint对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建http Post请求
        HttpPost httpPost = new HttpPost(url);
        // 构建请求配置信息
        RequestConfig config = RequestConfig.custom().setConnectTimeout(1000) // 创建连接的最长时间
                .setConnectionRequestTimeout(500) // 从连接池中获取到连接的最长时间
                .setSocketTimeout(3 * 1000) // 数据传输的最长时间10s
                .build();
        httpPost.setConfig(config);
        CloseableHttpResponse response = null;
        try {
            //采用SOAP1.1调用服务端,这种方式能调用服务端为soap1.1和soap1.2的服务
            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");

            //采用SOAP1.2调用服务端,这种方式只能调用服务端为soap1.2的服务
            // httpPost.setHeader("Content-Type", "application/soap+xml;charset=UTF-8");
            StringEntity stringEntity = new StringEntity(soapBuilder.toString(), Charset.forName("UTF-8"));
            httpPost.setEntity(stringEntity);
            response = httpClient.execute(httpPost);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println(content);

            } else {
                System.out.println("调用失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != response) {
                response.close();
            }
            if (null != httpClient) {
                httpClient.close();
            }
        }


    }
}

返回结果为:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
	<soap:Body>
		<ns2:authorizationResponse xmlns:ns2="http://webservice.soft.com/">
			<return>success</return>
		</ns2:authorizationResponse>
	</soap:Body>
</soap:Envelope>

(3)用Jsoup提取响应数据。
maven依赖

<properties>
      <jsoup.version>1.11.3</jsoup.version>
  </properties>
  
  <dependencies>
  
    <dependency>
      <groupId>org.jsoup</groupId>
      <artifactId>jsoup</artifactId>
      <version>${jsoup.version}</version>
    </dependency>
    
  </dependencies>

代码如下:

 Document soapRes = Jsoup.parse(content);
 Elements returnEle = soapRes.getElementsByTag("return");

 System.out.println("调用结果为:"+returnEle.text());

6、SoapUI

soapUI是一个开源测试工具,通过soap/http来检查、调用、实现Web Service的功能/负载/符合性测试。
用这个图形化工具也可以调用WebService服务,作为测试使用。
在这里插入图片描述

7、其他

只要WSDL服务地址能够访问,就能根据wsdl描述的信息手动造一个 服务接口文件 ,这样客户端就可以使用这个接口文件调用服务。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/158837.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • phpstrom 2021.12激活吗(JetBrains全家桶)「建议收藏」

    (phpstrom 2021.12激活吗)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~1435QFILVV-eyJsaWNlb…

    2022年3月30日
    40
  • IDEA设置自动导入包方法「建议收藏」

    作为程序员,有的时候多做一步就觉得累,所以本人就给IDEA设置了自动导入包,也算是提高了些效率吧。手动导入快捷键(Alt+Enter)1》》》》打开file——setting2》》》》打开General——Autoimport:将下列两项勾选中。…

    2022年4月10日
    355
  • html空格语言,html的空格代码是什么

    html空格语言,html的空格代码是什么html的空格代码是什么发布时间:2020-06-1716:00:48来源:亿速云阅读:253作者:元一html空格代码是为html空格字符代码,由“&+n+b+s+p+;”组成,记住最后一个分号不要忘记了。CSS中当white-space属性取值为pre时,浏览器会保留文本中的空格和换行,例如:AABB显示效果为:AABBCSS的letter-spacing属…

    2025年7月8日
    1
  • goland 激活3月最新在线激活

    goland 激活3月最新在线激活,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月15日
    45
  • https://sweetalert2.github.io/

    https://sweetalert2.github.io/

    2022年3月13日
    81
  • 计算机网络复习题

    计算机网络复习题1.计算机网络从逻辑功能上可分成(通信子网)和(资源子网)。2.网络的低层模块向高层提供功能性的支持称之为(服务)。3.TCP/1P标准模型从高到低分别是(应用层)、(运输层)、(网络层)、(链路层)和(物理层)。4.在一个物理信道内可以传输频率的范围称为(带宽)。5.计算机网络依据网络传输技术来分类,主要有(广播式)和(点对点式)两类。6.通信双方在进行通信之前,需要事先建立一个可以彼此沟通的的通道,这种通信方式称为(面向连接)的通信。7.因特网上提供的主要信息服务有(电子邮件)、(WWW

    2022年9月2日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号