Creating a RESTful service with Java and Jersey with Netbeans

Creating a RESTful service with Java and Jersey with Netbeans

Submitted by Mark Tsibulski on Fri, 11/28/2014 - 19:15
Step 6

There are many ways to implement a RESTful service, but what if you need to do it in Java and you use Netbeans? I recently had a project like this and it was quite an experience to get my enviorment up and working. With Java, there are just so many ways to do it that it starts to look like too much to handle. Well it is actually easier than you think. Let's dive in..

First, fire up Netbeans and create a new Maven Web Application project. Give it a name of you choosing.

NetBeans - New Project

Right off the bat, create a web.xml file by right clicking on the project and creating a new Standard Deployment Descriptor. This is the configuration file for the RESTful service mapping.

NetBeans - New File

And the last configuration step you will need to make before you actally write code is to add the Jersey Dependency. Add these lines to your pom.xml:


Now you can get to writing some code. To begin, create a Java class, name it Hello, and add this code:

public class Hello {
    public Response getMsg(@PathParam("param") String message) {
        String output = "Hello " + message + "!";
        return Response.status(200).entity(output).build();
This simple class will output a simple greeting when you visit the /hello/yourname path.

In order for this to work, you must make an addition to your web.xml to map the Java class to the RESTful path. Add this block of code to your web.xml:


Be sure to change the param-value to match your Java class package.

Now run this on your server and head to /api/hello/SpinSpire to see this:

server response

I use Jetty as a server because its simple to use, it recompiles/restarts on save, and its also easy to set up. Open your pom.xml and add this between your plugins tags:

      <!-- make jetty listen on for better security -->
      <!-- defaultsDescriptor>src/test/resources/jetty-maven-plugin-webdefault.xml</defaultsDescriptor -->

Now right click on your project and click properties. Find the Actions tab and then the Run Project action. Change it from "package" to "jetty:run". It should look like this:

NetBeans - project properties pane

Run the Jetty server simply by hitting the green play button in Netbeans.

NetBeans run button

Now let's say you have a database table called Event with an id, title, and created collumns and you want to pull that data from a REST HTTP GET call.

First open the Services tab in Netbeans, right click on Databases, and hit New Connection.

NetBeans - new database connection

Follow the wizard step by step to setup your database with Netbeans. Once thats done, you will need to let your project know which database to connect to. For Jetty, create a jetty-env.xml inside your WEB-INF folder and add these lines: 

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <New id="DSTix" class="">
      <New class="org.apache.commons.dbcp.BasicDataSource">
         <Set name="driverClassName">com.mysql.jdbc.Driver</Set>
         <Set name="url">jdbc:mysql://localhost:3306/rest</Set>
         <Set name="username">yourusername</Set>
         <Set name="password">yourpassword</Set>
         <Set name="validationQuery">SELECT 1</Set>

Of course you will have to set your own username and password, and also the url must match the one you created using the New Database Connection wizard.

Next you must add your database drivers to your project, and while we're at it, a JSON meda writer. Add this to your pom.xml:


Now keep in mind that i use a MySQL database, so my configuration and drivers are for MySQL.

That's it for the database configuration though , so now its time to create an Event model and Event Resource. Create a Java class named Event with the following code:

public class Event {
    private int id;
    private String title;
    private int created;
    public int getId() {
        return id;
    public void setId(int id) { = id;
    public String getTitle() {
        return title;
    public void setTitle(String title) {
        this.title = title;
    public int getCreated() {
        return created;
    public void setCreated(int created) {
        this.created = created;

and a EventResource class with the following code:

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class EventResource{
    protected Connection getConnection() throws SQLException, NamingException {
        InitialContext ic = new InitialContext();
        DataSource ds = (DataSource) ic.lookup("jdbc/DSTix");
        return ds.getConnection();
    public Event getFromResultSet(ResultSet rs) throws SQLException {
        Event event = new Event();
        return event;
    public List getList() throws SQLException, NamingException {
        List events = new ArrayList<>();
        Connection db = getConnection();
        try {
            PreparedStatement st = db.prepareStatement("SELECT id, title, created from tix_event");
            ResultSet rs = st.executeQuery();
            while ( {
                Event e = getFromResultSet(rs);
            return events;
        } finally {


I'll take a moment and explain what is going on in the code. Path is set to /event and the media that it will produce is JSON. Event.class is simply the model class, all it does it contain the fields and appropriate getters/setters. EventResource.class is the calss that does the work. Connection is self-explanatory, it just instantiates the connection. getList opens a new connection, then executes a query and populates a List with the returned data, which is rendered as JSON.

Working with Java can be confusing sometimes, but once you setup your enviornment, it becomes a breeze. With the code above, you now have a baseline to work from, and it becomes easy to implement CRUD operations into your project. Attached is a complete project with CRUD operations to a table, feel free to example and use it.

Project Download

More resources here:


© 2023 SpinSpire All rights reserved.