#
Create a REST Service (using Jersey)
This tutorial explains to you how to create a simple Java Web Service (JAX-RS) using Jersey implementation on Tomcat. This example is a simple one, just to make you understand the concept of JAX-RS. This example is producing an XML file for data exchange.
Here are some prerequisites for this article:
- You have installed the Apache Tomcat server (v.9)
- You have installed Java 8 on your machine
- Your environment is Windows
- You are using Eclipse for creating the JAX-RS
Info
This JAX-RS RESTful (using Jersey) example will produce XML for information exchange with the client. You can take a look at the article named "Create a JAX-RS WS using Jersey (producing JSON)" as well.
In order to create a RESTful Web Service (JAX-RS) using Jersey you have to create a Dynamic Web Project.
In Eclipse => File => New => Dynamic Web Project
You will see the following screen:
I will name this project "JAX-RS-Jersey-example". Click on "Next".
Click on "Next".
Don't forget to mark "Generate web.xml deployment descriptor". Click on "Finish" and the project will be created for your JAX-RX Web Service.
Now you can convert your Project into a Maven Project.
For this you have to click on the Project name, right click after that (take a look at the picture above) choose Configure and "Convert to Maven Project".
The following screen will appear:
Click on "Finish". Open the pom.xml file and add the dependencies below:
These dependencies are for a JAX-RS Web Service using Jersey implementation.
Now add the following servlet and servlet mapping into the web.xml file.
Info
A Servlet
is a Java solution to handle web requests.
Now create a class for the JAX-RS.
Right click on the Project name, choose "New", choose Class.
Choose the name of the class and the package name and click on "Finish". Code the Java class. Here it is an example:
Now you can build the project using Maven:
The goal must be "clean install" to perform a clean build.
Click on "Run".
Click on "Finish".
And now it's time for testing my JAX-RS example (using Jersey).
A JAX-RS (RESTful) Web Service could be tested using a browser.
Here you can see the result of my JAX-RS (RESTful) example without parameters:
Here you can see the result of my JAX-RS (RESTful) example with parameters:
Info
2 and 10 are the parameters.