Skip to content

Instantly share code, notes, and snippets.

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/2915642 to your computer and use it in GitHub Desktop.
Save sivaprasadreddy/2915642 to your computer and use it in GitHub Desktop.
RESTEasy + Spring Sample Code
package com.sivalabs.resteasydemo;
import java.util.List;
import org.springframework.stereotype.Service;
import com.sivalabs.resteasydemo.MockUserTable;
@Service
public class UserService
{
public void save(User user){
MockUserTable.save(user);
}
public User getById(Integer id){
return MockUserTable.getById(id);
}
public List<User> getAll(){
return MockUserTable.getAll();
}
public void delete(Integer id){
MockUserTable.delete(id);
}
}
package com.sivalabs.resteasydemo;
import java.util.List;
import javax.ws.rs.Consumes;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_XML)
public class UserResource
{
@Autowired
private UserService userService;
@Path("/")
@GET
public Response getUsersXML()
{
List<User> users = userService.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) {
User user = userService.getById(id);
return Response.ok(user).build();
}
@Path("/")
@POST
public Response saveUser(User user) {
userService.save(user);
return Response.ok("<status>success</status>").build();
}
@Path("/{id}")
@DELETE
public Response deleteUser(@PathParam("id") Integer id) {
userService.delete(id);
return Response.ok("<status>success</status>").build();
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.sivalabs.resteasydemo"/>
</beans>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment