soap webservice using jax ws ri

Steps to create webservice using jax ws
1) Create the dynamic web project using eclipse as shown in the below snapshot..
2) Create the package com.employee.service
3) Create EmployeeService.java
4) Create Employee.java
5) Create Address.java
6) Run the following command under your project path i.e..\build\classes
  G:\work_space\mobile\EmployeeService\build\classes>wsgen -keep -r   G:\work_space\mobile\EmployeeService\build\gen -s G:\work_space\mobile\EmployeeService\build\
gen -verbose -cp . com.employee.service.EmployeeService -wsdl
7) This command will generate gen folder with below artifacts
  jaxws\GetEmployeeDetails.java
  jaxws\GetEmployeeDetailsResponse.java
  EmployeeServiceService_schema1.xsd
  EmployeeServiceService.xml
8) Download the JAX-WS RI jars from https://jax-ws.java.net and copy to WEB_INF\lib folder
9) Build the project with out compilation errors
10) Create sun-jaxws.xml under WEB-INF folder
11) Create web.xml under WEB-INF folder
12) Deploy the application on application server
13) Check the console/log's if any errors while publishing
14) Access the Service in the browser with the URL as http://localhost:8080/EmployeeService/employee
15) Check the WSDL in the browser with the URL http://localhost:8080/EmployeeService/employee?wsdl
16) If the WSDL is opening correctly you can check service request/response using SOAP UI.
  

1) EmployeeService.java
package com.employee.service;

import java.io.StringWriter;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

@WebService
public class EmployeeService {
 @WebMethod
 public String getEmployeeDetails(int number1) {
  Employee employee = new Employee();
  Address address = new Address();
  String resString = "";
  employee.setId(1234);
  employee.setName("Jhon");
  address.setCity("bangalore");
  employee.setAddress(address);
  try {
   resString = marshal(employee);
  } catch (JAXBException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return resString;
 }

 private static String marshal(Employee employee) throws JAXBException {
  final Marshaller m = JAXBContext.newInstance(Employee.class)
    .createMarshaller();
  m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  final StringWriter w = new StringWriter();
  m.marshal(employee, w);
  return w.toString();
 }
}
2) Employee.java
package com.employee.service;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "employee")
public class Employee {
 @XmlAttribute
 private int id;
 private String name;
 private double salary;
 private String designation;
 private Address address;

 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public double getSalary() {
  return salary;
 }
 public void setSalary(double salary) {
  this.salary = salary;
 }
 public String getDesignation() {
  return designation;
 }
 public void setDesignation(String designation) {
  this.designation = designation;
 }
 public Address getAddress() {
  return address;
 }
 public void setAddress(Address address) {
  this.address = address;
 }
}
3) Address.java
package com.employee.service;
public class Address {
 private String line1;
 private String line2;
 private String city;
 private String state;
 private long zipcode;

 public String getLine1() {
  return line1;
 }
 public void setLine1(String line1) {
  this.line1 = line1;
 }
 public String getLine2() {
  return line2;
 }
 public void setLine2(String line2) {
  this.line2 = line2;
 }
 public String getCity() {
  return city;
 }
 public void setCity(String city) {
  this.city = city;
 }
 public String getState() {
  return state;
 }
 public void setState(String state) {
  this.state = state;
 }
 public long getZipcode() {
  return zipcode;
 }
 public void setZipcode(long zipcode) {
  this.zipcode = zipcode;
 }
}
4) 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="EmployeeService"
    implementation="com.employee.service.EmployeeService"
    url-pattern="/employee" />
</endpoints>
5) web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">
  <listener>
    <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>EmployeeService</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>EmployeeService</servlet-name>
    <url-pattern>/employee</url-pattern>
  </servlet-mapping>
  <session-config>
    <session-timeout>60</session-timeout>
  </session-config>
</web-app>
We can check the web service is working or not using the URL as shown below
Generated EmployeeServiceService_schema1.xsd
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" targetNamespace="http://service.employee.com/" xmlns:tns="http://service.employee.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="getEmployeeDetails" type="tns:getEmployeeDetails"/>
  <xs:element name="getEmployeeDetailsResponse" type="tns:getEmployeeDetailsResponse"/>
  <xs:complexType name="getEmployeeDetails">
    <xs:sequence>
      <xs:element name="arg0" type="xs:int"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="getEmployeeDetailsResponse">
    <xs:sequence>
      <xs:element name="return" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Generated EmployeeServiceService.wsdl
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<definitions targetNamespace="http://service.employee.com/" name="EmployeeServiceService" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://service.employee.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://service.employee.com/" schemaLocation="EmployeeServiceService_schema1.xsd"/>
    </xsd:schema>
  </types>
  <message name="getEmployeeDetails">
    <part name="parameters" element="tns:getEmployeeDetails"/>
  </message>
  <message name="getEmployeeDetailsResponse">
    <part name="parameters" element="tns:getEmployeeDetailsResponse"/>
  </message>
  <portType name="EmployeeService">
    <operation name="getEmployeeDetails">
      <input message="tns:getEmployeeDetails"/>
      <output message="tns:getEmployeeDetailsResponse"/>
    </operation>
  </portType>
  <binding name="EmployeeServicePortBinding" type="tns:EmployeeService">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <operation name="getEmployeeDetails">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <service name="EmployeeServiceService">
    <port name="EmployeeServicePort" binding="tns:EmployeeServicePortBinding">
      <soap:address location="REPLACE_WITH_ACTUAL_URL"/>
    </port>
  </service>
</definitions>