服务端
步骤
- 创建接口
- 创建实现类
- 开启服务
目录结果
代码
- SysMenuService
package com.laolang.kmservice.service;
import javax.jws.WebService;
@WebService
public interface SysMenuService {
String sayHello();
}
- SysMenuServiceImpl
package com.laolang.kmservice.service.impl;
import javax.jws.WebService;
import com.laolang.kmservice.service.SysMenuService;
@WebService(endpointInterface = "com.laolang.kmservice.service.SysMenuService")
public class SysMenuServiceImpl implements SysMenuService {
@Override
public String sayHello() {
return "hello world";
}
}
- KmServiceService
package com.laolang.kmservice;
import javax.xml.ws.Endpoint;
import com.laolang.kmservice.service.impl.SysMenuServiceImpl;
public class KmServiceService {
public static void main(String[] args) {
String address = "http://localhost:8093/ns";
Endpoint.publish(address, new SysMenuServiceImpl());
System.out.println("启动成功");
}
}
验证
客户端
生成代码
cmd进入src目录,执行如下命令
wsimport -keep http://localhost:8093/ns?wsdl
生成如下代码
调用
添加一个main函数调用
package com.laolang.kmservice;
import com.laolang.kmservice.service.impl.SysMenuServiceImplService;
public class KmServiceClient {
public static void main(String[] args) {
SysMenuServiceImplService sysMenuService = new SysMenuServiceImplService();
String sayHello = sysMenuService.getSysMenuServiceImplPort().sayHello();
System.out.println(sayHello);
}
}
运行结果如下所示