最详细的idea创建webservice教程

最详细的idea创建webservice教程

创建服务端

  1. File->New Project
    在这里插入图片描述点击next,会自动生成demo
  2. 将要发布的类加上@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);
  }
}
  1. 运行main方法发布server,访问发布地址测试是否成功
    在这里插入图片描述

创建消费端

  1. File->New Project

在这里插入图片描述注意Version的选择,否则idea会报错

  1. url填写服务端的发布地址加上?wsdl,path选择消费端项目路径,点击OK,自动生成代码在这里插入图片描述
  2. 测试调用
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();
    }
  }
}

控制台打印输出结果,调用成功!


版权声明:本文为weixin_44741590原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。