JAX-RS: 1 min RESTFul with JAXB

This is second post of the JAX-RS Tutorial: 1 min RESTFul service post extended to use JAXB. Follow the first tutorial to understand the Grizzly framework usage.

restexamplefirst

I’ve created two classes Person and the Address as shown in the above class diagram.

package com.blogspot.ojitha.rest.example.first;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Person {
	private String firstName;
	private String lastName;
	private Long id;
	private Address address;
	
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
}

 

package com.blogspot.ojitha.rest.example.first;

public class Address {
	private String street;
	private String suburb;
	private String state;
	private int postCode;
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public String getSuburb() {
		return suburb;
	}
	public void setSuburb(String suburb) {
		this.suburb = suburb;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public int getPostCode() {
		return postCode;
	}
	public void setPostCode(int postCode) {
		this.postCode = postCode;
	}

}

The most important annotation is @XmlRootElement which is from JAXB. Following service is capable of passing Person and Address classes as XML to the consumer.

package com.blogspot.ojitha.rest.example.first;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.xml.ws.http.HTTPException;

@Path("personservice")
public class PersonService {

	Map<Long, Person> people = new HashMap<Long, Person>();
	public PersonService(){
		Person p = new Person();
		p.setId(new Long(1));
		p.setFirstName("Ojitha");
		p.setLastName("Kumanayaka");
		Address a = new Address();
		a.setState("Tenison Woods");
		a.setSuburb("Bonython");
		a.setState("ACT");
		a.setPostCode(2905);
		p.setAddress(a);
		people.put(p.getId(), p);
	}
	
	@GET
	@Path("{id}")
	@Produces({"application/xml"})
	public Person findById(@PathParam("id")Long id){
		Person p = null;
		p = people.get(new Long(id));
		if (p!=null) return p;
		else throw new HTTPException(405);
	}
}

The most important element to consider is @Produces in the above service. You will get the following result when you test the application.

<person>
	<address>
		<postCode>2905</postCode>
		<state>ACT</state>
		<suburb>Bonython</suburb>
	</address>
	<firstName>Ojitha</firstName>
	<id>1</id>
	<lastName>Kumanayaka</lastName>
</person>

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python