@Override
  public RestLiResponseData<BatchCreateResponseEnvelope> buildRestLiResponseData(Request request,
                                                    RoutingResult routingResult,
                                                    Object result,
                                                    Map<String, String> headers,
                                                    List<HttpCookie> cookies)
  {
    Object altKey = null;
    if (routingResult.getContext().hasParameter(RestConstants.ALT_KEY_PARAM))
    {
      altKey = routingResult.getContext().getParameter(RestConstants.ALT_KEY_PARAM);
    }
    final ProtocolVersion protocolVersion = ProtocolVersionUtil.extractProtocolVersion(headers);

    final ResourceContext resourceContext = routingResult.getContext();

    if (result instanceof BatchCreateKVResult && resourceContext.isReturnEntityRequested())
    {
      BatchCreateKVResult<?, ?> list = (BatchCreateKVResult<?, ?>) result;
      if (list.getResults() == null)
      {
        throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR,
                                         "Unexpected null encountered. Null List inside of a BatchCreateKVResult returned by the resource method: " + routingResult
                                             .getResourceMethod());
      }
      List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> collectionCreateList = new ArrayList<>(list.getResults().size());

      TimingContextUtil.beginTiming(routingResult.getContext().getRawRequestContext(),
          FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());

      for (CreateKVResponse<?, ?> createKVResponse : list.getResults())
      {
        if (createKVResponse == null)
        {
          throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR,
                                           "Unexpected null encountered. Null element inside of List inside of a BatchCreateKVResult returned by the resource method: "
                                               + routingResult.getResourceMethod());
        }
        else
        {
          Object id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(createKVResponse.getId(), routingResult);
          if (createKVResponse.getError() == null)
          {
            DataMap entityData = createKVResponse.getEntity() != null ? createKVResponse.getEntity().data() : null;

            final DataMap data = RestUtils.projectFields(entityData, resourceContext);

            CreateIdEntityStatus<Object, RecordTemplate> entry = new CreateIdEntityStatus<>(
                    createKVResponse.getStatus().getCode(),
                    id,
                    new AnyRecord(data),
                    getLocationUri(request, id, altKey, protocolVersion), // location uri
                    null,
                    protocolVersion);
            collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(entry));

          }
          else
          {
            collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(createKVResponse.getError()));
          }
        }
      }

      TimingContextUtil.endTiming(routingResult.getContext().getRawRequestContext(),
          FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());

      return new RestLiResponseDataImpl<>(new BatchCreateResponseEnvelope(HttpStatus.S_200_OK, collectionCreateList, true), headers, cookies);
    }
    else
    {
      List<? extends CreateResponse> createResponses = extractCreateResponseList(result);

      //Verify that a null list was not passed into the BatchCreateResult. If so, this is a developer error.
      if (createResponses == null)
      {
        throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR,
                                         "Unexpected null encountered. Null List inside of a BatchCreateResult returned by the resource method: " + routingResult
                                             .getResourceMethod());
      }

      List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> collectionCreateList = new ArrayList<>(createResponses.size());
      for (CreateResponse createResponse : createResponses)
      {
        //Verify that a null element was not passed into the BatchCreateResult list. If so, this is a developer error.
        if (createResponse == null)
        {
          throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR,
                                           "Unexpected null encountered. Null element inside of List inside of a BatchCreateResult returned by the resource method: "
                                               + routingResult.getResourceMethod());
        }
        else
        {
          Object id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(createResponse.getId(), routingResult);
          if (createResponse.getError() == null)
          {
            CreateIdStatus<Object> entry = new CreateIdStatus<>(
                    createResponse.getStatus().getCode(),
                    id,
                    getLocationUri(request, id, altKey, protocolVersion), // location uri
                    null,
                    protocolVersion);
            collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(entry));
          }
          else
          {
            collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(createResponse.getError()));
          }
        }
      }

      return new RestLiResponseDataImpl<>(new BatchCreateResponseEnvelope(HttpStatus.S_200_OK, collectionCreateList, false), headers, cookies);
    }
  }
@Test(dataProvider = "exceptionTestData")
  public void testBuilderExceptions(Object result, String expectedErrorMessage) throws URISyntaxException
  {
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
    ServerResourceContext mockContext = getMockResourceContext(null);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);

    BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(null);
    RestRequest request = new RestRequestBuilder(new URI("/foo")).build();
    try
    {
      responseBuilder.buildRestLiResponseData(request, routingResult, result, headers, Collections.emptyList());
      Assert.fail("buildRestLiResponseData should have thrown an exception because of null elements");
    }
    catch (RestLiServiceException e)
    {
      Assert.assertTrue(e.getMessage().contains(expectedErrorMessage));
    }
  }