Skip to content

Instantly share code, notes, and snippets.

@sivaprasadreddy
Created June 6, 2012 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sivaprasadreddy/2882408 to your computer and use it in GitHub Desktop.
Save sivaprasadreddy/2882408 to your computer and use it in GitHub Desktop.
RESTEasy Sample Code
package com.sivalabs.resteasydemo;
import java.util.Date;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class User
{
private Integer id;
private String name;
private String email;
private Date dob;
//setters and getters
}
package com.sivalabs.resteasydemo;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.sivalabs.resteasydemo.User;
public class MockUserTable
{
private static Map<Integer, User> USER_MAP = new HashMap<Integer, User>();
static
{
USER_MAP.put(1, new User(1,"admin","admin@gmail.com",new Date()));
USER_MAP.put(2, new User(2,"test","test@gmail.com",new Date()));
}
public static void save(User user){
USER_MAP.put(user.getId(), user);
}
public static User getById(Integer id){
return USER_MAP.get(id);
}
public static List<User> getAll(){
List<User> users = new ArrayList<User>(USER_MAP.values());
return users;
}
public static void delete(Integer id){
USER_MAP.remove(id);
}
}
package com.sivalabs.resteasydemo;
import java.util.List;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.sivalabs.resteasydemo.MockUserTable;
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
public class UserResource
{
@Path("/")
@GET
public Response getUsersXML()
{
List<User> users = MockUserTable.getAll();
GenericEntity<List<User>> ge = new GenericEntity<List<User>>(users){};
return Response.ok(ge).build();
}
@Path("/{id}")
@GET
public Response getUserXMLById(@PathParam("id") Integer id) {
return Response.ok(MockUserTable.getById(id)).build();
}
@Path("/")
@POST
public Response saveUser(User user) {
MockUserTable.save(user);
return Response.ok("<status>success</status>").build();
}
@Path("/{id}")
@DELETE
public Response deleteUser(@PathParam("id") Integer id) {
MockUserTable.delete(id);
return Response.ok("<status>success</status>").build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment