Here are a few notes on how to run a simple java REST service and return JSON data as a response

  • REST service to do provide data on products
  • REST with jax-rs using Jersey

We start creating a StoreContent class representing the products in an online store.

public class StoreContent{
  // represent data
  DataAccess data = getDataAccessObject();

  @Path("/phone/android/{modelId}")
  public Object getPhoneAndroid(@PathParam("id") int id){
    return data.find("Phone.Android").select(id);
  }
}

Then, we add a container for our contents

/** Rest Container */
@Path("/")
public class RestStore{

  private StoreContent contents = new StoreContent();

  /** Return object representing the content */
  @Path("/store/")
  public Object getContent(){
    return this.contents;
  }

}

Here is how we return JSON content using the javax.json.JsonObjectBuilder

JsonObjectBuilder builder = Json.createObjectBuilder()

Next

Set up basic auth or https connection.

Categories: programming

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *