View Javadoc
1   package com.guinetik.examples;
2   
3   import com.guinetik.rr.RocketRest;
4   import com.guinetik.rr.RocketRestConfig;
5   import com.guinetik.rr.json.JsonObjectMapper;
6   
7   /**
8    * Example that demonstrates fetching a Todo from JSONPlaceholder API
9    */
10  public class JsonTodoExample implements Example {
11      
12      @Override
13      public String getName() {
14          return "JSONPlaceholder Todo Example";
15      }
16      
17      @Override
18      public void run() {
19          // Create configuration for the API
20          RocketRestConfig config = RocketRestConfig.builder("https://jsonplaceholder.typicode.com")
21                  .build();
22  
23          // Create RocketRest client
24          RocketRest rocketrest = new RocketRest(config);
25  
26          try {
27              System.out.println("Fetching Todo from JSONPlaceholder API...");
28              
29              // Perform GET request and deserialize to Todo class
30              Todo todo = rocketrest.get("/todos/1", Todo.class);
31              
32              // Print the result
33              System.out.println("Successfully fetched Todo:");
34              System.out.println(JsonObjectMapper.toJsonString(todo));
35              
36          } catch (Exception e) {
37              System.err.println("Error fetching data: " + e.getMessage());
38              e.printStackTrace();
39          } finally {
40              // Always shutdown the client to release resources
41              rocketrest.shutdown();
42          }
43      }
44  }