Over a million developers have joined DZone.

Build a REST service with Netbeans 7, Java, and Jersey that returns JSON

DZone's Guide to

Build a REST service with Netbeans 7, Java, and Jersey that returns JSON

· Java Zone ·
Free Resource

Delivering modern software? Atomist automates your software delivery experience.

One of the great things about Jersey is that it makes working with REST really easy. If your new to Jersey and REST then you can check out my previous article that shows you to easily build a simple REST service. Today I’m going to use Netbeans 7 (with its built-in Maven support) to build another simple REST service that can easily return JSON (with the help of JAXB).

With Netbeans 7 create a new Maven Web Application called: RestJson

You can remove the default index.jsp file that gets created. Right-click on the Dependencies and add jersey-server-linking as well as jersey-json.

Here is my complete POM file:

<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">

The next thing to do is to create a class that extends PackagesResourceConfig which in a nutshell will make it so we won’t need a web.xml for this particular project.

Create a new class called RestJsonApplication with a namespace like: com.giantflyingsaucer.restjson

Here are the contents for the RestJsonApplication.java file:

package com.giantflyingsaucer.restjson;
import com.sun.jersey.api.core.PackagesResourceConfig;
import javax.ws.rs.ApplicationPath;
public class RestJsonApplication extends PackagesResourceConfig {
    public RestJsonApplication() {

Now you will create a new class called Item with the namespace of: com.giantflyingsaucer.restjson.v1.resources

What is with the “v1″? Simply put I have that in there for versioning – this is version one of this API.

Here are the contents for the Item.java file:

package com.giantflyingsaucer.restjson.v1.resources;
import javax.xml.bind.annotation.XmlRootElement;
public class Item {
    private int id;
    private String name;
    private String description;
    public Item() {}
    public Item(int id, String name, String description) {
        this.id = id;
        this.name  = name;
        this.description = description;
    public String getDescription() {
        return description;
    public void setDescription(String description) {
        this.description = description;
    public int getId() {
        return id;
    public void setId(int id) {
        this.id = id;
    public String getName() {
        return name;
    public void setName(String name) {
        this.name = name;

As you can see this is a simple Java POJO with a sprinkle of JAXB. In the same package add another new class called: ItemResource

package com.giantflyingsaucer.restjson.v1.resources;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
public interface ItemResource {
    // Example: Returning more than one Item
    List<Item> getItems();

This is the interface to the service in which we define what it will do. In this case it will return item objects in JSON format.

Go ahead and create the new implementation class now called: ItemResourceImpl which implements the ItemResource interface. The namespace for the new class is: com.giantflyingsaucer.restjson.v1.resources.impl

Here is the code for the ItemResourceImpl.java file:

package com.giantflyingsaucer.restjson.v1.resources.impl;
import com.giantflyingsaucer.restjson.v1.resources.Item;
import com.giantflyingsaucer.restjson.v1.resources.ItemResource;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Path;
// Set the path, version 1 of API
public class ItemResourceImpl implements ItemResource{
    public List<Item> getItems() {
        List<Item> items = new ArrayList<Item>();
        items.add(new Item(100, "Widget", "A basic widget"));
        items.add(new Item(200, "SuperWidget", "A super widget"));
        items.add(new Item(300, "UberSuperWidget", "A uber super widget"));
        return items;

For simplicity I add a few items and then simply return them. The conversion to JSON is handled for me.

Using the Maven support in Netbeans 7 do a Clean and Build command (right-click on the project) and deploy the resulting WAR file to something like Tomcat 7 (I’m using Tomcat 7 but feel free to use what you want as long as it supports Servlet 3.0 since we are not using a web.xml file for this project). Once the WAR has been extracted and deployed by Tomcat go ahead and hit the URL and you should see JSON coming back.



{ "item" : [ { "description" : "A basic widget",
        "id" : "100",
        "name" : "Widget"
      { "description" : "A super widget",
        "id" : "200",
        "name" : "SuperWidget"
      { "description" : "A uber super widget",
        "id" : "300",
        "name" : "UberSuperWidget"
    ] }

You can get the full source code from here.


From http://www.giantflyingsaucer.com/blog/?p=3458

Start automating your delivery right there on your own laptop, today! Get the open source Atomist Software Delivery Machine.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}