Simply use Java's annotation tags to publish standard Java methods as Web service, but not all Java classes can be published as Web service. Java class to be a bean that implements a Web service, it needs to follow these guidelines:
-
This class must be a public class
-
These classes cannot be final or abstract.
-
This class must have a public default constructor
-
This class must never have a finalize () method
Annotation [email protected] (name= "Example", targetnamespace= "Http://www.jsoso.com/wstest", servicename= " Example ")
@WebService tags primarily expose classes as WebService, where the targetnamespace attribute defines its own namespace, ServiceName defines < definitions > tags and <service The Name property of the > tag.
Annotation 2: @SOAPBinding (STYLE=SOAPBINDING.STYLE.RPC)
The @SOAPBinding tag defines the message protocol for SOAP in the WSDL document, where the Style property corresponds to the document type of the soap, optionally with RPC and document
Annotation 3: @WebMethod (operationname= "Tosayhello", action= "SayHello", Exclude=false)
@WebMethod define how the Web service works,
Property action corresponds to the activity of the action, such as <soap:operation soapaction= "SayHello"/>
The OperationName property matches the name of the wsdl:operation, such as <operation name= "Tosayhello" parameterorder= "UserName" >
Property exclude is used to prevent an inherited method from being exposed as a Web service, false by default
Annotation 4: @WebResult (name= "Returnword")
@ Webresult defines a method that returns a worthy name, such as <part name= "Returnword" type= "xsd:string"/>
Annotation 5: @WebParam (partname= "person", mode=mode.in
@WebParam defines the parameter name of the method, such as <part name= "person" type= "Tns:person"/>, where the Mode property represents the direction of the parameter, and the optional value is In/out/inout
-
Package org.jsoso.jws.server; import java.util.arraylist; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.webresult; import javax.jws.webservice; import javax.jws.webparam.mode; import javax.jws.soap.soapbinding; / * Class */ @WebService for WebService services (name= "Example", targetnamespace= "http:/ /www.jsoso.com/wstest ", servicename=" Example ") @SOAPBinding (STYLE=SOAPBINDING.STYLE.RPC) public class Example { private ArrayList< Person> persons = new arraylist<person> (); /** * * returns a string * @param username * @return */ @WebMethod (operationname= "Tosayhello", action= "SayHello", Exclude=false) @WebResult (name= "Returnword")//Customize the description of the method return value in the WSDL related public String sayhello (@WebParam (name= "UserName") string username) { return "Hello:" &NBSP;+&NBSP;USERNAME;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;} /** * web The return value of the services method and the type of the parameter cannot be interface * @param person * @return * @throws HelloException */ @WebMethod (operationname= "SayHello", action= "SayHello") @WebResult (partname= "personlist") public person[] sayhello (@WebParam (partname= "person", mode=mode.in) person person, string username) throws helloexception { if (person == null | | person.getname () == null) { throw new helloexception ("Say hello error, the image is empty. "); } system.out.println (Person.getname () + " to " + username + " said Hello, I am this year" + person.getage () + "Years"); persons.add (person); &Nbsp; return persons.toarray (new person[0]); } }
annotation tags for Java