一.创建一个新项目,创建时选择webservice
二.在HelloWorld类里面写自己的方法,在file下编译一下这个类,不编译,idea会提示不通过,编译后需要将为该服务发布WSDL文件,此文件必须生成.
选择要发布的服务,点击ok生成wsdl文件
引入axis库
三.把项目部署到tomcat,启动。
在浏览器输入:http://localhost:8080/services/HelloWorld?wsdl
看到如下,发布成功
四.代码
1)服务端代码
package example;/**
* Created by uisftech on 2018/4/27.
*/
public class HelloWorld {
public String palyBefore(String before) {
String result = "--------运动前---------" + before;
System.out.println(result);
return result;
}
public String play(String play){
String result = "--------运动中---------" + play;
System.out.println(result);
return result;
}
public String playAfter(String after){
String result = "--------运动后---------" + after;
System.out.println(result);
return result;
}
}
2)客户端代码
package com.zqr.client;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.utils.StringUtils;
import javax.xml.rpc.ServiceException;
import java.net.URL;
/**
* Created by uisftech on 2018/4/27.
*/
public class WebServiceClient {
public static void main(String[] args){
String url = null;
String method = null;
String[] params = null;
//设置入参
url = "http://localhost:8080/services/HelloWorld";
method = "play";
params = new String[]{"伸腰,踢腿"};
WebServiceClient wsClient = new WebServiceClient();
String result = wsClient.send(url,method,params);
System.out.println(result);
}
public String send(String url,String method,Object[] params){
if (StringUtils.isEmpty(url))
return "url地址为空";
if (StringUtils.isEmpty(method))
return "method方法名为空";
String result = null;
Call call = null;
try {
//创建webservice调用实例
Service service = new Service();
call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(url));
call.setOperationName(method);
//执行方法
result = (String)call.invoke(params);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}五:可能会出现的问题
org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
修复:
重启tomcat,运行客户端main方法:
版权声明:本文为zz_ddup原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。