Introduction to JavaEE
This is the first in a series of blog posts meant to introduce JavaEE by building a “To Do” REST back-end service. In this post we’ll get everything started and ready for future posts.
The first thing that needs to be done is to download and install a JavaEE server such as Wildfly. The nice thing about Wildfly is that it’s both popular and free.
Once Wildfly is installed, start it by invoking:
WILDFLY_HOME/bin/standalone.bat
Now, open a browser to http://localhost:8080. You should see something like this:
Now we’ll need to start up an IDE and create a new project. This will be a Maven project, so make sure you have support for that in your IDE or have it installed locally.
Create a file called “MyApplication.java” in whichever package you want and add this to it:
import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("/") public class MyApplication extends Application { }
Think of this file as a “global configuration” for JAX-RS, which is what provides the ability to use REST within our application. The @ApplicationPath annotation is important as it tells the JavaEE server where all of our REST endpoints will be located. In this example, we’re keeping at the root of the application context.
The next file that needs to be created is where our endpoints will live. Create a file called “TodoService.java” in whichever package you want and add the following code to it:
import javax.ws.rs.GET; import javax.ws.rs.Path; @Path("/") public class TodoService { @GET public String get() { return "Hello Jimmy!"; } }
The @Path annotation tells JAX-RS which folder under the @ApplicationPath that this service will be accessible from. Since it’s also set to the root, it will be accessible at the root of the application context.
Currently, we’re only defining one endpoint: “get” and we do this by using the @GET annotation. It’s important to note that when you open a browser to a URL, the browser is actually issuing a “GET” call. This means that we will be able to test that endpoint using our browser.
The last file that needs to be created is the “pom.xml” file which is a Maven configuration file for the project. Create it if you don’t already have one then paste the following:
<?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> <groupId>com.synaptik.javaee</groupId> <artifactId>todo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <build> <finalName>todo</finalName> <plugins> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <webResources> <resource> <directory>${project.basedir}/src/main/resources</directory> <targetPath>WEB-INF/classes</targetPath> <filtering>true</filtering> </resource> </webResources> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> </dependencies> </project>
We’re not going to cover too much of this file other than to note that it’s including JavaEE 7 as a project dependency, it will compile our application into a “war” file, and the file output will be “todo.war”. This file name is important as it will be the name of our application context. The application context is how we will access our application on the JavaEE server.
If you don’t already have a PROJECT_ROOT/src/main/resources folder, create one now.
Now that we have a “pom.xml” file, we can build it. If you aren’t sure how to do this using your IDE, run the following command at the base of your project:
mvn clean package
This will compile everything, build our todo.war file, and put it in the target/ folder.
Once everything is built, go ahead and copy that todo.war file out to WILDFLY_HOME/standalone/deployments/
Wildfly should automatically see the file and deploy it for you. It should only take a few seconds for it to finish, but it depends on your computer.
After Wildfly has finished deploying, you can open http://localhost:8080/todo and you should see the following:
In the next post, we’ll define the API that we’ll be building.
Source code is available here:
1 Response
[…] series is meant as a companion to the JavaEE series where we built a back-end service. In this series, we’ll build an AngularJS front-end that […]