最详细的idea创建webservice教程
创建服务端
- File->New Project
点击next,会自动生成demo - 将要发布的类加上@WebService,方法加上@WebMethod,设置服务发布address
@WebService()
public class HelloWorld {
@WebMethod
public String sayHelloWorldFrom(String from) {
String result = "Hello, world, from " + from;
System.out.println(result);
return result;
}
@WebMethod
public String sqyHai(String from) {
String result = "Hai, world, from " + from;
System.out.println(result);
return result;
}
public static void main(String[] argv) {
Object implementor = new HelloWorld ();
String address = "http://localhost:9000/HelloWorld";
Endpoint.publish(address, implementor);
}
}
- 运行main方法发布server,访问发布地址测试是否成功
创建消费端
- File->New Project
注意Version的选择,否则idea会报错
- url填写服务端的发布地址加上?wsdl,path选择消费端项目路径,点击OK,自动生成代码
- 测试调用
public class HelloWorldClient {
public static void main(String[] argv) {
// Please, do not remove this line from file template, here invocation of web service will be inserted
try {
HelloWorld service=new HelloWorldServiceLocator().getHelloWorldPort();
String name=new String("tangzq");
System.out.println(service.sqyHai(name));
} catch (ServiceException | RemoteException e) {
e.printStackTrace();
}
}
}
控制台打印输出结果,调用成功!