Java Magazine, Jan/Feb 2016
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2016 84 microservices @ Temporal TemporalType TIMESTAMP private Date duration @ ManyToMany mappedBy routes private List Booking bookings Omitted getters setters The booking file models Booking java contains this @ Entity @ NamedQuery name Booking findAll query SELECT b FROM Booking b public class Booking @ Id @ GeneratedValue strategy GenerationType IDENTITY private Integer id @ Temporal TemporalType TIMESTAMP private Date orderDate @ ManyToMany private List Route routes Omitted getters setters Implementing the Routes and Booking Services We are now ready to implement the routes and booking services We will need JAX RS for the REST interfaces We will use Jersey as implemented in KumuluzEE We will also add CDI because we want to use the EntityManager injection using @ PersistenceContext However we could just as well do without CDI to keep our microservice even lighter We add the following dependencies to booking pom xml dependency groupId com kumuluz ee groupId artifactId kumuluzee jax rs jersey artifactId version kumuluzee version version dependency dependency groupId com kumuluz ee groupId artifactId kumuluzee cdi weld artifactId version kumuluzee version version dependency Now lets implement the booking REST service Well create a booking resource that contains two methods createBooking Booking and getBooking Make sure you add the correct dependencies as described earlier Also make sure you add the models module to every microservice for access to JPA entities whose REST service will be used The code is @ ApplicationPath public class BookingApplication extends javax ws rs core Application Then in BookingsResource java we have this @ Path bookings @ Produces MediaType APPLICATION_ JSON @ Consumes MediaType APPLICATION_ JSON @ RequestScoped public class BookingsResource @ PersistenceContext unitName trains private EntityManager em @ GET public Response getBookings List Booking bookings em createNamedQuery Booking findAll Booking class
You must have JavaScript enabled to view digital editions.