Class JsonTemplate

java.lang.Object
com.tccc.commons.httpclient.JsonTemplate

public final class JsonTemplate extends Object
System component that provides an easy way to issue HTTP/REST calls, such at GET, POST, PUT, and DELETE. It is built on top of the Jetty HttpClient class. The RequestContent serializes the response to a JSON object.

Usage

 
  public class MyClass {

      @Autowired
      private JsonTemplate jsonTemplate;

      // . . .
  }
 
 
Since:
1.0
Version:
2023-03-06
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> JsonResp<T>
    DELETE(String url, Object body, Class<T> clazz, boolean envelope)
    Performs a DELETE request on the specified URL and then parses the response into an instance of the specified class.
    <T> JsonResp<T>
    DELETE(URI uri, Object body, Class<T> clazz, boolean envelope)
    Performs a DELETE request on the specified URI and then parses the response into an instance of the specified class.
    <T> JsonResp<T>
    fromJson(ContentResponse resp, Class<T> clazz, boolean envelope)
    Parses the body of the specified response into the specified class.
    <T> JsonResp<T>
    GET(String url, Class<T> clazz, boolean envelope)
    Performs a GET request on the specified URL and then parses the response into an instance of the specified class.
    <T> JsonResp<T>
    GET(URI uri, Class<T> clazz, boolean envelope)
    Performs a GET request on the specified URI and then parses the response into an instance of the specified class.
    Returns the HTTP client.
    <T> JsonResp<T>
    POST(String url, Object body, Class<T> clazz, boolean envelope)
    Performs a POST request on the specified URL with the body object and then parses the response into an instance of the specified class.
    <T> JsonResp<T>
    POST(URI uri, Object body, Class<T> clazz, boolean envelope)
    Performs a POST request on the specified URI with the body object and then parses the response into an instance of the specified class.
    <T> JsonResp<T>
    PUT(String url, Object body, Class<T> clazz, boolean envelope)
    Performs a PUT request on the specified URL with the body object and then parses the response into an instance of the specified class.
    <T> JsonResp<T>
    PUT(URI uri, Object body, Class<T> clazz, boolean envelope)
    Performs a PUT request on the specified URI with the body object and then parses the response into an instance of the specified class.
    <T> JsonResp<T>
    send(Request request, Class<T> clazz, boolean envelope)
    Sends the given request and then parses the response into an instance of the specified class.
    <T> JsonResp<T>
    send(org.eclipse.jetty.http.HttpMethod method, String url, Object body, Class<T> clazz, boolean envelope)
    Sends the given request and then parses the response into an instance of the specified class.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getClient

      public HttpClient getClient()
      Returns the HTTP client.
      Returns:
      HttpClient
    • send

      public <T> JsonResp<T> send(Request request, Class<T> clazz, boolean envelope)
      Sends the given request and then parses the response into an instance of the specified class.
      Parameters:
      request - the request to send
      clazz - the response type
      envelope - is true if response has kOS envelope
    • send

      public <T> JsonResp<T> send(org.eclipse.jetty.http.HttpMethod method, String url, Object body, Class<T> clazz, boolean envelope)
      Sends the given request and then parses the response into an instance of the specified class.
      Parameters:
      method - the HTTP method
      url - the URL endpoint
      body - the object which is the message body
      clazz - the response type
      envelope - true if response has kOS envelope
    • GET

      public <T> JsonResp<T> GET(String url, Class<T> clazz, boolean envelope)
      Performs a GET request on the specified URL and then parses the response into an instance of the specified class.
      Parameters:
      url - the URL endpoint to GET
      clazz - the response type
      envelope - true if response has kOS envelope
    • GET

      public <T> JsonResp<T> GET(URI uri, Class<T> clazz, boolean envelope)
      Performs a GET request on the specified URI and then parses the response into an instance of the specified class.
      Parameters:
      uri - the URI endpoint to GET
      clazz - the response type
      envelope - true if response has kOS envelope
    • POST

      public <T> JsonResp<T> POST(String url, Object body, Class<T> clazz, boolean envelope)
      Performs a POST request on the specified URL with the body object and then parses the response into an instance of the specified class.
      Parameters:
      url - the URL endpoint to POST
      body - the body object
      clazz - the response type
      envelope - true if response has kOS envelope
    • POST

      public <T> JsonResp<T> POST(URI uri, Object body, Class<T> clazz, boolean envelope)
      Performs a POST request on the specified URI with the body object and then parses the response into an instance of the specified class.
      Parameters:
      uri - the URI endpoint to POST
      body - the body object
      clazz - the response type
      envelope - true if response has kOS envelope
    • PUT

      public <T> JsonResp<T> PUT(String url, Object body, Class<T> clazz, boolean envelope)
      Performs a PUT request on the specified URL with the body object and then parses the response into an instance of the specified class.
      Parameters:
      url - the URL endpoint to PUT
      body - the body object
      clazz - the response type
      envelope - true if response has kOS envelope
    • PUT

      public <T> JsonResp<T> PUT(URI uri, Object body, Class<T> clazz, boolean envelope)
      Performs a PUT request on the specified URI with the body object and then parses the response into an instance of the specified class.
      Parameters:
      uri - the URI endpoint to PUT
      body - the body object
      clazz - the response type
      envelope - true if response has kOS envelope
    • DELETE

      public <T> JsonResp<T> DELETE(String url, Object body, Class<T> clazz, boolean envelope)
      Performs a DELETE request on the specified URL and then parses the response into an instance of the specified class.
      Parameters:
      url - the URL endpoint to DELETE
      clazz - the response type
      envelope - true if response has kOS envelope
    • DELETE

      public <T> JsonResp<T> DELETE(URI uri, Object body, Class<T> clazz, boolean envelope)
      Performs a DELETE request on the specified URI and then parses the response into an instance of the specified class.
      Parameters:
      uri - the URI endpoint to DELETE
      clazz - the response type
      envelope - true if response has kOS envelope
    • fromJson

      public <T> JsonResp<T> fromJson(ContentResponse resp, Class<T> clazz, boolean envelope)
      Parses the body of the specified response into the specified class.
      Parameters:
      resp - the response
      clazz - the response type
      envelope - true if response has kOS envelope