Porting Oracle’s “First Cup” Tutorial to Wildfly / JBoss

  JBoss, Tutorial, Wildfly

Creating the REST Resource

Now we will create the first submodule called ‘dukes-age’. This time, select File -> New -> Other -> Maven -> Maven Module. According to the Oracle tutorial chapter 3.2.2, this module will contain the REST resource.

New Maven Module

On the next screen, select the ‘Default Local’ catalog and then choose the previously created maven archetype dukes-age-archetype before clicking on ‘Next’:

New Maven Module dukes age

In the next dialog, make sure you enter the same groupId and version that you entered in your parent project. Ignore the package entry because it was set by the archetype which we used.

New Maven Module dukes age 2

Now, after you clicked on “Finish”, the project will be created according to the archetype template which, you might remember, we’ve taken from the original Oracle tutorial. For that reason, after the IDE finished creating it we’ll see that the project contains errors. Nothing to call home about. See all changes to the pom.xml which you have to make:

The archetype set it to ‘org.glassfish.javaeetutorial.firstcup’ but it has to be the groupId you chose from the beginning (see your parent’s project pom.xml).

You should also delete the version element because our parent already set it.

You can ignore the warning ‘GroupId is duplicate of parent groupId’.

Now save it and check the ‘Problems’ tab in you IDE. It complains about the project not being in sync with the pom.xml. RIght-click on the ‘dukes-age’ project in Project Explorer and select Maven -> Update Project. After completion, you’ll see even more errors in the Problems tab.

The new problems can be solved by adding a missing dependency into the projects pom.xml. In summary, ‘dukes-age’ pom.xml should look like this::

<?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/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <parent>
      <artifactId>firstcup</artifactId>
      <groupId>com.neuromechanics</groupId>
      <version>7.0.5</version>
   </parent>

   <groupId>com.neuromechanics</groupId>
   <artifactId>dukes-age</artifactId>
   <packaging>war</packaging>
   <name>dukes-age</name>
   <dependencies>
      <dependency>
         <groupId>org.jboss.spec.javax.ws.rs</groupId>
         <artifactId>jboss-jaxrs-api_2.0_spec</artifactId>
         <version>1.0.0.Final</version>
      </dependency>
   </dependencies>
</project>

The only problem left is in the DukesAgeResource.java file. Jump to Oracle’s tutorial and check how you are supposed to complete the code.

When there is no error any more, try running the ‘dukes-age’ project by right-clicking on it in the Project Explorer window and select “Run on server”. Since you already created a Wildfly server instance in the first chapter of this tutorial, you can safely select the Wildfly instance that’s being offered now. A web window will be opened in you IDE with a predefined URL which is just dead wrong. In fact, it must be http://localhost:8080/dukes-age/webapi/dukesAge. Why is that?

First, if you check the properties of the dukes-age project (right-click on the project’s root node in Project Explorer and select ‘Properties’), the context root of your app is defined in the ‘Web project settings’:

Web Project Settings

To find out the correct URL under which we will reach our REST service, we start with our host/domain/port values and append the value of the context root from the dialog above. As a result, we have http://localhost:8080/dukes-age (8080 is Wildfly’s default port on Windows).

The next part of the correct URL to trigger our REST resource can be found in the web.xml deployment descriptor. You can find it at /dukes-age/src/main/webapp/WEB-INF/web.xml. There the url-mapping for REST resources is set to <url-pattern>/webapi/*</url-pattern>.

Appending this to our incomplete URL makes <code>http://localhost:8080/dukes-age/webapi</code>.

The only thing missing is the path our REST resource watches out for. Open the DukesAgeResource.java and look out for the  @Path("dukesAge"). This is the missing part. Appending it to our still incomplete URL results in http://localhost:8080/dukes-age/webapi/dukesAge.

Yeah, this is the URL that should have appeared in the IDE’s browser window in the first place. If you copy and paste it into the address bar, hit enter and you should see ’20’ as the resource’s response. If not, see following checklist:

  1. In the IDE’s ‘Servers’ tab, check if it lists a Wildfly instance and its status is “[Started, Synchronized]”. If it’s not started, try starting it manually by right-click on the Wildfly instance and select ‘Start’. If its not starting, the console tab should display debug information which you can use to determin possible reasons for your problem.
  2. If the server has started, you should at least get a response from the Wildfly server instance root page which you get when calling for http://localhost:8080/
  3. . If you entered this URL in the address bar and still don’t get a Wildfly response, your port is probably different from the default 8080, or the port was already in use and you can find a corresponding error message in the console log during Wildfly startup.
  4. If Wildfly responded to ‘localhost:8080’ but not to our REST resource URL, then check for typos.

Important: The ‘dukes-age’ project contains the JSF backed bean for our UI called DukesBDay.java. Make sure you corrected the URL in the getAge() method of this class.

LEAVE A COMMENT