{"id":1161,"date":"2010-11-18T19:47:43","date_gmt":"2010-11-18T14:17:43","guid":{"rendered":"http:\/\/naveenbalani.com\/?p=1161"},"modified":"2017-03-24T18:11:04","modified_gmt":"2017-03-24T12:41:04","slug":"creating-contract-first-web-services-using-jax-ws-stack","status":"publish","type":"post","link":"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-contract-first-web-services-using-jax-ws-stack\/","title":{"rendered":"Creating Contract First web services using JAX-WS Stack"},"content":{"rendered":"<p>This is an ongoing blog to Web Services development. In my previous <a href=\"http:\/\/naveenbalani.com\/index.php\/2010\/11\/jax-ws-stack-for-developing-code-first-web-services\/\">blog<\/a>, I talked about JAX-WS framework in detail and how to create code first web services. In this blog, I would describe how to create Contract first web services.<\/p>\n<h3>Contract First Web Service<\/h3>\n<p>With Contract First approach for web services development, you start off with defining the XML Schema\/WSDL contract for your service instead of Java code. One you have defined a WS- Compatible WSDL, you can take any programming language like Java or .Net for its implementation.<\/p>\n<p><em> Note : For Contract First Web service development , you must be have a good idea about XML , XML Schema and WSDL specification.<\/em><\/p>\n<p>As you have already analyzed the WSDL and XML Schema for Order Process Web Service , you will take the same WSDL artifact and look at how to generate the implementation stubs from WSDL and publish it.<\/p>\n<h3>Generating Implementation Stubs from WSDL<\/h3>\n<p>The JAX-WS specification provides a detail mapping from a service defined in WSDL to the Java classes that will implement that service. You already looked at how the WSDL was generated from JAX-WS annotations defined on OrderProcessService service implementation and how th Java objects (inputs ad output to service methods) were converted into XML schemas and referenced in WSDL messages based on JAXB specification. This process is the reverse of code first approach , of generating java implemetations from WSDL and Schema contracts.<\/p>\n<p><strong>Generating Implementation Stubs from WSDL<\/strong><\/p>\n<p>The process outlined here is similar to Creating Web Service client process described in my earlier <a href=\"http:\/\/naveenbalani.com\/index.php\/2010\/11\/jax-ws-stack-for-developing-code-first-web-services\/\">blog<\/a>, with the intention of using the generated JAX-WS artifacts for creating service implementation. As you recollect , the wsimport based on the WSDL generates the JAXB binding classes along with service interfaces from the WSDL. You will implement the service interface generated to provide your implemetation and later publish it.<!--more--><\/p>\n<p>For simplicity , lets generate the JAX-WS artifacts in a seperate package, navigate to bin directory which contains all the compiled class files (previously created as a result of executing compile.bat) and run the following command.<\/p>\n<p><em>Note: Before running the wsimport command, make sure you have published the web service by running the OrderWebServicePublisher<\/em><\/p>\n<p><strong>wsimport -keep -p com.nb.beginjava6.service.contractfirst http:\/\/localhost:8080<\/strong><\/p>\n<p><strong>\/OrderProcessWeb\/orderprocess?wsdl<\/strong><\/p>\n<p>Note: The artifacts generated will be same as mentioned earlier in my previous <a href=\"http:\/\/naveenbalani.com\/index.php\/2010\/11\/jax-ws-stack-for-developing-code-first-web-services\/\">blog<\/a>, in creating web service client section.<\/p>\n<p>You will implement the OrderProcess service interface that generated by the wsimport tool to provided required implementation for the service.The code for service implemetation is provided below.<\/p>\n<pre>\r\npackage com.nb.beginjava6.service.contractfirst;\r\n\r\nimport javax.jws.WebService;\r\n\r\n@WebService(serviceName = \"OrderProcess\",\r\n\r\nportName = \"OrderProcessPort\",\r\n\r\nendpointInterface=\"com.nb.beginjava6.service.contractfirst.OrderProcessService\",\r\n\r\ntargetNamespace = \"http:\/\/nb.com\/beginjava6\/orderprocess\")\r\n\r\npublic class OrderProcessContractService implements OrderProcessService {\r\n\r\n\/\/The @WebMethod annotation is defined in the OrderProcessService interface.\r\n\r\npublic OrderBean processOrder(OrderBean orderBean) {\r\n\r\nSystem.out.println(\"OrderProcessContractService called\");\r\n\r\norderBean.setOrderId(\"BC15\");\r\n\r\nreturn orderBean;\r\n\r\n}\r\n\r\n}\r\n<\/pre>\n<p><strong>Code: The code listing for order process contract service.<\/strong><\/p>\n<p>The OrderProcessContractService class implements the service interface OrderProcessService (generated by wsimport tool) and provides the @WebService annotation. The @WebService annotation attaributes defined is similar to earlier OrderProcess web service implementaion, with one more attribute , endpointInterface added to it , which defines the interface class , i.e &#8220;com.nb.beginjava6.service.contractfirst.OrderProcessService&#8221;.<\/p>\n<p>You don\u2019t need to define @WebMethod annotation for processOrder methods , as the annotation is already defined for the method in the generated OrderProcessService interface. Following code shows the listing of OrderProcessService interface.<\/p>\n<pre>\r\npackage com.nb.beginjava6.service.contractfirst;\r\n\r\nimport javax.jws.WebMethod;\r\n\r\nimport javax.jws.WebParam;\r\n\r\nimport javax.jws.WebResult;\r\n\r\nimport javax.jws.WebService;\r\n\r\nimport javax.xml.ws.RequestWrapper;\r\n\r\nimport javax.xml.ws.ResponseWrapper;\r\n\r\n\/**\r\n\r\n* This class was generated by the JAXWS SI.\r\n\r\n* JAX-WS RI 2.0_02-b08-fcs\r\n\r\n* Generated source version: 2.0\r\n\r\n*\r\n\r\n*\/\r\n\r\n@WebService(name = \"OrderProcessService\", targetNamespace = \"http:\/\/nb.com\/beginjava6\/orderprocess\")\r\n\r\npublic interface OrderProcessService {\r\n\r\n\/**\r\n\r\n*\r\n\r\n* @param arg0\r\n\r\n* @return\r\n\r\n*     returns com.nb.beginjava6.service.contractfirst.OrderBean\r\n\r\n*\/\r\n\r\n@WebMethod\r\n\r\n@WebResult(targetNamespace = \"\")\r\n&lt;pre&gt;@RequestWrapper(localName = \"processOrder\", targetNamespace = \"http:\/\/nb.com\/beginjava6\/orderprocess\", className = \"com.nb.beginjava6.service.contractfirst.ProcessOrder\")&lt;\/pre&gt;\r\n@ResponseWrapper(localName = \"processOrderResponse\", targetNamespace = \"http:\/\/nb.com\/beginjava6\/orderprocess\", className = \"com.nb.beginjava6.service.contractfirst.ProcessOrderResponse\")\r\n\r\npublic OrderBean processOrder(\r\n\r\n@WebParam(name = \"arg0\", targetNamespace = \"\")\r\n\r\nOrderBean arg0);\r\n\r\n}\r\n<\/pre>\n<p><strong>Code: The code listing for generated service interface.<\/strong><\/p>\n<p>The processOrder method listed above defines @RequestWrapper and @ResponseWrapper method , in addition to @WebMethod. The @RequestWrapper and @ResponseWrapper as discussed in my previous <a href=\"http:\/\/naveenbalani.com\/index.php\/2010\/11\/jax-ws-stack-for-developing-code-first-web-services\/\">blog<\/a> , wraps the input (OrderBean) and output (OrderBean) parameter of the method (processOrder) with a wrapper class , which is required for resolve overloading conflicts in document literal mode.<\/p>\n<h3>Publishing the Order Process Contract Web Service<\/h3>\n<p>The process of publishing the web service remains the same irrespective of code-first or contract first development approach.<\/p>\n<p><strong>Publish the Order Process Web Service<\/strong><\/p>\n<p>You don\u2019t need to generate any JAX-WS artifacts , as you have already generated required JAX-WS artifacts by running the wsimport tool , when you imported the WSDL. Publish the Order Process Contract Web Service by running the following web service publisher client.<\/p>\n<p><strong>java com.nb.beginjava6.service.publish.OrderWebServiceContractFirstPublisher<\/strong><\/p>\n<p>This will publish the Order Web Service at location <a href=\"http:\/\/localhost:8081\/OrderProcessContractWeb\/orderprocess\">http:\/\/localhost:8081\/OrderProcessContractWeb\/orderprocess<\/a> .You can verify if the web service is running by displaying the Web Services Definition Language (WSDL) generated of the Order Process web service by opening the browser and navigating to the following location:<\/p>\n<p><a href=\"http:\/\/localhost:8081\/OrderProcessContractWeb\/orderprocess?wsdl\">http:\/\/localhost:8081\/OrderProcessContractWeb\/orderprocess?wsdl<\/a><\/p>\n<p>Following provides the source code of the <strong>OrderProcessContractService <\/strong>The code is excalty similar to earlier code for publishing the <strong>OrderWebServicePublisher <\/strong>client , except the location of the web service and service implementation differ.<\/p>\n<pre>\r\npackage com.nb.beginjava6.service.publish;\r\n\r\nimport javax.xml.ws.Endpoint;\r\n\r\nimport com.nb.beginjava6.service.contractfirst.OrderProcessContractService;\r\n\r\npublic class OrderWebServiceContractFirstPublisher {\r\n\r\npublic static void main(String[] args) {\r\n\r\nEndpoint.publish(\"http:\/\/localhost:8081\/OrderProcessContractWeb\/orderprocess\",\r\n\r\nnew OrderProcessContractService());\r\n\r\n}\r\n\r\n}\r\n<\/pre>\n<p><strong>Code: The code listing for publishing order process contract web service.<\/strong><\/p>\n<h3>Generating Web Service Clients from WSDL<\/h3>\n<p>The process of generating web service clients doesn\u2019t differ if you are using code-first or contract first development approach, as you generate web service clients from WSDL.<\/p>\n<p><strong>Creating Web Service Client<\/strong><\/p>\n<p>You can use the same web service client that you created for code-first approach. As the WSDL\/Schema definitions are same for both of these services expect the soap:address location in WSDL , you will execute the same web service client by passing in the WSDL URL of the OrderProcessContractService.<\/p>\n<p>Execute the web service client by providing the WSDL URL for the OrderProcessContract Service using the following command. Refer to my previous <a href=\"http:\/\/naveenbalani.com\/index.php\/2010\/11\/jax-ws-stack-for-developing-code-first-web-services\/\">blog<\/a> , about details on the OrderClient.<\/p>\n<p>java com.nb.beginjava6.service.client.OrderClient<\/p>\n<p><a href=\"http:\/\/localhost:8081\/OrderProcessContractWeb\/orderprocess?wsdl\">http:\/\/localhost:8081\/OrderProcessContractWeb\/orderprocess?wsdl<\/a><\/p>\n<p>When the web service is executed, you will see the following output at the console, where the <strong>OrderWebServiceContractFirstPublisher <\/strong>is running.<\/p>\n<p><em>OrderProcessContractService called<\/em><\/p>\n<p>At the console where the web service client is executed, you will receive the following output.<\/p>\n<p><em>Order id is BC15<\/em><\/p>\n<p>You have successfully implemented your web services using a contract first development approach. In next blog, I would describe how to intercept SOAP messages.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is an ongoing blog to Web Services development. In my previous blog, I talked about JAX-WS framework in detail and how to create code first web services. In this blog, I would describe how to create Contract first web services. Contract First Web Service With Contract First approach for web services development, you start [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2128,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,6],"tags":[136,137,64,17],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.0.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Creating Contract First web services using JAX-WS Stack - Current and Future Technology Trends by Navveen Balani<\/title>\n<meta name=\"description\" content=\"Creating Contract First web services using JAX-WS Stack -\" \/>\n<link rel=\"canonical\" href=\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-contract-first-web-services-using-jax-ws-stack\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating Contract First web services using JAX-WS Stack - Current and Future Technology Trends by Navveen Balani\" \/>\n<meta property=\"og:description\" content=\"Creating Contract First web services using JAX-WS Stack -\" \/>\n<meta property=\"og:url\" content=\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-contract-first-web-services-using-jax-ws-stack\/\" \/>\n<meta property=\"og:site_name\" content=\"Current and Future Technology Trends by Navveen Balani\" \/>\n<meta property=\"article:published_time\" content=\"2010-11-18T14:17:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-03-24T12:41:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2016\/09\/bk6.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"450\" \/>\n\t<meta property=\"og:image:height\" content=\"374\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"6 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/navveenbalani.dev\/#website\",\"url\":\"https:\/\/navveenbalani.dev\/\",\"name\":\"Current and Future Technology Trends by Navveen Balani\",\"description\":\"Current and Future Technology Trends by Navveen Balani\",\"publisher\":{\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/navveenbalani.dev\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-contract-first-web-services-using-jax-ws-stack\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2016\/09\/bk6.jpg\",\"width\":450,\"height\":374},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-contract-first-web-services-using-jax-ws-stack\/#webpage\",\"url\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-contract-first-web-services-using-jax-ws-stack\/\",\"name\":\"Creating Contract First web services using JAX-WS Stack - Current and Future Technology Trends by Navveen Balani\",\"isPartOf\":{\"@id\":\"https:\/\/navveenbalani.dev\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-contract-first-web-services-using-jax-ws-stack\/#primaryimage\"},\"datePublished\":\"2010-11-18T14:17:43+00:00\",\"dateModified\":\"2017-03-24T12:41:04+00:00\",\"description\":\"Creating Contract First web services using JAX-WS Stack -\",\"breadcrumb\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-contract-first-web-services-using-jax-ws-stack\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-contract-first-web-services-using-jax-ws-stack\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-contract-first-web-services-using-jax-ws-stack\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/navveenbalani.dev\/\",\"url\":\"https:\/\/navveenbalani.dev\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-contract-first-web-services-using-jax-ws-stack\/\",\"url\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-contract-first-web-services-using-jax-ws-stack\/\",\"name\":\"Creating Contract First web services using JAX-WS Stack\"}}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-contract-first-web-services-using-jax-ws-stack\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-contract-first-web-services-using-jax-ws-stack\/#webpage\"},\"author\":{\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\"},\"headline\":\"Creating Contract First web services using JAX-WS Stack\",\"datePublished\":\"2010-11-18T14:17:43+00:00\",\"dateModified\":\"2017-03-24T12:41:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-contract-first-web-services-using-jax-ws-stack\/#webpage\"},\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\"},\"image\":{\"@id\":\"https:\/\/navveenbalani.dev\/index.php\/articles\/creating-contract-first-web-services-using-jax-ws-stack\/#primaryimage\"},\"keywords\":\"web service contract tutorial,web service tutorial,web services frameworks,webservices\",\"articleSection\":\"Articles,Web Services\",\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/navveenbalani.dev\/#\/schema\/person\/51f7ab14b20611d95e3c7fd4ea0950bf\",\"name\":\"Navveen\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/navveenbalani.dev\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/navveenbalani.dev\/wp-content\/uploads\/2019\/07\/navveen_balani.jpeg\",\"width\":200,\"height\":200,\"caption\":\"Navveen\"},\"logo\":{\"@id\":\"https:\/\/navveenbalani.dev\/#personlogo\"},\"sameAs\":[\"http:\/\/naveenbalani.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts\/1161"}],"collection":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/comments?post=1161"}],"version-history":[{"count":14,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts\/1161\/revisions"}],"predecessor-version":[{"id":2394,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/posts\/1161\/revisions\/2394"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/media\/2128"}],"wp:attachment":[{"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/media?parent=1161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/categories?post=1161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/navveenbalani.dev\/index.php\/wp-json\/wp\/v2\/tags?post=1161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}