Java Code Examples for org.springframework.web.util.DefaultUriBuilderFactory#setEncodingMode()

The following examples show how to use org.springframework.web.util.DefaultUriBuilderFactory#setEncodingMode() . 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
Source File: WebClientRequestsUnitTest.java    From tutorials with MIT License 7 votes vote down vote up
@Test
public void whenUriComponentEncoding_thenQueryParamsNotEscaped() {
    DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(BASE_URL);
    factory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.URI_COMPONENT);
    this.webClient = WebClient
            .builder()
            .uriBuilderFactory(factory)
            .baseUrl(BASE_URL)
            .exchangeFunction(exchangeFunction)
            .build();
    this.webClient.get()
            .uri(uriBuilder -> uriBuilder
                    .path("/products/")
                    .queryParam("name", "AndroidPhone")
                    .queryParam("color", "black")
                    .queryParam("deliveryDate", "13/04/2019")
                    .build())
            .exchange()
            .block(Duration.ofSeconds(1));
    verifyCalledUrl("/products/?name=AndroidPhone&color=black&deliveryDate=13/04/2019");
}
 
Example 2
Source File: SpringRESTClientConnector.java    From egeria with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor
 */
public SpringRESTClientConnector() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException
{
    super();

    /*
     * Rather than creating a RestTemplate directly, the RestTemplateBuilder is used so that the
     * uriTemplateHandler can be specified. The URI encoding is set to VALUES_ONLY so that the
     * '+' character, which is used in queryParameters conveying searchCriteria, which can be a
     * regex, is encoded as '+' and not converted to a space character.
     * Prior to this change a regex containing a '+' character would be split into two space
     * separated words. For example, the regex "name_0+7" (which would match name_07, name_007,
     * name_0007, etc) would be sent to the server as "name_0 7".
     */
    DefaultUriBuilderFactory builderFactory = new DefaultUriBuilderFactory();
    builderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.VALUES_ONLY);


    /* TODO: Disable SSL cert verification -- for now */
    HttpsURLConnection.setDefaultHostnameVerifier(bypassVerifier);
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, INSECURE_MANAGER, null);
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

    restTemplate = new RestTemplate();

    restTemplate.setUriTemplateHandler(builderFactory);

    /* Ensure that the REST template always uses UTF-8 */
    List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
    converters.removeIf(httpMessageConverter -> httpMessageConverter instanceof StringHttpMessageConverter);
    converters.add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));

}
 
Example 3
Source File: RestTemplate.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private static DefaultUriBuilderFactory initUriTemplateHandler() {
	DefaultUriBuilderFactory uriFactory = new DefaultUriBuilderFactory();
	uriFactory.setEncodingMode(EncodingMode.URI_COMPONENT);  // for backwards compatibility..
	return uriFactory;
}
 
Example 4
Source File: RestTemplate.java    From java-technology-stack with MIT License 4 votes vote down vote up
private static DefaultUriBuilderFactory initUriTemplateHandler() {
	DefaultUriBuilderFactory uriFactory = new DefaultUriBuilderFactory();
	uriFactory.setEncodingMode(EncodingMode.URI_COMPONENT);  // for backwards compatibility..
	return uriFactory;
}