[Lab] Using JAXB Annotations with SOAP Web Services

We can use JAXB annoatations to specify the XML element names within the SOAP request and response.

JAXB will be making use of your default constructor and setters/getters. Remember to give a default constructor for your class in case you are having parameterized constrcutors.

 

Initial Setup

You have to do the initial setup similar to what you have done for http://javajee.com/lab-deploying-a-simple-soap-web-service-in-glassfish-server-from-eclipse.  

 

The Javabean – Employee.java

We will use the annotation @XmlType with attribute propOrder  on top of Employee class to define order of field elements.

A list of commonly used JAXB annnoations can be found @ http://javajee.com/commonly-used-jaxb-2-annotations.

 

Employee.java

package com.javajee.webservices.soap;

import javax.xml.bind.annotation.XmlType;

 

@XmlType(propOrder={"empNumber", "empName", "age"})

public class Employee {

  String empName;

  int empNumber;

  int age;

 

  public Employee(String name, int number, int age) {

    this.empName = name;

    this.empNumber = number;

    this.age = age;

  }

 

  Employee() {

  }

 

  public String getEmpName() {

    return empName;

  }

 

  public void setEmpName(String empName) {

    this.empName = empName;

  }

 

  public int getEmpNumber() {

    return empNumber;

  }

 

  public void setEmpNumber(int empNumber) {

    this.empNumber = empNumber;

  }

 

  public int getAge() {

    return age;

  }

 

  public void setAge(int age) {

    this.age = age;

  }

 

}

 

The web service – EmployeeManager.java

package com.javajee.webservices.soap;

 

import java.util.ArrayList;

import java.util.List;

 

import javax.jws.WebService;

 

@WebService

public class EmployeeManager {

 

  List<Employee> empList;

 

  EmployeeManager() {

    empList = new ArrayList<Employee>();

    createEmployees();

  }

 

  private void createEmployees() {

    Employee emp1 = new Employee();

    emp1.setEmpName("Sneha");

    emp1.setEmpNumber(1);

    empList.add(emp1);

 

    Employee emp2 = new Employee();

    emp2.setEmpName("Heartin");

    emp2.setEmpNumber(2);

    empList.add(emp2);

  }

 

  public List<Employee> getAllEmployees() {

    return this.empList;

  }

}

 

Deploying and verifying the service

Deploy the service and verify the wsdl similar to what you have done in http://javajee.com/lab-deploying-a-simple-soap-web-service-in-glassfish-server-from-eclipse.

Current wsdl url after deploying in glassfish is:

http://localhost:8080/WebServiceDemo/EmployeeManagerService?wsdl

The wsdl types section refers to a schema available @ http://localhost:8080/WebServiceDemo/EmployeeManagerService?xsd=1.

 

You can find the Emplyee fields that will be part of the xml defined in this schema.

 

The order can be verified in the response also:

 

Important Note:

Please note that, to change the response tag name or anything on the top level you will have to use JAX-WS annotations like @WebResult annoatation on top of an exposed method.

For example, to change the grouping element name from <return> to employee, add the annotation @WebResult(name = “”) on top of public List<Employee> getAllEmployees() as:

@WebResult(name="Employee")

public List<Employee> getAllEmployees() {

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!