javaee7-eclipse icon indicating copy to clipboard operation
javaee7-eclipse copied to clipboard

Java EE 7 using Eclipse

Java EE 7 using Eclipse

Project creation

  • Create a simple Dynamic Web Project ** Choose the module version as 3.1'' ** Add a new configuration for WildFly ** Add `index.html` in WebContent'' ** Run as'', Run on Server'' ** Change content on index.html and show http://docs.jboss.org/tools/whatsnew/livereload/livereload-news-1.0.0.Alpha2.html[LiveReload]

Servlet

  • Add a new Servlet ** In Servers tab, select the module, right-click and select ``Restart'' to restart the module ** Show http://localhost:8080/HelloJavaEE7/TestServlet

Persistence

  • Right-click on project, select Properties'', search for facet'', enable JPA'', click on Apply'' ** Talk about ``Further configuration available'' and default data source (no configuration required)
  • Right-click on project, select New'', JPA Entity'' and give the name as Student
  • Add a primary key in the wizard. Use long datatype and id name.
  • Generate Getter/Setter by clicking Source'', Getters and Setters''.
  • Add toString implementation
  • The updated class should look like:

[source, java]

@Entity @XmlRootElement @NamedQuery(name="findAllStudents", query="select s from Student s") 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 + "]";
}

}

** @XmlRootElement in Student entity enables XML <-> Java conversion. ** @NamedQuery(name="findAllStudents", query="select s from Student s") in Student enables querying all students.

  • Add the following properties to persistence.xml:

[source.xml]

---- + * Show ``server console'' when the application is deployed. Show the generated SQL statements.

Setup console to watch database


* Download
  https://github.com/jboss-developer/jboss-eap-quickstarts/blob/6.3.0.GA/h2-console/h2console.war?raw=true[H2 console war] to WildFly's `standalone/deployments` directory
+
[source,text]
----
curl -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`

RESTful Web Services
--------------------

* Create a JAX-RS resource `StudentEndpoint`
** `rest.RestApplication` is added and specifies the base URI for REST resoures as `/rest`.
** Use `Student` entity as the basis, select `create`, `findById`, `listAll` methods to be generated.
* Update the class so that it looks like as shown:
+
[source,java]
----
@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 to http://localhost:8080/HelloJavaEE7/rest/students
*** 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
* Edit `doGet` of `TestServlet` to match the code given below
+
[source,java]
----
ServletOutputStream out = response.getOutputStream();
out.print("Retrieving results ...");
Client client = ClientBuilder.newClient();
Student[] result = client
.target("http://localhost:8080/HelloJavaEE7/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


Bean Validation
---------------

* Change `create` method to add Bean Validation constraint
+
[source,java]
----
public void create(@Min(10) final long id) {
----
+
* Make a POST request with payload as `1` and show an error is being received


CDI
---

* Generate an interface `Greeting`
+
[source,java]
----
public interface Greeting {
	public String sayHello();
}
----
+
* Create a new class `SimpleGreeting`, implement the interface as:
+
[source, java]
----
public class SimpleGreeting implements Greeting {

	@Override
	public String sayHello() {
		return "Hello World";
	}

}
----
+
* 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 `@Dependent` on bean
* Create a new class `FancyGreeting`, implement the interface, add `@Dependent`
* Create a new qualifier using ``New'', ``Qualifier Annotation Type''

Advanced CDI
------------

* Wizards:
http://docs.jboss.org/tools/4.1.x.Final/en/cdi_tools_reference_guide/html/chap-CDI_Tools_Reference_Guide-Creating_a_CDI_Web_Project.html[New CDI Web Project Wizard],
http://docs.jboss.org/tools/4.1.x.Final/en/cdi_tools_reference_guide/html/chap-CDI_Tools_Reference_Guide-Wizards_and_Dialogs.html#d0e555[CDI Wizards]
* Content assist: CDI Named Beans are available in JSF EL #{} content assist in XHTML/Java/XML files (See JSF)
* Validation:
http://docs.jboss.org/tools/4.1.x.Final/en/cdi_tools_reference_guide/html/chap-CDI_Tools_Reference_Guide-Validation.html
* Navigation (open the bean producer from the @Inject annotation for example):
http://docs.jboss.org/tools/4.1.x.Final/en/cdi_tools_reference_guide/html/chap-CDI_Tools_Reference_Guide-Hyperlink_Navigation.html[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: https://issues.jboss.org/browse/JBIDE-8705[Injection Points], EL #{} (See JSF)
* CDI 1.2 support was introduced in JBoss Tools 4.3.0.Alpha1: http://tools.jboss.org/documentation/whatsnew/jbosstools/4.3.0.Alpha1.html#cdi
But it's mostly about showing CDI 1.2 as available in our wizards (New CDI Project wizard for example) + minor bug fixing. In JBoss Tools 4.2 (Eclipse Luna) you can use CDI 1.1 in wizards for CDI 1.2 projects since 1.2 is just a maintenance release and CDI Tools relays on actual CDI jars from the project's class path. 

Batch
-----

* Open https://github.com/javaee-samples/javaee7-samples/[Java EE 7 Samples project] and show Job XML
* http://tools.jboss.org/documentation/whatsnew/jbosstools/4.3.0.Alpha1.html#batch[Batch job XML editor] - available in JBoss Tools 4.3.0.Alpha1
* Upcoming 4.3.0.Alpha2 features: Validation, Content Assist, Navigation, - https://issues.jboss.org/browse/JBIDE-18857

JavaServer Faces
----------------

* 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

OpenShift
---------

* Create a new server adapter from ``OpenShift Explorer''
* More details at http://blog.arungupta.me/getting-started-wildfly-openshift-jboss-developer-studio/

Forge
-----

* 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`
* More details about Batch and Forge at: http://blog.arungupta.me/javaee7-batch-addon-jboss-forge-part1/
* More details about other technologies at: http://blog.arungupta.me/rapid-javaee-development-forge2/

Continuous Delivery
-------------------