Search in sources :

Example 1 with BodyBuilder

use of org.springframework.http.RequestEntity.BodyBuilder in project tutorials by eugenp.

the class ApiClient method invokeAPI.

/**
 * Invoke API by sending HTTP request with the given options.
 *
 * @param <T> the return type to use
 * @param path The sub-path of the HTTP URL
 * @param method The request method
 * @param queryParams The query parameters
 * @param body The request body object
 * @param headerParams The header parameters
 * @param formParams The form parameters
 * @param accept The request's Accept header
 * @param contentType The request's Content-Type header
 * @param authNames The authentications to apply
 * @param returnType The return type into which to deserialize the response
 * @return The response body in chosen type
 */
public <T> T invokeAPI(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
    updateParamsForAuth(authNames, queryParams, headerParams);
    final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
    if (queryParams != null) {
        builder.queryParams(queryParams);
    }
    final BodyBuilder requestBuilder = RequestEntity.method(method, builder.build().toUri());
    if (accept != null) {
        requestBuilder.accept(accept.toArray(new MediaType[accept.size()]));
    }
    if (contentType != null) {
        requestBuilder.contentType(contentType);
    }
    addHeadersToRequest(headerParams, requestBuilder);
    addHeadersToRequest(defaultHeaders, requestBuilder);
    RequestEntity<Object> requestEntity = requestBuilder.body(selectBody(body, formParams, contentType));
    ResponseEntity<T> responseEntity = restTemplate.exchange(requestEntity, returnType);
    statusCode = responseEntity.getStatusCode();
    responseHeaders = responseEntity.getHeaders();
    if (responseEntity.getStatusCode() == HttpStatus.NO_CONTENT) {
        return null;
    } else if (responseEntity.getStatusCode().is2xxSuccessful()) {
        if (returnType == null) {
            return null;
        }
        return responseEntity.getBody();
    } else {
        // The error handler built into the RestTemplate should handle 400 and 500 series errors.
        throw new RestClientException("API returned " + statusCode + " and it wasn't handled by the RestTemplate error handler");
    }
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) RestClientException(org.springframework.web.client.RestClientException) MediaType(org.springframework.http.MediaType) BodyBuilder(org.springframework.http.RequestEntity.BodyBuilder)

Aggregations

MediaType (org.springframework.http.MediaType)1 BodyBuilder (org.springframework.http.RequestEntity.BodyBuilder)1 RestClientException (org.springframework.web.client.RestClientException)1 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)1