Java Code Examples for org.springframework.web.util.UriComponentsBuilder#queryParams()

The following examples show how to use org.springframework.web.util.UriComponentsBuilder#queryParams() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
protected ServiceCallResponse callService(String serviceId,
											OAuth2AuthorizedClient authorizedClient,
											MultiValueMap<String, String> params) {

	ServicesConfig.ServiceConfig serviceConfig = this.servicesConfig.getConfig(serviceId);
	UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(serviceConfig.getUri());
	if (!params.isEmpty()) {
		uriBuilder.queryParams(params);
	}
	URI uri = uriBuilder.build().toUri();

	return this.webClient
			.get()
			.uri(uri)
			.attributes(oauth2AuthorizedClient(authorizedClient))
			.retrieve()
			.bodyToMono(ServiceCallResponse.class)
			.block();
}
 
Example 2
Source Project: openapi-generator   File: ApiClient.java    License: Apache License 2.0 6 votes vote down vote up
private WebClient.RequestBodySpec prepareRequest(String path, HttpMethod method, Map<String, Object> pathParams, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames) {
    updateParamsForAuth(authNames, queryParams, headerParams, cookieParams);

    final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
    if (queryParams != null) {
        builder.queryParams(queryParams);
    }

    final WebClient.RequestBodySpec requestBuilder = webClient.method(method).uri(builder.build(false).toUriString(), pathParams);
    if(accept != null) {
        requestBuilder.accept(accept.toArray(new MediaType[accept.size()]));
    }
    if(contentType != null) {
        requestBuilder.contentType(contentType);
    }

    addHeadersToRequest(headerParams, requestBuilder);
    addHeadersToRequest(defaultHeaders, requestBuilder);
    addCookiesToRequest(cookieParams, requestBuilder);
    addCookiesToRequest(defaultCookies, requestBuilder);

    requestBuilder.body(selectBody(body, formParams, contentType));
    return requestBuilder;
}
 
Example 3
Source Project: plumdo-work   File: RestClient.java    License: Apache License 2.0 6 votes vote down vote up
public <T> T exchange(String url, HttpMethod method, MultiValueMap<String, String> queryParams, Object requestBody, Class<T> responseType) {
    T response = null;
    long beginTime = System.currentTimeMillis();
    try {
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        builder.queryParams(queryParams);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Token", Authentication.getToken());
        response = restTemplate.exchange(builder.build().toUri(), method, new HttpEntity<>(requestBody, headers), responseType).getBody();
        log.debug("发送Http请求:{},方法:{},参数:{},返回:{},[{}]ms", url, method, requestBody, response, DateUtils.getTimeMillisConsume(beginTime));
    } catch (RestClientException e) {
        log.error("发送http请求:{},方法:{},参数:{},异常:{},[{}]ms", url, method, requestBody, e.getMessage(), DateUtils.getTimeMillisConsume(beginTime));
    }

    return response;
}
 
Example 4
/**
 * Method that adds all parameters needed to make the request with the provider
 *
 * @param uri The {@link UriComponentsBuilder} to set query params
 * @param providerParams List of the {@link ProviderParam}
 * @return The {@link Http} result
 */
private HttpEntity createHttpEntity(UriComponentsBuilder uri, List<ProviderParam> providerParams) {

    HttpHeaders headers = new HttpHeaders();
    String body = "";

    Map<String, Object> paramsBody = new HashMap<>();
    MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();

    for (ProviderParam param : providerParams) {

        if (Objects.isNull(param.getValue()) || param.getValue().isEmpty()) {
            param.setValue(context.getRequest().getHeader(param.getName()));
        }

        switch (param.getLocation().toUpperCase()) {
            case "HEADER":
                headers.add(param.getName(), param.getValue());
                break;
            case "BODY":
                paramsBody.put(param.getName(), param.getValue());
                break;
            default:
                queryParams.put(param.getName(), Collections.singletonList(param.getValue()));
                break;
        }
    }

    if (paramsBody.size() > 0) {
        try {
            body = StringEscapeUtils.unescapeJava(mapper().writeValueAsString(paramsBody));
        } catch (JsonProcessingException e) {
            log.error(e.getMessage(), e);
        }
    }

    uri.queryParams(queryParams);
    return new HttpEntity<>(body, headers);
}
 
Example 5
Source Project: tutorials   File: ApiClient.java    License: MIT License 5 votes vote down vote up
/**
 * 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");
    }
}