Use HTTP PUT to create new cache (ehCache) running on the same Tomcat?

Posted by socal_javaguy on Stack Overflow See other posts from Stack Overflow or by socal_javaguy
Published on 2012-12-05T22:02:00Z Indexed on 2012/12/05 23:04 UTC
Read the original article Hit count: 163

Filed under:
|
|
|
|

I am trying to send a HTTP PUT (in order to create a new cache and populate it with my generated JSON) to ehCache using my webservice which is on the same local tomcat instance.

Am new to RESTful Web Services and am using JDK 1.6, Tomcat 7, ehCache, and JSON.

I have my POJOs defined like this:

Person POJO:

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Person {
    private String firstName;
    private String lastName;
    private List<House> houses;

    // Getters & Setters
}

House POJO:

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class House {
    private String address;
    private String city;
    private String state;

    // Getters & Setters
}

Using a PersonUtil class, I hardcoded the POJOs as follows:

public class PersonUtil {
    public static Person getPerson() {
        Person person = new Person();
        person.setFirstName("John");
        person.setLastName("Doe");
        List<House> houses = new ArrayList<House>();
        House house = new House();
        house.setAddress("1234 Elm Street");
        house.setCity("Anytown");
        house.setState("Maine");
        houses.add(house);
        person.setHouses(houses);
        return person;
    }
}

Am able to create a JSON response per a GET request:

@Path("")
public class MyWebService{

     @GET
     @Produces(MediaType.APPLICATION_JSON) 
     public Person getPerson() {
         return PersonUtil.getPerson();
     }
}

When deploying the war to tomcat and pointing the browser to

http://localhost:8080/personservice/

Generated JSON:

{ 
    "firstName" : "John", 
    "lastName" : "Doe",
    "houses": 
        [ 
            { 
              "address" : "1234 Elmstreet",
              "city"    : "Anytown",
              "state"   : "Maine"
            }
        ]
}

So far, so good, however, I have a different app which is running on the same tomcat instance (and has support for REST):

http://localhost:8080/ehcache/rest/

While tomcat is running, I can issue a PUT like this:

echo "Hello World" |  curl -S -T -  http://localhost:8080/ehcache/rest/hello/1

When I "GET" it like this:

 curl http://localhost:8080/ehcache/rest/hello/1

Will yield:

Hello World

What I need to do is create a POST which will put my entire Person generated JSON and create a new cache:

  http://localhost:8080/ehcache/rest/person

And when I do a "GET" on this previous URL, it should look like this:

{ 
    "firstName" : "John", 
    "lastName" : "Doe",
    "houses": 
        [ 
            { 
              "address" : "1234 Elmstreet",
              "city"    : "Anytown",
              "state"   : "Maine"
            }
        ]
}

So, far, this is what my PUT looks like:

@PUT
@Path("/ehcache/rest/person")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON) 
public Response createCache() {
    ResponseBuilder response = Response.ok(PersonUtil.getPerson(), MediaType.APPLICATION_JSON);
    return response.build();
}

Question(s):

(1) Is this the correct way to write the PUT?

(2) What should I write inside the createCache() method to have it PUT my generated JSON into:

http://localhost:8080/ehcache/rest/person

(3) What would the command line CURL comment look like to use the PUT?

Thanks for taking the time to read this...

© Stack Overflow or respective owner

Related posts about java

Related posts about JSON