Aggiunto l'esempio del capitolo su JAX-RS

This commit is contained in:
Fabio Scotto di Santolo
2019-04-04 19:42:05 +02:00
parent d47e2d0d02
commit f3682c9a5f
14 changed files with 387 additions and 1 deletions

View File

@@ -0,0 +1,23 @@
package it.plague.jeedemo;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("rs")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new HashSet<>();
addRestResources(resources);
return resources;
}
private void addRestResources(Set<Class<?>> resources) {
resources.add(it.plague.jeedemo.BookRestService.class);
resources.add(org.eclipse.persistence.jaxb.rs.MOXyJsonProvider.class);
}
}

View File

@@ -0,0 +1,43 @@
package it.plague.jeedemo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.xml.bind.annotation.XmlRootElement;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@XmlRootElement
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@NamedQuery(name = Book.FIND_ALL, query = "select b from Book b")
public class Book {
public static final String FIND_ALL = "FIND_ALL";
@Id
@GeneratedValue
private String id;
@Column(nullable = false)
private String title;
private Float price;
@Column(length = 2000)
private String description;
private String isbn;
private Integer nbOfPage;
private Boolean illustrations;
}

View File

@@ -0,0 +1,75 @@
package it.plague.jeedemo;
import java.net.URI;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
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.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
@Path("/book")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Stateless
public class BookRestService {
@PersistenceContext(name = "jaxrsPU")
private EntityManager em;
@Context
private UriInfo uriInfo;
@GET
public Response getBooks() {
TypedQuery<Book> query = em.createNamedQuery(Book.FIND_ALL, Book.class);
Books books = new Books(query.getResultList());
return Response.ok(books).build();
}
@GET
@Path("{id}")
public Response getBook(@PathParam("id") String id) {
Book book = em.find(Book.class, id);
if (book == null) {
throw new NotFoundException();
}
return Response.ok(book)
.build();
}
@POST
public Response createBook(Book book) {
if (book == null) {
throw new BadRequestException();
}
em.persist(book);
URI location = uriInfo.getAbsolutePathBuilder()
.path(book.getId())
.build();
return Response.created(location).build();
}
@DELETE
@Path("{id}")
public Response deleteBook(@PathParam("id") String id) {
Book book = em.find(Book.class, id);
if (book == null) {
throw new NotFoundException();
}
em.remove(book);
return Response.noContent().build();
}
}

View File

@@ -0,0 +1,29 @@
package it.plague.jeedemo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@XmlRootElement
@XmlSeeAlso(Book.class)
public class Books extends ArrayList<Book> {
public Books(Collection<? extends Book> c) {
super(c);
}
@XmlElement(name = "book")
public List<Book> getBooks() {
return this;
}
public void setBooks(List<Book> books) {
this.addAll(books);
}
}

View File

@@ -0,0 +1,59 @@
package it.plague.jeedemo;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.sql.DataSourceDefinition;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Startup
@Singleton
@DataSourceDefinition(
className = "org.apache.derby.jdbc.EmbeddedDataSource",
name = "java:global/jdbc/jaxrsoPU",
user = "APP",
password = "APP",
databaseName = "testdb",
properties = {"connectionAttributes=;create=true"})
public class DatabasePopulator {
@PersistenceContext(name = "jaxrsPU")
private EntityManager em;
private Book h2g2;
private Book lord;
@PostConstruct
private void populateDB() {
h2g2 = Book.builder()
.title("Beginning Java EE 7")
.price(35f)
.description("Great book")
.isbn("1-8763-9125-7")
.nbOfPage(605)
.illustrations(true)
.build();
lord = Book.builder()
.title("The Lord of the Rings")
.price(50.4f)
.description("SciFi")
.isbn("1-84023-742-2")
.nbOfPage(1216)
.illustrations(true)
.build();
em.persist(h2g2);
em.persist(lord);
}
@PreDestroy
private void clearDB() {
em.remove(h2g2);
em.remove(lord);
}
}

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="jaxrsoPU" transaction-type="JTA">
<jta-data-source>java:global/jdbc/jaxrsoPU</jta-data-source>
<properties>
<property name="eclipselink.target-database" value="DERBY"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="ALL"/>
</properties>
</persistence-unit>
</persistence>