Sunday 15 December 2013

JAX-WS


JAX-WS

Hello Friends i hope you all are aware of web -services.i am going to demonstrate you simple example of soap web service using glashfish server. before going into details of JAX-ws you need to have understanding of wsdl docuement.when client sends Request it will be soap request & soap response will be returned by container.however in case of Glashfish ,it autogenerates wsdl document.

i personally advice you to go through elements of wsdl document  prior to going into the JAX-WS. some of the elements of WSDL document are <definations>,<operation>,<service>,<porttype>,<binding>,<type> etc.

If i want to make web service than i will simply mark class as @WebService. by default all methods defined in the class will be  exposed as web methods.for detail understanding of JAX-WS go to http://oak.cs.ucla.edu/cs144/reference/WSDL/

i am going to demonstrate you product Catalog Web Service.

Sample Code:
//Model Data
package org.demo.model;

public class Product
{
    private String name;
    private String sku;
    private double price;
   
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSku() {
        return sku;
    }

    public void setSku(String sku) {
        this.sku = sku;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

   
    public Product() { }
   
    public Product(String name,String sku,double price)
    {
        this.name=name;
        this.sku=sku;
        this.price=price;
       
    }
   
   

}

//Service Implementation consisting of bussiness logic

package org.demo.bussiness;

import java.util.*;

import javax.jws.WebMethod;

public class ProductServiceImpl
{
   
    List<String> booklist =new ArrayList<>();
    List<String> musiclist=new ArrayList<>();
    List<String> movielist= new ArrayList<>();

    public ProductServiceImpl()
    {
    booklist.add("Inferno");
    booklist.add("Joyland");
    booklist.add("The Game Of Thrones");

    musiclist.add("Random Access Memories");
    musiclist.add("Night Visions");
    musiclist.add("Unorthodox Jukebox");

    movielist.add("Oz the Great and Powerful");
    movielist.add("Despicable Me");
    movielist.add("Star Trek Into Darkness");
    }
   
    public List<String> getProductCategories()
    {
        List<String> categories = new ArrayList<>();
       
        categories.add("book list");
        categories.add("Music list");
        categories.add("Movie list");
       
        return categories;
       
       
    }
    public List<String> getProducts(String catname)
    {
        switch (catname.toLowerCase()) {
        case "books":
            return booklist;
        case "music":
            return musiclist;
        case "movies":
            return movielist;
        }
        return null;
    }
   
    public boolean addproduct(String  category,String product)
    {
        switch (category.toLowerCase()) {
        case "books":
            booklist.add(product);
            break;
        case "music":
            musiclist.add(product);
            break;
        case "movies":
            movielist.add(product);
            break;
        default:
            return false;
        }
        return true;
       
       
    }


}


//End Point Interface which client will able to see
package org.demo.nisarg;

import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;
import java.util.*;

@WebService(name="TestmartCatalog" ,targetNamespace="nisargpathak.blogspot.in")
public interface ProductCatalogInterface
{
   
    @WebMethod(action="fetch_categories", operationName="fetchCategories")
    public List<String> getProductCategories();
   
   
    @WebMethod
    public List<String> getProducts(String productname);
   
    @WebMethod
    public boolean addproduct(String productname,String category);
   
    @WebMethod
    public List<String> getProductv2(String productname);
   

}

//Service First Approach


package org.demo.nisarg;

import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import org.demo.bussiness.ProductServiceImpl;


@WebService(endpointInterface="org.demo.nisarg.ProductCatalogInterface", portName="TestMartCatalogPort", serviceName="TestMartCatalogService")
public class ProductCatalog implements ProductCatalogInterface
{

    ProductServiceImpl product= new ProductServiceImpl();
   
    @Override
    @WebMethod
    public List<String> getProductCategories()
    {
       
        return   product.getProductCategories();
    }

    @Override
    @WebMethod
    public List<String> getProducts(@WebParam(partName="pName")String productname)
    {
        // TODO Auto-generated method stub
        return product.getProducts(productname);
    }

    @Override
    @WebMethod
    public boolean addproduct(@WebParam(partName="Product-Name")String productname, @WebParam(partName="Category")String category)
    {
        // TODO Auto-generated method stub
        return  product.addproduct(productname, category);
    }

    @Override
    @WebMethod
    public List<String> getProductv2(String productname)
    {
        // TODO Auto-generated method stub
        return product.getProducts(productname);
    }

}


WSDL Document :
definitions targetNamespace="http://nisarg.demo.org/" name="TestMartCatalogService">
<import namespace="www.testmart.com" location="http://nisarg-pc:8080/jax-ws_demo/TestMartCatalogService?wsdl=1"/>
<binding name="TestMartCatalogPortBinding" type="ns1:TestmartCatalog">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>

<operation name="fetchCategories">
<soap:operation soapAction="fetch_categories"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="getProducts">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="addproduct">
<soap:operation soapAction=""/>
<input><soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="getProductv2">
<soap:operation soapAction=""/>
<input><soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="TestMartCatalogService">
<port name="TestMartCatalogPort" binding="tns:TestMartCatalogPortBinding">
<soap:address location="http://nisarg-pc:8080/jax-ws_demo/TestMartCatalogService"/>
</port>
</service>
</definitions>

Run it on Glashfish you can find tester link as well in order to test functionality of web service.

Thanks
for any query Regarding post ping me on pathak.nisarg@yahoo.com

No comments:

Post a Comment

Spring Boot SSL configuration -Tomcat Server

Hi Friends hope you all are doing well. Today I am going to demonstrate about how to configure SSL in Spring boot web Application. Need o...