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

4
.idea/compiler.xml generated
View File

@@ -9,16 +9,18 @@
<outputRelativeToContentRoot value="true" /> <outputRelativeToContentRoot value="true" />
<module name="cdidemo" /> <module name="cdidemo" />
<module name="jpademo" /> <module name="jpademo" />
<module name="xmlandjsondemo" />
<module name="beanvalidationdemo" /> <module name="beanvalidationdemo" />
<module name="jmsdemo" /> <module name="jmsdemo" />
<module name="jaxwsdemo-client" /> <module name="jaxwsdemo-client" />
<module name="xmlandjsondemo" />
<module name="jaxwsdemoservice" /> <module name="jaxwsdemoservice" />
<module name="jaxwsdemo-service" /> <module name="jaxwsdemo-service" />
<module name="ejbdemo" /> <module name="ejbdemo" />
<module name="jaxrsdemo" />
</profile> </profile>
</annotationProcessing> </annotationProcessing>
<bytecodeTargetLevel> <bytecodeTargetLevel>
<module name="jaxwsdemo-client" target="1.7" />
<module name="maven-archiver (1)" target="1.5" /> <module name="maven-archiver (1)" target="1.5" />
<module name="maven-archiver (2)" target="1.5" /> <module name="maven-archiver (2)" target="1.5" />
<module name="maven-archiver (3)" target="1.5" /> <module name="maven-archiver (3)" target="1.5" />

1
.idea/encodings.xml generated
View File

@@ -4,6 +4,7 @@
<file url="file://$PROJECT_DIR$/beanvalidationdemo" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/beanvalidationdemo" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/cdidemo" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/cdidemo" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/ejbdemo" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/ejbdemo" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/jaxrsdemo" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/jaxwsdemo-client" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/jaxwsdemo-client" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/jaxwsdemo-service" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/jaxwsdemo-service" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/jmsdemo" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/jmsdemo" charset="UTF-8" />

1
.idea/misc.xml generated
View File

@@ -2,6 +2,7 @@
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="FrameworkDetectionExcludesConfiguration"> <component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$/jaxrsdemo" />
<file type="web" url="file://$PROJECT_DIR$/jaxwsdemo-service" /> <file type="web" url="file://$PROJECT_DIR$/jaxwsdemo-service" />
</component> </component>
<component name="GOROOT" path="/usr/lib/go" /> <component name="GOROOT" path="/usr/lib/go" />

16
jaxrsdemo/jaxrsdemo.iml Normal file
View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="FacetManager">
<facet type="jpa" name="JPA">
<configuration>
<setting name="validation-enabled" value="true" />
<setting name="provider-name" value="EclipseLink" />
<datasource-mapping>
<factory-entry name="NewPersistenceUnit" />
</datasource-mapping>
<naming-strategy-map />
<deploymentDescriptor name="persistence.xml" url="file://$MODULE_DIR$/src/main/resources/META-INF/persistence.xml" />
</configuration>
</facet>
</component>
</module>

85
jaxrsdemo/pom.xml Normal file
View File

@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>jaxrsdemo</artifactId>
<packaging>war</packaging>
<name>jaxrsdemo</name>
<parent>
<artifactId>jeedemo</artifactId>
<groupId>it.plague.jeedemo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.5.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12.4</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

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>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/target/generated-sources/wsimport" isTestSource="false" generated="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.glassfish.main.extras:glassfish-embedded-all:4.1.2" level="project" />
<orderEntry type="module-library">
<library name="Maven: com.sun:tools-jar:1">
<CLASSES>
<root url="jar://C:/Program Files/Java/jdk1.8.0_201/lib/tools.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
</component>
</module>

View File

@@ -16,6 +16,7 @@
<module>xmlandjsondemo</module> <module>xmlandjsondemo</module>
<module>jmsdemo</module> <module>jmsdemo</module>
<module>jaxwsdemo-service</module> <module>jaxwsdemo-service</module>
<module>jaxrsdemo</module>
</modules> </modules>
<dependencyManagement> <dependencyManagement>