JDK implementation of the implementation of WebService

We know that Java implements WebService in N ways. The first description here is the support of jdk.

1, Write server side

Write publishing interface:

//Provide a Web Service for obtaining lucky numbers@WebServicepublic interface LockNumService { @WebMethod int getNum(); @WebMethod @WebResult(name="luck") int getNumByName(@WebParam(name="name")String name);}

Interface implementation class:

//SEI//Mark this is a Web Service interface data< /span>@WebService(endpointInterface="org.qf.web.ws .LockNumService")public class LockNum implements LockNumService{ private Random rm=new Random(); @Override public int getNum() {< span class="hljs-comment">// TODO Auto-generated method stub System.out.println( "I am the server, and the client called me"< /span>); return rm.nextInt(10);} @Override public int getNumByName< /span>(String name) { // TODO Auto-generated method stub System.out.println(name+":Your lucky number will be announced soon"< /span>); return rm.nextInt(10); }}

Publish interface:

public static void main(String[] args) {//Release service, parameter description: 1. Access path 2. Object to be published Endpoint.publish("http://10.31.152.15:9090/ws/lock", new LockNum()); System.out.println("Dear , The lucky number of WS, has been released..."); }

2, using wsimport to automatically generate client code

The wsimport software is used to generate the client code in the jdk installation directory.
Open cmd, enter the specified path, use the command:
wsimport -keep http://10.31.152.15:9090/ws/lock?wsdl
You can generate the client code

< h2 id="3Client calling interface">3. Client calling interface

public static void main(String[] args) throws Exception {//Create a service object LockNumService_Service service=new LockNumService_Service(); / /Create the implementation class object of the interface---server LockNumService ln=service.getLockNumPort(); //Call the method in the interface System.out.println("I am a client:"+ln.getNum()); System.out.println("I am a client:"+ln.getNumByName("Little Er")); }

Leave a Comment

Your email address will not be published.