src/main/java/util/RESTUtil.java
public <T> T sendPostRestRequest(java.lang.String endPointURL,
                                     Object requestClass,
                                     Class<T> responseClass) throws Exception {

        Gson gson = new Gson();
        //store request
        jsonRequest = gson.toJson(requestClass);
        URL url = new URL(endPointURL);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setConnectTimeout(CONN_TIME_OUT);
        conn.setReadTimeout(READ_TIME_OUT);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        OutputStream out = (conn.getOutputStream());
        out.write(gson.toJson(requestClass).getBytes());
        out.close();

        JsonReader reader =
            new JsonReader(new InputStreamReader(conn.getInputStream()));
        JsonParser parser = new JsonParser();
        JsonElement rootElement = parser.parse(reader);
        jsonObject = rootElement.getAsJsonObject();
        if (responseClass == null)
            return null;
        return gson.fromJson(jsonObject, responseClass);

    }
/** All In One Method for your REST service call , this method is using POST request.
     * @param <T>
     * @param endPointURL : url as string for your REST call
     * @param requestClass : pass your object of request class which will go in rest request as PUT method
     * @param responseClass : pass your class type u want in response:  for Ex: AddressResponse.Class
     * @return your desired object of class which u specified in responseClass param
     * @throws Exception
     */
All In One Method for your REST service call , this method is using POST request.
All In One Method for your REST service call this method is using POST request .
public <T> T sendPostRestRequest(java.lang.String endPointURL,
                                     Object requestClass,
                                     Class<T> responseClass) throws Exception {
        Gson gson = new Gson();
        jsonRequest = gson.toJson(requestClass);
        URL url = new URL(endPointURL);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setConnectTimeout(CONN_TIME_OUT);
        conn.setReadTimeout(READ_TIME_OUT);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        OutputStream out = (conn.getOutputStream());
        out.write(gson.toJson(requestClass).getBytes());
        out.close();
        JsonReader reader =
            new JsonReader(new InputStreamReader(conn.getInputStream()));
        JsonParser parser = new JsonParser();
        JsonElement rootElement = parser.parse(reader);
        jsonObject = rootElement.getAsJsonObject();
        if (responseClass == null)
            return null;
        return gson.fromJson(jsonObject, responseClass);
    }
public < T > T sendPostRestRequest ( java . lang . String endPointURL , Object requestClass , Class < T > responseClass ) throws Exception { Gson gson = new Gson ( ) ; jsonRequest = gson . toJson ( requestClass ) ; URL url = new URL ( endPointURL ) ; HttpURLConnection conn = ( HttpURLConnection ) url . openConnection ( ) ; conn . setConnectTimeout ( CONN_TIME_OUT ) ; conn . setReadTimeout ( READ_TIME_OUT ) ; conn . setDoOutput ( true ) ; conn . setRequestMethod ( "POST" ) ; conn . setRequestProperty ( "Content-Type" , "application/json" ) ; OutputStream out = ( conn . getOutputStream ( ) ) ; out . write ( gson . toJson ( requestClass ) . getBytes ( ) ) ; out . close ( ) ; JsonReader reader = new JsonReader ( new InputStreamReader ( conn . getInputStream ( ) ) ) ; JsonParser parser = new JsonParser ( ) ; JsonElement rootElement = parser . parse ( reader ) ; jsonObject = rootElement . getAsJsonObject ( ) ; if ( responseClass == null ) return null ; return gson . fromJson ( jsonObject , responseClass ) ; }
All In One Method for your REST service call , this method is using POST request.
All In One Method for your REST service call this method is using POST request .
//store request