Thursday, January 16, 2014

JAX-WS (SOAP Web Services) with JAXB

JAXB - Java Architecture for XML Binding. support for marshalling and unmarshalling.

marshalling :- converting java objects to XML files/contents

unmarshalling :- converting XML contents back to the java objects


here i have develop a sample application to demonstrate simple java web service with JAXB support.

you can get the fully source code of the project with following gitHub Repo.

https://github.com/chathurangat/jax-ws-jaxb-sample-app


this is just a sample implementation with some hard coded values in the back-end.


WebService

Employee.java

package com.chathurangaonline.samples.jax.ws.jaxb.model;

public class Employee {

    private int id;
    private String empId;
    private String name;
    private String email;
    private String website;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }

    public String getName() {
        return name;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }
}




EmployeeService.java

package com.chathurangaonline.samples.jax.ws.jaxb;

import com.chathurangaonline.samples.jax.ws.jaxb.model.Employee;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface EmployeeService {

    @WebMethod
    Employee create(Employee employee);

    @WebMethod
    Employee findEmployeeById(String empId);
}




EmployeeServiceImpl.java

package com.chathurangaonline.samples.jax.ws.jaxb;

import com.chathurangaonline.samples.jax.ws.jaxb.model.Employee;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;


@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class EmployeeServiceImpl implements EmployeeService{

    @WebMethod
    public Employee create(Employee employee) {
        //todo sample implementation
        if(employee!=null && employee.getEmail()!=null && employee.getEmail().equals("chathuranga.t@gmail.com")){
            employee.setEmpId("emp4235");
            employee.setName("chathuranga tennakoon");
            employee.setWebsite("www.chathurangaonline.com");
        }
        return employee;
    }

    @WebMethod
    public Employee findEmployeeById(String empId) {
        //todo sample implementation
        if(empId!=null && empId.equals("emp4235")){
            Employee employee = new Employee();
            employee.setId(123);
            employee.setEmpId("emp4235");
            employee.setName("chathuranga tennakoon");
            employee.setWebsite("www.chathurangaonline.com");
            employee.setEmail("chathuranga.t@gmail.com");
        }
        Employee employee = new Employee();
        employee.setName("chathuranga tennakoon");
        return employee;
    }
}




web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

    <listener>
        <listener-class>
            com.sun.xml.ws.transport.http.servlet.WSServletContextListener
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>empService</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>empService</servlet-name>
        <url-pattern>/empServiceUrl</url-pattern>
    </servlet-mapping>
</web-app>





sun-jaxws.xml

<?xml version="1.0" encoding="UTF-8"?>
<endpoints
        xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
        version="2.0">
    
    <endpoint name="empService" 
              implementation="com.chathurangaonline.samples.jax.ws.jaxb.EmployeeServiceImpl" 
              url-pattern="/empServiceUrl"/>
</endpoints>




pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.chathurangaonline.jax.ws.samples</groupId>
    <artifactId>jaxb-sample-web-service</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>jaxb-sample-web-service Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
    <build>
        <finalName>employee-service</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <!--compiles with java 7-->
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <!--WAR plugin-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.5</version>
            </plugin>
        </plugins>
    </build>
</project>








WebClient

web client can be generated with the wsimport tool. the sample command is as follows. replace the value with your configurations.

wsimport  -keep  -verbose -d /home/chathuranga/Projects/jax-ws-tutorial/jax-ws-jaxb-sample-app/webClient/jaxb-client/src/main/java/  http://localhost:8080/employee-service/empServiceUrl?wsdl


EmpServiceClient.java


import com.chathurangaonline.samples.jax.ws.jaxb.Employee;
import com.chathurangaonline.samples.jax.ws.jaxb.EmployeeServiceImpl;
import com.chathurangaonline.samples.jax.ws.jaxb.EmployeeServiceImplService;

/**
 * <p>
 *     sample web service client implementation
 * </p>
 * 
 * @Author Chathuranga Tennakoon
 */
public class EmpServiceClient {

    public static void main(String[] args) {

        EmployeeServiceImplService employeeServiceImplService = new EmployeeServiceImplService();
        EmployeeServiceImpl employeeService = employeeServiceImplService.getEmployeeServiceImplPort();

        //creating the employee
        Employee employeeOb = new Employee();
        employeeOb.setEmail("chathuranga.t@gmail.com");

        Employee employeeCreated = employeeService.create(employeeOb);

        System.out.println(" employee created  ["+employeeCreated.getEmpId()+"]");

        //find the employee with id
        Employee employee = employeeService.findEmployeeById("emp4235");

        System.out.println(" employee found ["+ employee.getName()+"]");
    }
}






hope this will be helpful for you!


Thanks
Cahthuranga Tennakoon
www.chathurangaonline.com


No comments:

Post a Comment