Reasons My Code Is Crying

.java, .js, .* - by Pieter Grobler

Creating an Application With Wildfly (Java EE REST) and backbone.js - Part 1

In these dark days, we find ourselves listlessly longing for order. Therefore, let us create an application that brings us a beautiful to-do list. For this exciting prospect, I plan to use the following stack:

  • Windows 8.1;
  • Eclipse;
  • Brackets;
  • Java EE, REST;
  • Maven;
  • Backbone.js;
  • JQuery;
  • Bootstrap;
  • RequireJS;
  • Sass;
  • Grunt; and
  • Git

Lest we swallow the elephant whole, I’d rather we broke this down into a series of posts. That way, we maintain sanity across the board. In this post I will set up the back-end skeletal framework of the project. If we are able to see Hello World! on a local running instance of Wildfly, that would signal success, and we then ought to smugly look back and reminisce over our journey. And then move on.

Now, cunningly, when I open Eclipse, we immediately notice that I have the M2Eclipse (Maven) plugin pre-installed. This would be the case for you too if you chose the Eclipse edition for Java EE developers. However, if you do find your Eclipse instance is without the joy that Maven brings, you would simply need to add http://download.eclipse.org/technology/m2e/releases to your software update sites and install the software. Information on how to do this is available here.

I start by following File -> New -> Maven Project, wanting to create a new project from a Maven archetype. To enable this, I need to have added the appropriate catalog for Wildfly archetypes:

http://repository.jboss.org/nexus/content/groups/public-jboss/

Adding the Jboss/Wildfly catalog

Once this is in place, I proceed to create the project, selecting the latest version available, opting for the whole deal, an EAR project:

Selecting an archetype

I swiftly complete the archetype parameters, self-righteously using my own name, like so:

Archetype parameters

The project is then nicely created in the workspace as 4 separate projects (todo, todo-ear, todo-ejb and todo-web).

For our test, I create a class called Test.java inside the todo-web project in the com.pietergrobler.todo.rest package:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

package com.pietergrobler.todo.rest;

import javax.enterprise.context.RequestScoped;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/test")
@RequestScoped
public class Test
{

  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayHello()
  {
      return "Hello World!";
  }

}

Then we go on to install Wildfly, starting by downloading the latest version as a zip file and extracting it to C:\wildfly.

Next we update the environmental variables, setting:

  1. JAVA_HOME to the Java root installation folder. I’m using a JDK C:\Program Files\Java\jdk1.7.0_25
  2. JBOSS_HOME to C:\wildfly
  3. Edit PATH to include wildfly: .....;%JBOSS_HOME%\bin

Next we open a terminal (cmd) and add an admin user for the server:

add-user

Follow the instructions to add your management user, making a note of the username and password you select.

Now we give the server a kick, by simply typing:

standalone

in the terminal.

If you have Maven installed on your machine, you would now open another terminal and change the directory to the root of our todo project, and type:

mvn clean install wildfly:deploy

You could perform the same operation from within Eclipse by specifying a Run Configuration based on a Maven Build:

Maven Wildfly Run Configuration

Now, we eagerly open the browser, point it to localhost:8080/todo-web/rest/test, and we find our very Holy Grail:

Hello World!

My next post will deal with creating the actual REST services for our todo application. Thank you for reading!

Comments