Jersey on Native Netbeans Project

Here is a quick way to setup a REST web service with Netbeans native project (without using maven or gradle).

The following instructions do not setup JPA, DAO etc. Goal is only to setup a jersey service.

1. Create a new web project.

Optionally configure a separate folder for the libraries when asked. Recommended is lib.

2. Add the dependencies

Add the following libraries: (right click at libraries->Add library->import)

  1. JAX-RS
  2. Jersey

3. Create a class for your model

The data you want to expose, eg

public class StateModel {
    private String name;
    private double lat;
    private double lng;

    public StateModel() {

    }
    ...
}

It is important that the model always includes:

  1. default constructor
  2. proper encapsulation (private properties and related getter/setters)

It is recommended that it is serializable and annotated properly if XML is also used.

4. Create the Jersey service (web resource)

Create the web resource.

The following example show to you how to expose a single item and a collection of items.

@Path("/states")
public class WebResource { 
    @GET
    @Path("/one")
    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    public Response getStates() {
        StateModel obj = new StateModel("bla", 30, 20);
        Response response = Response.status(Response.Status.OK).entity(obj).build();
        return response;
    }
 
    @GET
    @Path("/all")
    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    public Response getState() {
        StateModel obj1 = new StateModel("bla", 10, 20);
        StateModel obj2 = new StateModel("bloo", 30, 40);
 
        List<StateModel> list = new ArrayList<>();
        list.add(obj1);
        list.add(obj2);

        GenericEntity<List<StateModel>> genericList = new GenericEntity<List<StateModel>>(list) {};
 
        Response response = Response.status(Response.Status.OK).entity(genericList).build(); 
        return response;
    }
}

 

4. Create a class to load the web resource

Anywhere on your project (better in a separate package) create the following class:

@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationConfig extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<>();
        addRestResourceClasses(resources);
        return resources;
    }
    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(service.WebResource.class);
    }
}

Note that in the example above:

  1. service is the package name
  2. WebResource is the annotated class that defines the web service.

5. Compile.

At this point the resource should be available at:

http://localhost:XXXX/x/y/z

where:

  1. XXXX is the port of the web server eg 8084
  2. x is the application path defined in ApplicationConfig class
  3. y is the path defined in the class configuring the service
  4. z is the path defined in the method of the class

So in this example, the path is:

http://localhost:8084/RestExample/webresources/states/one

and

http://localhost:8084/RestExample/webresources/states/all

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s