-
Create a simple Dynamic Web Project
-
Choose the module version as “3.1”
-
Add a new configuration for WildFly
-
Add
index.htmlin “WebContent” -
“Run as”, “Run on Server”
-
Change content on
index.htmland show LiveReload
-
-
Add a new Servlet
-
In Servers tab, select the module, right-click and select “Restart” to restart the module
-
-
Right-click on project, select “Properties”, search for “facet”, enable “JPA”, click on “Apply”, talk about “Further configuration available” and default data source
-
Create a new entity
Student -
Add a primary key in the wizard. Use
longdatatype andidname. -
Generate Getter/Setter by clicking “Source”, “Getters and Setters”.
-
Add
toStringimplementation -
The updated class should look like:
@Entity public class Student implements Serializable { @Id private long id; private static final long serialVersionUID = 1L; public Student() { super(); } public Student(long id) { this.id = id; } public long getId() { return this.id; } public void setId(long id) { this.id = id; } public String toString() { return "Student[" + id + "]"; } }
-
Add the following properties to
persistence.xml:<properties> <property name="javax.persistence.schema-generation.database.action" value="drop-and-create" /> <property name="javax.persistence.schema-generation.create-source" value="metadata" /> <property name="javax.persistence.schema-generation.drop-source" value="metadata" /> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> </properties> -
Show “server console” when the application is deployed. Show the generated SQL statements.
-
Download H2 console war to the server
standalone/deploymentsdirectorycurl -L https://github.com/jboss-developer/jboss-eap-quickstarts/blob/6.3.0.GA/h2-console/h2console.war?raw=true -o h2console.war -
Start the server and access http://localhost:8080/h2console
-
Enter the JDBC URL and credentials. To access the "test" database your application uses, enter these details (everything else is default):
-
JDBC URL:
jdbc:h2:mem:test;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1 -
User Name:
sa -
Password:
sa
-
-
Create a JAX-RS resource
StudentEndpoint-
Use
Studententity as the basis, selectcreate,findById,listAllmethods to be generated.
-
-
Add
@XmlRootElementinStudententity to enable XML <→ Java conversion. -
Add
@NamedQuery(name="findAllStudents", query="select s from Student s")toStudentto enable querying all students. -
Update the class so that it looks like as shown:
@RequestScoped @Path("/students") public class StudentEndpoint { @PersistenceContext EntityManager em; @Transactional @POST @Consumes({ "application/xml", "application/json", "text/plain" }) public void create(final long id) { Student student = new Student(id); em.persist(student); } @GET @Path("/{id:[0-9][0-9]*}") @Produces({ "application/xml", "application/json" }) public Response findById(@PathParam("id") final Long id) { Student student = em.find(Student.class, id); if (student == null) { return Response.status(Status.NOT_FOUND).build(); } return Response.ok(student).build(); } @GET @Produces("application/xml") public Student[] listAll( @QueryParam("start") final Integer startPosition, @QueryParam("max") final Integer maxResult) { TypedQuery<Student> query = em.createNamedQuery("findAllStudents", Student.class); final List<Student> students = query.getResultList(); return students.toArray(new Student[0]); } }
-
Use “Advanced REST Client” in Chrome
-
Make a GET request
-
Add Accept: application/xml
-
Show empty response
-
-
Make a POST request
-
Payload as
1 -
“Content-Type” header to
text/plain
-
-
Make a GET request to validate the data is posted
-
Change
createmethod to add Bean Validation constraintpublic void create(@Min(10) final long id) {
-
Make a POST request with payload as
1and show an error is being received
-
-
Edit
doGetof `TestServlet to match the code given belowServletOutputStream out = response.getOutputStream(); out.print("Retrieving results ..."); Client client = ClientBuilder.newClient(); Student[] result = client .target("http://localhost:8080/helloworld/rest/students") .request() .get(Student[].class); for (Student s : result) { out.print(s.toString()); }
-
Access the Servlet in the browser to show the results and explain JAX-RS Client API
-
Generate an interface
Greetingpublic interface Greeting { public String sayHello(); }
-
Create a new class
SimpleGreeting, implement the interface -
Inject the bean in Servlet as
@Inject Greeting greeting; -
Print the output as
response.getOutputStream().print(greeting.sayHello()); -
Show “New missing/unsatisfied dependencies” error and explain default injection
-
Add
@Dependenton bean -
Create a new class
FancyGreeting, implement the interface, add@Dependent -
Create a new qualifier using the wizard
-
Wizards: New CDI Web Project Wizard, CDI Wizards
-
Content assist: CDI Named Beans are available in JSF EL #{} content assist in XHTML/Java/XML files (See JSF)
-
Navigation (open the bean producer from the @Inject annotation for example): Java source navigation, from EL #{} to CDI bean (See JSF)
-
Open CDI Named bean: http://docs.jboss.org/tools/4.1.x.Final/en/cdi_tools_reference_guide/html_single/index.html#d0e597
-
Beans.xml editor: Content assist, Navigation, Validation http://docs.jboss.org/tools/whatsnew/cdi/cdi-news-3.2.0.Beta1.html
-
Search usage: Injection Points, EL #{} (See JSF)
-
EL content assist in XHTML: http://docs.jboss.org/tools/whatsnew/jst/jst-news-3.3.0.M3.html
-
Navigation from/to bean
-
Search usage
-
Refactoring: http://docs.jboss.org/tools/whatsnew/jst/jst-news-3.2.0.M1.html
-
New JSF project wizard (JSF 2.2 or older)
-
Composite component code assist: https://issues.jboss.org/browse/JBIDE-4970, http://docs.jboss.org/tools/whatsnew/jst/jst-news-3.2.0.Beta2.html, Validation and refactoring are also available
-
EL Validation: http://docs.jboss.org/tools/whatsnew/jst/jst-news-3.2.0.M2.html
-
Switch to JBoss perspective
-
Go to “Forge Console”, click on play button to start it
-
project-new --named sample -
javaee-setup --javaEEVersion 7 -
jpa-setup --jpaVersion 2.1 -
Install plugin:
addon-install-from-git --url https://github.com/forge/addon-batch-
Create new Job XML:
batch-new-jobxml --jobXML myJob.xml --reader org.svcc.MyReader --writer org.svcc.MyWriter
-