1、不同于Http接口的WebService接口
个人推荐文章:https://www.cnblogs.com/phoebes/p/8029464.html
2、WebService接口的调用
axis的pom依赖:
<dependency>
<groupId>axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.atlassian.confluence.rpc</groupId>
<artifactId>confluence-axis-soap-plugin</artifactId>
<version>1.7.1</version>
</dependency>
测试接口:
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
@WebService
public interface IWeatherService {
@WebMethod(action="query")
String query(@WebParam(name="string")String string);
@WebMethod(action="sayHello")
String sayHello(@WebParam(name="name")String name);
@WebMethod(action="save")
String save(@WebParam(name="name")String name,@WebParam(name="pwd")String pwd);
}
接口实现类:
import javax.jws.WebService;
import javax.jws.WebMethod;
@WebService(endpointInterface = "com.dsp.IWeatherService")
public class WebServiceImpl implements IWeatherService {
public String sayHello(String name) {
System.out.println(name);
return "Hello " + name;
}
public String query(String string) {
System.out.println(string);
return "query:"+string;
}
public String save(String name, String pwd) {
System.out.println(name+""+pwd);
return "保存成功:name:"+name+"password:"+pwd;
}
}
接口发布类:
import javax.xml.ws.Endpoint;
public class WebServicePublish {
public static void main(String[] args) {
//定义WebService的发布地址,这个地址就是提供给外界访问Webervice的URL地址,URL地址格式为:http://ip:端口号/xxxx
//String address = "http://192.168.1.100:8989/";这个WebService发布地址的写法是合法的
//String address = "http://192.168.1.100:8989/Webservice";这个WebService发布地址的是合法的
String address = "http://127.0.0.1:8989/WS_Server/Webservice";
//使用Endpoint类提供的publish方法发布WebService,发布时要保证使用的端口号没有被其他应用程序占用
Endpoint.publish(address , new WebServiceImpl());
System.out.println("发布webservice成功!");
}
}
WebService接口调用方法:
public String message(String mobiles, String smsContent) throws Exception {
String url = "http://127.0.0.1:8989/WS_Server/Webservice";
try {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName(new QName("http://dsp.com/","query")); // WSDL里面描述的接口 名称 以及发布项目路径
call.addParameter("string",
org.apache.axis.encoding.XMLType.XSD_STRING,
ParameterMode.IN);// 接口的参数 参数名//XSD_STRING:String类型//.IN入参
// call.addParameter("pwd",
// org.apache.axis.encoding.XMLType.XSD_STRING,
// ParameterMode.IN);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型
String arg0 = "6666";
String result = (String) call.invoke(new Object[] { arg0 });
// 给方法传递参数,并且调用方法
System.out.println("result is " + result);
} catch (Exception e) {
System.err.println(e.toString());
}
}
接口调用结果:
result is query:6666