Professional Documents
Culture Documents
RestEasy, Tomcat, Maven, IntelliJ IDEA - A Getting Started Guide PDF
RestEasy, Tomcat, Maven, IntelliJ IDEA - A Getting Started Guide PDF
I spent some time searching for a recently written getting started guide
or some tutorials for working with RestEasy and how to setup my
IntelliJ IDE for it. Unfortunately, I wasn’t able to nd anything recent
enough for my liking so I gured I’d have a go at writing one myself!
I’m also pretty new at writing guides like this, so any feedback would be
greatly appreciated!
You can probably replicate a lot of the steps I’m doing with any other
IDE, but I’m using IntelliJ.
0. Assumptions
I am assuming that you already have Apache Tomcat installed as well as
some IDE of your choosing.
1. Setup
First, create a new basic Java project in your IDE, for IntelliJ, hit “Create
New Project”. On the left hand side, just select “Java” and hit “Next”.
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 1/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
You will then be prompted to provide a name and path to your project:
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 2/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 3/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
Right click the bar on the left hand side, the project explorer window.
Hit “Add Framework Support”.
Select ‘Maven’
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 4/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
• RestEasy
• RestEasy Client
• JUnit
<repositories>
<repository>
<id>JBoss repository</id>
<url>https://repository.jboss.org/nexus/content/groups/publi
c-jboss/</url>
</repository>
</repositories>
<dependencies>
<!--resteasy-->
<dependency>
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 5/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.1.4.Final</version>
</dependency>
<!--rest-easy client-->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.1.4.Final</version>
</dependency>
<!--unit test-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Now let’s specify in our pom.xml that we are writing a web application:
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 6/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
<packaging>war</packaging>
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.nicolaifsf</groupId>
<artifactId>RESTHello</artifactId>
<version>1.0-SNAPSHOT</version>
<repositories>
<repository>
<id>JBoss repository</id>
<url>https://repository.jboss.org/nexus/content/groups/publi
c-jboss/</url>
</repository>
</repositories>
<dependencies>
<!--resteasy-->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.1.4.Final</version>
</dependency>
<!--rest-easy client-->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.1.4.Final</version>
</dependency>
<!--unit test-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 7/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
<packaging>war</packaging>
</project>
Then, we now want to create a new le inside of this directory with the
name “web.xml”.
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 8/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 9/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-
name>
<!--Prefix for the endpoints-->
<param-value>/</param-value>
</context-param>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatc
her
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>{{ groupId
}}.app.HelloApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<!--Prefix for endpoint-->
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 10/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-
name>
<!--Prefix for the endpoints-->
<param-value>/</param-value>
</context-param>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatc
her
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-
value>com.nicolaifsf.app.HelloApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<!--Prefix for endpoint-->
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Here we have speci ed the some of the con gurations of our web
application. We speci ed that this application will be at “<host
addr>/”, or the root.
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 11/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 12/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
@Path("/")
public class HelloRestService {
@GET // This annotation indicates GET request
@Path("/hello")
public Response hello() {
return Response.status(200).entity("hello").build();
}
}
This speci es a base path “/” and if “/hello” follows, to then reply back
with a response with a 200 status code (OK), and “hello”.
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 13/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
“@GET” speci es that this is the code to execute if we get a GET request
We now have to create the actual application. Note how before in the
web.xml we speci ed com.nicolaifsf.app.HelloApplication . Let’s
add that class to our app package now!
import com.nicolaifsf.rest.HelloRestService;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 14/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
But rst…
First, select this dropdown on the top right hand side of the window:
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 15/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
From the new window that popped open, hit the “+” button on the top
left, and select “Tomcat Server” > “Local”.
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 16/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
Hit the “+” button on the bottom part, right below “Build” and select
“Build Artifacts”.
Next click the “Deployment” tab and hit the “+” under the “Deploy at
the server startup” section:
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 17/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
Hit “Artifact”, then in the window that pops up, select the artifact you
speci ed earlier and hit OK:
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 18/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
Finally:
You should now see on your browser the word “hello” on your screen.
It works!
5. Testing
Very important is also testing our code!
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 19/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
import com.nicolaifsf.rest.HelloRestService;
import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
import
org.jboss.resteasy.plugins.server.resourcefactory.POJOResour
ceFactory;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import javax.ws.rs.core.Response;
import java.net.URISyntaxException;
dispatcher.getRegistry().addResourceFactory(noDefaults);
}
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 20/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
Assert.assertEquals(Response.Status.OK.getStatusCode(),
response.getStatus());
Assert.assertEquals("hello",
response.getContentAsString());
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
The stu in Setup creates a Mock dispatcher that we can utilize to test
the HelloRestService class without having to run our server. With this,
we can run our tests much faster!
You can run the test on IntelliJ by hitting the “play” icons on the left!
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 21/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
Congrats, not only have you developed a RESTful service with RestEasy
at this point, but now you’ve also learned how to write a basic Unit Test
for it as well!
Conclusion
Thanks for taking the time to read my tutorial! Please let me know if
there are any mistakes I’ve made, or if there’s anything I can do to
improve. I’m always looking for feedback and am always looking to get
better :).
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 22/23
21-01-2019 RestEasy, Tomcat, Maven, IntelliJ IDEA: A Getting Started Guide
https://medium.com/@nicolaifsf78/intellij-idea-maven-resteasy-tomcat-f95bb41e6362 23/23