☰ See All Chapters |
How to return object from web service
In this tutorial we will learn to return object from jax-ws soap web service. We are returning the object of Person class. The class whose objects are being returned from web service should be serializable. Do not forget to implement Serializable interface.
Person.java
package com.manum.hassan;
import java.io.Serializable;
public class Person implements Serializable{
private String name; private int age; private int id;
public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString(){ return id+"::"+name+"::"+age; } } |
PersonService.java
package com.manum.hassan;
import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style;
@WebService @SOAPBinding(style = Style.RPC) //@SOAPBinding(style = Style.DOCUMENT) public interface PersonService{ @WebMethod Person getPerson(String name); } |
PersonServiceImpl.java
package com.manum.hassan;
import javax.jws.WebService;
//Service Implementation @WebService(endpointInterface = "com.manum.hassan.PersonService") public class PersonServiceImpl implements PersonService{
@Override public Person getPerson(String name) { Person person = new Person(); if(name.equals("Manu")){ person.setAge(10); person.setId(100); person.setName("Manu Manjunatha"); } else if(name.equals("Liki")){ person.setAge(20); person.setId(200); person.setName("Likitha Manjunatha"); } else if(name.equals("Adi")){ person.setAge(30); person.setId(300); person.setName("AdiTemp"); } else { person.setAge(-1); person.setId(-100); person.setName("Not Found"); } return person; } } |
PersonPublisher.java
package com.manum.hassan;
import javax.xml.ws.Endpoint;
public class PersonPublisher{
public static void main(String[] args) { Endpoint.publish("https://localhost:7779/ws/person", new PersonServiceImpl()); System.out.println("done"); }
} |
Output:
Run PersonPublisher.java as java application. Access the web services at "https://localhost:7779/ws/person" |
All Chapters