Using JAX-WS to develop Web Services
This article provides how to develop soap-based Web services using Java. The client can be Perl, Ruby, Python or Java.
Java SE 6 encapsulates JAX-WS (Java API for XML-web services), while JAX-WS also supports soap-based Web Services and rest-style Web Services. The JAX-WS can usually be abbreviated as JWS, and the current version of JWS is 2.x.
Soap-based Web services can be implemented by a single Java class, but it is best to use the "interface + Implementation" method to achieve the best.
The Web service interface is called sei, that is, the service endpoint interface;
The implementation of Web Services is called SIB, that is, service implementation bean.
SIB can be a pojo or stateless session EJB. The sib in this article is a common Java class. You can publish Web services through the JDK 6 class library.
Code 1: Service Interface sei
Package myweb. service; <br/> Import javax. JWS. webService; <br/> Import javax. JWS. webmethod; <br/> Import javax. JWS. soap. soapbinding; <br/> Import javax. JWS. soap. soapbinding. style; <br/> @ WebService <br/> @ soapbinding (style = style. RPC) <br/> Public interface timeserver {<br/> @ webmethod <br/> string gettimeasstring (); <br/> @ webmethod <br/> long gettimeaselapsed (); <br/>}
Code 2: service implementation class SIB
Package myweb. service; <br/> Import Java. text. dateformat; <br/> Import Java. util. date; <br/> Import javax. JWS. webService; <br/> @ WebService (endpointinterface = "myweb. service. timeserver ") <br/> public class timeserverimpl implements timeserver {<br/>/** <br/> * returns the number of milliseconds from 00:00, January 1, January 1, 1970 <br/> */<br /> Public long gettimeaselapsed () {<br/> return new date (). gettime (); <br/>}< br/>/** <br/> * return a date in the format of "2009-12-21" <br/> */<br/> Public String gettimeasstring () {<br/> date = new date (); <br/> dateformat df = dateformat. getdateinstance (); <br/> return DF. format (date); <br/>}< br/>}
Code 3: publish a service
Package myweb. service; <br/> Import javax. XML. WS. endpoint; <br/> public class timeserverpublisher {<br/> Public static void main (string [] ARGs) {<br/> // The first parameter is the published URL <br/> // The second parameter is the sib implementation <br/> endpoint. publish ("http: // 127.0.0.1: 10100/myweb", new timeserverimpl (); <br/>}< br/>
Compile the above Code:
Javac myweb/service/*. Java
Running Service:
Java myweb/service/timeserverpublisher
In the browser address bar, enter:
Http: // localhost: 10100/myweb? WSDL
Shows:
You can also compile the client code Test Service.
Java client:
Package myweb. client; <br/> Import javax. XML. namespace. QNAME; <br/> Import javax. XML. WS. service; <br/> Import java.net. URL; <br/> Import myweb. service. *; <br/> public class timeclient {<br/> Public static void main (string [] ARGs) throws exception {<br/> URL url = new URL ("http: /// localhost: 10100/myweb? WSDL "); <br/> // The first parameter is the service URI <br/> // The second parameter is the service name published in WSDL <br/> QNAME = new QNAME (" http://service.myweb /", "timeserverimplservice"); <br/> // create a service <br/> service = service. create (URL, QNAME); <br/> // extract the endpoint interface and the service "Port ". <Br/> timeserver EIF = service. getport (timeserver. class); <br/> system. out. println (EIF. gettimeasstring (); <br/> system. out. println (EIF. gettimeaselapsed (); <br/>}< br/>}
Run the client and the result is as follows:
2009-12-21
1261402511859
You can also use Ruby to write the client as follows:
#! /Usr/bin/ruby <br/> # One ruby package for soap-based services <br/> require 'soap/wsdldriver' <br/> wsdl_url = 'HTTP: // 127.0.0.1: 10100/myweb? WSDL '</P> <p> service = soap: wsdldriverfactory. new (wsdl_url ). create_rpc_driver <br/> # Save request/response messages in files named '... soapmsgs... '<br/> service. wiredump_file_base = 'soapmsg' <br/> # invoke service operations. <br/> result1 = service. gettimeasstring <br/> result2 = service. gettimeaselapsed <br/> # output results. <br/> puts "current time is: # {result1}" <br/> puts "elapsed milliseconds from the epoch :#{ result2 }"
The running result is the same!