In this blog post I am going to discuss how to use the
custom objects in the web service. This EmployeeService will have two operations
namely addEmployee() and getEmployees(). Let’s start coding.
EmployeeService.java
(service interface)
package com.sri.service;
import javax.jws.WebMethod;
import
javax.jws.WebService;
import
com.sri.model.Employee;
@WebService
public interface EmployeeService {
@WebMethod
public void addEmployee(Employee e);
@WebMethod
public Employee[]
getEmployees();
}
EmployeeServiceImpl.java
(service Implementation)
package com.sri.service;
import java.util.ArrayList;
import
javax.jws.WebService;
import com.sri.model.Employee;
@WebService(endpointInterface="com.sri.service.EmployeeService")
public class EmployeeServiceImpl implements EmployeeService{
private ArrayList<Employee> employee;
@Override
public void addEmployee(Employee e) {
if(employee == null)
employee = new ArrayList<Employee>();
employee.add(e);
}
@Override
public Employee[] getEmployees()
{
return (Employee[])employee.toArray();
}
}
Employee.java
(pojo class)
package com.sri.model;
public class Employee {
private int empId;
private String empName;
private Department deptId;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName()
{
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Department
getDeptId() {
return deptId;
}
public void setDeptId(Department
deptId) {
this.deptId = deptId;
}
}
Department.java
(Pojo
class)
package com.sri.model;
public class Department {
private int deprtmentId;
private String departmentName;
public int getDeprtmentId() {
return deprtmentId;
}
public void setDeprtmentId(int deprtmentId) {
this.deprtmentId = deprtmentId;
}
public String
getDepartmentName() {
return departmentName;
}
public void
setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}
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="Employee"
implementation="com.sri.service.EmployeeServiceImpl"
url-pattern="/employee"/>
</endpoints>
For deploying this webservice in tomcat you can refer.
Now the webservice would be available in the following location.
http://localhost:8080/Employee/employee?wsdl
In the WSDL we used to define the input arguments and return
types. If the input arguments are custom objects then how it get declared in
WSDL? In this web service we are using two custom objects Employee and
Department. These objects are defined in the another schema file associated
with WSDL. See the line highlighted in the WSDL
No comments:
Post a Comment