rest - How Jax-RS used in Restfull Webservices and JAX-WS used in SOAP ? -


jax-ws( java api xml webservice) ---- set of api creating webservices in xml format . jax-rs (java api used developer develop rest web application easily)

i trying understand api used .

please me in understand concept .

soap , restfull webservices standards. describes how soap/rest web services should be. example soap web service call starts envelope , can have (soap) header , (saop) body. soap services calls uses post http method default. more detail, please check soap specification , restful web services.

so, java communutiy tries follows these specifications. instead copying these specifications java environment, build api's on top of these.

let me try explain example, lets developing "hello, world" service. want use soap service architecture. when finish designing service, write own wsdl document (which in soap specification). in wsdl document define object using xml, , define service methods soapaction , operation tag. this.

<?xml version='1.0' encoding='utf-8'?> <wsdl:definitions xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://helloworld.bahadirakin.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="helloworldserviceservice" targetnamespace="http://helloworld.bahadirakin.com/">   <wsdl:types> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns="http://helloworld.bahadirakin.com/" attributeformdefault="unqualified" elementformdefault="unqualified" targetnamespace="http://helloworld.bahadirakin.com/">   <xs:complextype name="hellorequest">     <xs:sequence>       <xs:element minoccurs="0" name="firstname" type="xs:string"/>       <xs:element minoccurs="0" name="lastname" type="xs:string"/>     </xs:sequence>   </xs:complextype>   <xs:element name="hellorequest" nillable="true" type="hellorequest"/>   <xs:element name="sayhelloresponse" nillable="true" type="xs:string"/> </xs:schema>   </wsdl:types>   <wsdl:message name="sayhelloresponse">     <wsdl:part element="tns:sayhelloresponse" name="sayhelloresponse">     </wsdl:part>   </wsdl:message>   <wsdl:message name="sayhello">     <wsdl:part element="tns:hellorequest" name="hellorequest">     </wsdl:part>   </wsdl:message>   <wsdl:porttype name="helloworldservice">     <wsdl:operation name="sayhello">       <wsdl:input message="tns:sayhello" name="sayhello">     </wsdl:input>       <wsdl:output message="tns:sayhelloresponse" name="sayhelloresponse">     </wsdl:output>     </wsdl:operation>   </wsdl:porttype>   <wsdl:binding name="helloworldserviceservicesoapbinding" type="tns:helloworldservice">     <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>     <wsdl:operation name="sayhello">       <soap:operation soapaction="sayhello" style="document"/>       <wsdl:input name="sayhello">         <soap:body use="literal"/>       </wsdl:input>       <wsdl:output name="sayhelloresponse">         <soap:body use="literal"/>       </wsdl:output>     </wsdl:operation>   </wsdl:binding>   <wsdl:service name="helloworldserviceservice">     <wsdl:port binding="tns:helloworldserviceservicesoapbinding" name="helloworldserviceport">       <soap:address location="http://localhost:8080/camel-soap/helloworldinternal"/>     </wsdl:port>   </wsdl:service> </wsdl:definitions> 

in example define service contains method named sayhello gets parameter of type hellorequest , returns of type sayhelloresponse. , lets defined sayhelloresponse

<xs:element name="sayhelloresponse" nillable="true" type="xs:string"/> 

it string, defined using xml.

but when change perspective xml java, won't define object xml instead use classes. also, won't define methods using soapaction's or other xml tag. write methods in java. need framework make these kind of changes you. otherwise end writing framework make these kind of change possible. , also, able define simple pojo soap (or rest) services annotations.

so, jax-ws , jax-rs names of java standards (apis) handling soap , restful service architectures. using these frameworks never worry how change java method web services method. here jax-ws version of wsdl given above. first lets @ our request parameter hellorequest.

import javax.xml.bind.annotation.xmlaccesstype; import javax.xml.bind.annotation.xmlaccessortype; import javax.xml.bind.annotation.xmltype;  @xmlaccessortype(xmlaccesstype.field) @xmltype(name = "hellorequest", proporder = {     "firstname",     "lastname" }) public class hellorequest {      protected string firstname;     protected string lastname;     // getters & setters } 

as can see pojo. , lets our service interface.

import javax.jws.webmethod; import javax.jws.webparam; import javax.jws.webresult; import javax.jws.webservice; import javax.jws.soap.soapbinding; import javax.xml.bind.annotation.xmlseealso;  @webservice(targetnamespace = "http://helloworld.bahadirakin.com/", name = "helloworldservice") @soapbinding(parameterstyle = soapbinding.parameterstyle.bare) public interface helloworldservice {      @webresult(name = "sayhelloresponse", targetnamespace = "http://helloworld.bahadirakin.com/", partname = "sayhelloresponse")     @webmethod     public java.lang.string sayhello(         @webparam(partname = "hellorequest", name = "hellorequest", targetnamespace = "http://helloworld.bahadirakin.com/")         hellorequest hellorequest     ); } 

as can see there no envelope, no body, no http post or else. write java interface , match them soap using jax-ws api. yes, still need have basic understanding of soap. can develop soap web service jax-ws api.

since jax-ws , jax-rs apis, there different implementation of each api. example jax-ws there metro, cxf, axis2 implementations. , jax-rs there jersey, cxf etc. implementations. fallows same api implementations totally different. applications servers provide own implementations.

but still need convert java object xml since soap specification forces it. there other libraries convert java xml , xml java. such jaxb , xstream. also, when develop rest services want convert java objects json. there tools purpose jackson or gson.

but jax-rs , jax-ws not way develop rest or soap web services. example in order develop restful web services may use springmvc.

"then, why should use specification" may ask. totally use specification or not. theoretically, if use specification, can change implementation time want. but, since each different implementations provides different cool features, end highly coupled application. may not change easily.


Comments