Server development steps:
//1. Must have this annotation @WebService public class HelloService { //2. There must be a public method public void doublekill() { System.out.println("doublekill"); } public User getUser(int id) { User user=new User(); user.setId(id); user.setName("Zhang San"); user.setPwd("123456"); return user; } }
public class PublishServer { public static void main(String[] args) { //3. Publish service external access address Endpoint.publish("http://192.168.0.29:1669/helloService", new HelloService()); } }
Client development steps:
1. Generate local java files according to wsdl
wsimport -d . http://192.168.0.29:1669/helloService?wsdl Generate a local class file
wsimport -s . + address of service specification (wsdl) Generate local class files and java files
wsimport -s . -p (package name) + address of service specification (wsdl) Generate local class files and java files
2. Paste the generated package into the program
3. call
public class InvokeService { public static void main(String[] args) { HelloServiceService service=new HelloServiceService(); HelloService port = service.getHelloServicePort(); port.doublekill(); User user = port.getUser(2); System.out.println(user.getName()); }
Some annotation applications on the server side
@WebService ( targetNamespace="www.baidu.com", serviceName="HelloServicePortType", portName="ServicePortType" ) public class HelloService { @WebMethod(exclude=true) public void doubleKill(){ System.out.println("aaaa"); } public @WebResult(name="date") String getDate( @WebParam(name="date") String date){ DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return dateFormat.format(new Date()); } @WebMethod(operationName="getUserByName") public User getUserById(int id){ User user=new User(); user.setId(id); user.setMomo("123456664"); user.setUsername("Old Feng"); return user; } }