It goes to show you that CDI and JAX-RS are simple enough in combination to use in a live high stress demo environment. Imagine what it could do for you in your code. This code was written using NetBeans 7 IDE which may be responsible for the simplicity as much as the other technologies.
Here is the source code: cdi-example.zip
The code below demonstrates some interesting bits. The source code above contains the less interesting POJOs.
PersonResource.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | package com.bluelotussoftware.example; import java.util.List; import javax.annotation.ManagedBean; import javax.inject.Inject; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; /** * REST resource for {@link com.bluelotussoftware.example.Person} objects. * * @author John Yeary * @version 1.0 */ @Path( "person" ) @ManagedBean public class PersonResource { @Inject private PersonDB pdb; @GET @Produces({MediaType.APPLICATION_JSON}) public List<person> getPersons() { return pdb.getPersons(); } @GET @Path( "{index}" ) @Produces({MediaType.APPLICATION_JSON}) public Person getPerson(@PathParam( "index" ) int index) { try { return pdb.getPersons().get(index); } catch (IndexOutOfBoundsException e) { throw new WebApplicationException(e, Response.Status.NOT_FOUND); } } @GET @Path( "{index}/phones" ) @Produces({MediaType.APPLICATION_JSON}) public List<phone> getPhones(@PathParam( "index" ) int index) { return pdb.getPersons().get(index).getPhoneNumbers(); } @GET @Path( "{index}/phones/{location}" ) @Produces({MediaType.APPLICATION_JSON}) public Phone getPhone(@PathParam( "index" ) int index, @PathParam( "location" ) String location) { try { List<phone> phones = pdb.getPersons().get(index).getPhoneNumbers(); for (Phone p : phones) { if (location != null && location.equals(p.getLocation())) { return p; } } return new Phone(); } catch (IndexOutOfBoundsException e) { throw new WebApplicationException(e, Response.Status.NOT_FOUND); } } } |
PersonDB.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | package com.bluelotussoftware.example; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import javax.annotation.PostConstruct; import javax.inject.Singleton; /** * A {@code @Singleton} database of {@link com.bluelotussoftware.example.Person} * objects. * * @author John Yeary * @version 1.0 */ @Singleton public class PersonDB implements Serializable { private static final long serialVersionUID = 2500854255025449263L; private List<person> persons; public PersonDB() { persons = new ArrayList<person>(); } @PostConstruct private void init() { List<phone> phones = new ArrayList<phone>(); phones.add( new Phone( "Home" , "111-222-3333" )); phones.add( new Phone( "Mobile" , "999-111-2222" )); phones.add( new Phone( "Work" , "888-777-6666" )); persons.add( new Person( "John" , new Date(), phones)); persons.add( new Person( "Sean" , new Date(), phones)); persons.add( new Person( "Ethan" , new Date(), phones)); } public List<person> getPersons() { return Collections.unmodifiableList(persons); } public boolean addPerson(Person p) { return persons.add(p); } public boolean removePerson(Person p) { return persons.remove(p); } } |
0 comments :
Post a Comment