public static void main(String[] args) throws URISyntaxException, IOException {

    // Create and configure a new instance of RallyRestApi
    // (Server, username, password and proxy settings configured in Factory)
    RallyRestApi restApi = RestApiFactory.getRestApi();

    try {
      String ref = "/hierarchicalrequirement/12544729477";
      GetRequest getRequest = new GetRequest(ref);
      getRequest.setFetch(new Fetch("Defects"));

      System.out.println(String.format("\nReading defect info for story %s...", ref));
      GetResponse getResponse = restApi.get(getRequest);
      JsonObject story = getResponse.getObject();

      JsonObject defectInfo = story.getAsJsonObject("Defects");
      int defectCount = defectInfo.get("Count").getAsInt();
      System.out.println(String.format("\nTotal defects: %d", defectCount));

      if (defectCount > 0) {
        QueryRequest defectRequest = new QueryRequest(defectInfo);
        defectRequest.setFetch(new Fetch("FormattedID", "Name", "State", "Priority"));

        QueryResponse queryResponse = restApi.query(defectRequest);
        if (queryResponse.wasSuccessful()) {
          for (JsonElement result : queryResponse.getResults()) {
            JsonObject defect = result.getAsJsonObject();
            System.out.println(
                String.format(
                    "\t%s - %s: Priority=%s, State=%s",
                    defect.get("FormattedID").getAsString(),
                    defect.get("Name").getAsString(),
                    defect.get("Priority").getAsString(),
                    defect.get("State").getAsString()));
          }
        }
      }
    } finally {
      // Release resources
      restApi.close();
    }
  }
  public static void main(String[] args) throws URISyntaxException, IOException {

    // Create and configure a new instance of RallyRestApi
    // (Server, username, password and proxy settings configured in Factory)
    RallyRestApi restApi = RestApiFactory.getRestApi();

    try {

      // Create a defect
      System.out.println("Creating defect...");
      JsonObject newDefect = new JsonObject();
      newDefect.addProperty("Name", "Test Defect");
      CreateRequest createRequest = new CreateRequest("defect", newDefect);
      CreateResponse createResponse = restApi.create(createRequest);
      System.out.println(
          String.format("Created %s", createResponse.getObject().get("_ref").getAsString()));

      // Read defect
      String ref = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
      System.out.println(String.format("\nReading defect %s...", ref));
      GetRequest getRequest = new GetRequest(ref);
      GetResponse getResponse = restApi.get(getRequest);
      JsonObject obj = getResponse.getObject();
      System.out.println(
          String.format(
              "Read defect. Name = %s, State = %s",
              obj.get("Name").getAsString(), obj.get("State").getAsString()));

      // Update defect
      System.out.println("\nUpdating defect state...");
      JsonObject updatedDefect = new JsonObject();
      updatedDefect.addProperty("State", "Fixed");
      UpdateRequest updateRequest = new UpdateRequest(ref, updatedDefect);
      UpdateResponse updateResponse = restApi.update(updateRequest);
      obj = updateResponse.getObject();
      System.out.println(
          String.format("Updated defect. State = %s", obj.get("State").getAsString()));

      // Delete defect
      System.out.println("\nDeleting defect...");
      DeleteRequest deleteRequest = new DeleteRequest(ref);
      DeleteResponse deleteResponse = restApi.delete(deleteRequest);
      if (deleteResponse.wasSuccessful()) {
        System.out.println("Deleted defect.");
      }

    } finally {
      // Release all resources
      restApi.close();
    }
  }