Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
titleSample Code invoking a POST - add a user
linenumberstrue
            User us = new User("David", "");

            ClientConfig clientConfig = new DefaultClientConfig();

            clientConfig.getFeatures().put(
                    JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);

            Client client = Client.create(clientConfig);

            WebResource webResource = client
                    .resource("http://localhost:8084/dbwstier/dbrest/user/cu");

            ClientResponse response = webResource.accept("application/json")
                    .type("application/json").post(ClientResponse.class, us);

            if (response.getStatus() != 201)
            {
                throw new RuntimeException("Failed : HTTP error code : "
                        + response.getStatus());
            }

            String output = response.getEntity(String.class);

            System.out.println("Server response .... \n");
            System.out.println(output);

To update a record we are going to use HTTP PUT command

Code Block
languagejava
firstline26
titleSample Code invoking a PUT - update a user
linenumberstrue
            User oldUser = new User("David", "ppp");
            User newUser = new User("David", "PP-XX-YY");
            UserMap umap = new UserMap( oldUser, newUser );

            ClientConfig clientConfig = new DefaultClientConfig();

            clientConfig.getFeatures().put(
                    JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);

            Client client = Client.create(clientConfig);

            WebResource webResource = client
                    .resource("http://localhost:8084/dbwstier/dbrest/user/uu");

            ClientResponse response = webResource.accept("application/json")
                    .type("application/json").put(ClientResponse.class, umap);

            if (response.getStatus() != 200)
            {
                throw new RuntimeException("Failed : HTTP error code : "
                        + response.getStatus());
            }

            String output = response.getEntity(String.class);

            System.out.println("Server response .... \n");
            System.out.println(output);

Notice the new class UserMap

Code Block
languagejava
titleCode Sample - UserMap
public class UserMap
{
    public  User    oldUser;
    public  User    newUser;
    
    public  UserMap(){}
    
    public  UserMap( User oldU, User newU )
    {
        oldUser = oldU;
        newUser = newU;
    }
}

This will be a carrier object, holding two objects of type User