之前都是需要根据发布的ws服务生成wsdl和客户端代码,其中不生成也可以动态调用。
现在,在LoginService的实现类中添加一个方法:getUser
@Override
public User getUser(String username, String password) {
return new User(username,password);
}
User
package com.example.server.bean;
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
我们不需要生成wsdl和那一堆文件,直接通过ws接口地址,也可以去调用getUser方法。
//创建动态客户端
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
Client client = factory.createClient("http://localhost:8080/webservice/api?wsdl");
// 需要密码的情况需要加上用户名和密码
//client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
HTTPConduit conduit = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(2000); //连接超时
httpClientPolicy.setAllowChunking(false); //取消块编码
httpClientPolicy.setReceiveTimeout(120000); //响应超时
conduit.setClient(httpClientPolicy);
//client.getOutInterceptors().addAll(interceptors);//设置拦截器
try{
Object[] objects = new Object[0];
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("getUser","jack","123");
Method[] methods = objects[0].getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println(methods[i].getName());
Object invoke = methods[i].invoke(objects[0]);
System.out.println(invoke);
}
}catch (Exception e){
e.printStackTrace();
}
因为没有描述文件,所以你需要花费很大的代价去动态解析,说实话,还不如用上一节的方法呢,省心!
所以,站长个人是不推荐这种写法的。
public static void main(String[] args) throws Exception {
//创建动态客户端
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
Client client = factory.createClient("http://localhost:8080/webservice/loginApi?wsdl");
HTTPConduit conduit = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(2000); //连接超时
httpClientPolicy.setAllowChunking(false); //取消块编码
httpClientPolicy.setReceiveTimeout(120000); //响应超时
conduit.setClient(httpClientPolicy);
Object[] objects = client.invoke("userLogin");
if(objects != null && objects.length > 0){
Method[] methods = objects[0].getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
//System.out.println(methods[i].getName());
if(methods[i].getName().equals("getEntry")){
Object entry = methods[i].invoke(objects[0]); //实际上是 UserLoginResponse.Return.Entry
Method iteratorMethod = entry.getClass().getMethod("iterator");
//反射调用迭代方法
Object iteratorBean = iteratorMethod.invoke(entry);
Method hasNextMethod = iteratorBean.getClass().getMethod("hasNext");
hasNextMethod.setAccessible(true);
while(Boolean.parseBoolean(hasNextMethod.invoke(iteratorBean).toString()) == true){
Method nextMethod = iteratorBean.getClass().getMethod("next");
nextMethod.setAccessible(true);
Object entryBean = nextMethod.invoke(iteratorBean);
Method getKeyMethod = entryBean.getClass().getMethod("getKey");
Method getValueMethod = entryBean.getClass().getMethod("getValue");
Object value = getValueMethod.invoke(entryBean);
value = new String(value.toString().getBytes("GBK"), "UTF-8");
System.out.println(getKeyMethod.invoke(entryBean) + "=" + value);
}
}
}
}
}
经过几番测试,这种动态获取的总是有中文乱码的问题,好像接口被编译的时候是用GBK编译的(windows自带的编码,所以linux就没有这个问题)。
所以,只好通过代码手工转换一下。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有