HttpGETCollectionExample.java Source code

Java tutorial

Introduction

Here is the source code for HttpGETCollectionExample.java

Source

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kles.mi.MIError;
import com.kles.mi.MIProgramMetadata;
import com.kles.mi.MIResult;
import com.kles.utils.XMLtoJSonConverter;
import java.io.IOException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.glassfish.jersey.jackson.JacksonFeature;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author jchau
 */
public class HttpGETCollectionExample {

    public static void main(String[] args) throws IOException {
        httpGETCollectionExample();
    }

    private static void httpGETCollectionExample() throws IOException {
        //        String testJson="{\"name\":\"mkyong\",\"age\":33,\"position\":\"Developer\",\"salary\":7500,\"skills\":[\"java\",\"python\"]}";
        //        ObjectMapper map = new ObjectMapper();
        //        Staff staff1 = map.readValue(testJson, Staff.class);
        //        
        //        String testApi="{\"program\":\"CRS610MI\",\"transaction\":\"LstByName\"}";
        //        ObjectMapper map1 = new ObjectMapper();
        //        Staff r=map1.readValue(testApi, Staff.class);

        ClientConfig clientConfig = new ClientConfig();

        HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("JCHAUT", "Sgknoa1984");
        clientConfig.register(feature);

        clientConfig.register(JacksonFeature.class);

        String urlExecute = "http://192.168.111.17:20007/m3api-rest/execute/";
        String urlMetadata = "http://192.168.111.17:20007/m3api-rest/metadata/";
        String pgmAPI = "PMS070MI";
        String trnAPI = "RptOperation";

        Client client = ClientBuilder.newClient(clientConfig);
        WebTarget webTarget = client.target(urlExecute + pgmAPI + "/" + trnAPI);
        //        WebTarget webTarget = client.target(urlMetadata + pgmAPI);

        Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
        Response response = invocationBuilder.get();

        System.out.println(response.getStatus());
        System.out.println(response.getStatusInfo());
        ObjectMapper mapper = new ObjectMapper();
        if (response.getStatus() == 200) {
            String responseAsString = response.readEntity(String.class);
            System.out.println(responseAsString);
            //MIProgramMetadata pgmmeta=mapper.readValue(XMLtoJSonConverter.getJSONFromXML(responseAsString).toString(), MIProgramMetadata.class);
            try {
                MIResult res = mapper.readValue(responseAsString, MIResult.class);
            } catch (Exception e) {
                MIError err = mapper.readValue(responseAsString, MIError.class);
            }
        }
    }
}