Created
January 15, 2015 20:23
-
-
Save friek/d947e666d51a0eedfec0 to your computer and use it in GitHub Desktop.
Minimal jaxrs setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.persistence.*; | |
| @Entity | |
| public class Resource | |
| { | |
| @Id | |
| private int id; | |
| private String value; | |
| public int getId() | |
| { | |
| return id; | |
| } | |
| public void setId(int id) | |
| { | |
| this.id = id; | |
| } | |
| public int getValue() | |
| { | |
| return value; | |
| } | |
| public void setValue(String value) | |
| { | |
| this.value = value; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.persistence.EntityManager; | |
| import javax.persistence.Id; | |
| import javax.persistence.ManyToOne; | |
| import javax.persistence.PersistenceContext; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.ws.rs.*; | |
| import javax.ws.rs.core.MediaType; | |
| import javax.ws.rs.core.Response; | |
| import java.util.Arrays; | |
| /** | |
| * JAXRS Service class. | |
| * URL: /<mod name>/resources/resource/* | |
| */ | |
| @Path("/resource") | |
| public class ResourceService | |
| { | |
| @PersistenceContext(unitName = "DSName") | |
| private EntityManager em; | |
| @GET | |
| @Path("/{id}") | |
| @Produces(MediaType.APPLICATION_JSON) | |
| public Response getPerson(@PathParam("id") int id) | |
| { | |
| return Response.ok().entity(em.find(Resource.class, id)).build(); | |
| } | |
| @POST | |
| @Path("/") | |
| @Consumes(MediaType.APPLICATION_JSON) | |
| @Produces(MediaType.APPLICATION_JSON) | |
| public Response createPerson(Resource resource) | |
| { | |
| try | |
| { | |
| return Response.ok().entity(em.persist(resource)).build(); | |
| } | |
| catch (IllegalAccessException e) | |
| { | |
| return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); | |
| } | |
| } | |
| @PUT | |
| @Path("/{id}") | |
| @Consumes(MediaType.APPLICATION_JSON) | |
| @Produces(MediaType.APPLICATION_JSON) | |
| public Response updatePerson(@PathParam("id") int id, Reource resource) | |
| { | |
| // Retrieve the original entity first. | |
| Resource original = em.find(Resource.class, id); | |
| if (original == null) | |
| { | |
| return Response.status(Response.Status.NOT_FOUND).build(); | |
| } | |
| try | |
| { | |
| return Response.ok().entity(em.merge(resource)).build(); | |
| } | |
| catch (IllegalAccessException e) | |
| { | |
| e.printStackTrace(); | |
| return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.ws.rs.ApplicationPath; | |
| import javax.ws.rs.core.Application; | |
| import java.util.Arrays; | |
| import java.util.HashSet; | |
| import java.util.Set; | |
| /** | |
| * JAXRS configuration class. | |
| */ | |
| @ApplicationPath("/resources") | |
| public class RestConfig extends Application | |
| { | |
| public Set<Class<?>> getClasses() | |
| { | |
| return new HashSet<Class<?>>(Arrays.asList(ResourceService.class)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment