private static Function<HttpResponse, HttpResponse> createModifiedLocationHeaderAddingResponseMapper( final HttpRequest request, final CommandResponse commandResponse) { return response -> { if (HttpStatusCode.CREATED == commandResponse.getStatusCode()) { Uri newUri = request.getUri(); if (!request.method().isIdempotent()) { // only for not idempotent requests (e.g.: POST), add the "createdId" to the path: final String uriStr = newUri.toString(); String createdLocation; final int uriIdIndex = uriStr.indexOf(commandResponse.getId()); // if the uri contains the id, but *not* at the beginning if (uriIdIndex > 0) { createdLocation = uriStr.substring(0, uriIdIndex) + commandResponse.getId() + commandResponse.getResourcePath().toString(); } else { createdLocation = uriStr + "/" + commandResponse.getId() + commandResponse.getResourcePath() .toString(); } if (createdLocation.endsWith("/")) { createdLocation = createdLocation.substring(0, createdLocation.length() - 1); } newUri = Uri.create(createdLocation); } return response.addHeader(Location.create(newUri)); } else { return response; } }; }