Versions Compared

Key

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

...

Code Block
languagejava
titleCode Sample invoking a GET - to find a user
linenumberstrue
package com.celestial.jaxrs.client;

import com.celestial.dbankbusinessobjects.User;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;

/**
 *
 * @author Selvyn
 */
public class TestServices
{
    public static void main(String[] args)
    {
        try
        {
            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/fu/selvyn/gradprog2016");

            ClientResponse response = webResource.accept("application/json")
                    .type("application/json").get(ClientResponse.class);

            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);

        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

...

Code Block
languagejava
firstline26
titleSample Code invoking a DELETE - to remove a user
linenumberstrue
            User us = new User("penny", "leonard");

            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/du");

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

            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);


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);

...