Java Magazine, Jan/Feb 2016
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2016 17 web apps @ Entity public class Reservation @ Id @ GeneratedValue private Long id private String reservationName public Reservation String reservationName this reservationName reservationName Reservation @ Override public String toString return Reservation id id reservationName reservationName public Long getId return id public String getReservationName return reservationName The Reservation entity is trivial its just meant to demonstrate that we can use the full power of JPA Well use Spring Data JPA one module of the larger Spring Data project which features similar support for other persistence technologies such as Oracle Database MongoDB Neo4j Cassandra and others It enables us to declaratively define a repository object based on interface method name conventions such as this interface ReservationRepository extends JpaRepository Reservation Long Collection Reservation findByReservationName String rn With this in place weve also defined an @ Bean provider method in the ReservationServiceApplication class Because its annotated with @ SpringBootApplication the ReservationServiceApplication class is also an @ Configuration class where @ Bean provider methods may live Any @ Configuration class may use defined beans as return values from provider methods This is an alternative to annotating the component itself with stereotype annotations like @ Component @ RestController and so on The returned object of type CommandLineRunner will be run when the Spring Boot application starts up Therefore this is an ideal place to insert some sample data for this demonstration @ Bean CommandLineRunner runner ReservationRepository rr return args Stream of Julia Mia Phil Dave Pieter Bridget Stéphane Josh Jennifer forEach n rr save new Reservation n The Web We can then create a REST API using Spring MVC as well as the Jersey JAX RS implementation or Ratpack for which there are checkboxes in Spring Initializr Heres our code @ RestController class ReservationRestController
You must have JavaScript enabled to view digital editions.