org.springframework.web.util.UriTemplateHandler Java Examples

The following examples show how to use org.springframework.web.util.UriTemplateHandler. 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: CustomRestTemplate.java    From hesperides with GNU General Public License v3.0 6 votes vote down vote up
public CustomRestTemplate(Gson gson, UriTemplateHandler uriTemplateHandler, CloseableHttpClient httpClient) {
    super();
    this.gson = gson;
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    if (httpClient != null) {
        requestFactory.setHttpClient(httpClient);
    }
    requestFactory.setBufferRequestBody(false);
    setRequestFactory(requestFactory);
    setUriTemplateHandler(uriTemplateHandler);
    setErrorHandler(new NoOpResponseErrorHandler());
    // On vire Jackson:
    List<HttpMessageConverter<?>> converters = getMessageConverters().stream()
            .filter(httpMessageConverter -> !(httpMessageConverter instanceof MappingJackson2HttpMessageConverter))
            .collect(Collectors.toList());
    configureMessageConverters(converters, gson);
    setMessageConverters(converters);
}
 
Example #2
Source File: AsyncRestTemplate.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Configure default URI variable values. This is a shortcut for:
 * <pre class="code">
 * DefaultUriTemplateHandler handler = new DefaultUriTemplateHandler();
 * handler.setDefaultUriVariables(...);
 *
 * AsyncRestTemplate restTemplate = new AsyncRestTemplate();
 * restTemplate.setUriTemplateHandler(handler);
 * </pre>
 * @param defaultUriVariables the default URI variable values
 * @since 4.3
 */
@SuppressWarnings("deprecation")
public void setDefaultUriVariables(Map<String, ?> defaultUriVariables) {
	UriTemplateHandler handler = this.syncTemplate.getUriTemplateHandler();
	if (handler instanceof DefaultUriBuilderFactory) {
		((DefaultUriBuilderFactory) handler).setDefaultUriVariables(defaultUriVariables);
	}
	else if (handler instanceof org.springframework.web.util.AbstractUriTemplateHandler) {
		((org.springframework.web.util.AbstractUriTemplateHandler) handler)
				.setDefaultUriVariables(defaultUriVariables);
	}
	else {
		throw new IllegalArgumentException(
				"This property is not supported with the configured UriTemplateHandler.");
	}
}
 
Example #3
Source File: UriTemplateHandlerTest.java    From spring-boot-cookbook with Apache License 2.0 6 votes vote down vote up
@Test
public void uriTemplateHandlerTest() {
    UriTemplateHandler uriTemplateHandler = new DefaultUriTemplateHandler();
    Map<String, String> uriVariables = new HashMap<>();
    uriVariables.put("orderNo", "12345");
    URI expand = uriTemplateHandler.expand("https://chaojihao.com/user/order/detail?orderno={orderNo}", uriVariables);
    System.out.println(expand.toString());
    assertThat(expand.toString()).isEqualTo("https://chaojihao.com/user/order/detail?orderno=12345");
}
 
Example #4
Source File: AsyncRestTemplate.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Configure default URI variable values. This is a shortcut for:
 * <pre class="code">
 * DefaultUriTemplateHandler handler = new DefaultUriTemplateHandler();
 * handler.setDefaultUriVariables(...);
 *
 * AsyncRestTemplate restTemplate = new AsyncRestTemplate();
 * restTemplate.setUriTemplateHandler(handler);
 * </pre>
 * @param defaultUriVariables the default URI variable values
 * @since 4.3
 */
@SuppressWarnings("deprecation")
public void setDefaultUriVariables(Map<String, ?> defaultUriVariables) {
	UriTemplateHandler handler = this.syncTemplate.getUriTemplateHandler();
	if (handler instanceof DefaultUriBuilderFactory) {
		((DefaultUriBuilderFactory) handler).setDefaultUriVariables(defaultUriVariables);
	}
	else if (handler instanceof org.springframework.web.util.AbstractUriTemplateHandler) {
		((org.springframework.web.util.AbstractUriTemplateHandler) handler)
				.setDefaultUriVariables(defaultUriVariables);
	}
	else {
		throw new IllegalArgumentException(
				"This property is not supported with the configured UriTemplateHandler.");
	}
}
 
Example #5
Source File: UriTemplateHandlerTest.java    From spring-boot-cookbook with Apache License 2.0 5 votes vote down vote up
@Test
public void uriTemplateHandlerWithTwoQueryStringTest() {
    UriTemplateHandler uriTemplateHandler = new DefaultUriTemplateHandler();
    Map<String, String> uriVariables = new HashMap<>();
    uriVariables.put("orderNo", "12345");
    URI expand = uriTemplateHandler.expand("https://chaojihao.com/user/order/detail?orderno={orderNo}&logtoSensor=1", uriVariables);
    System.out.println(expand.toString());
    assertThat(expand.toString()).isEqualTo("https://chaojihao.com/user/order/detail?orderno=12345&logtoSensor=1");
}
 
Example #6
Source File: RestTemplatePostProcessor.java    From summerframework with Apache License 2.0 5 votes vote down vote up
public void customize(final AsyncRestTemplate restTemplate) {
    if (restTemplate.getInterceptors().contains(this.metricsClientHttpRequestInterceptor)) {
        return;
    }
    UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler();
    templateHandler = this.metricsClientHttpRequestInterceptor.createUriTemplateHandler(templateHandler);
    restTemplate.setUriTemplateHandler(templateHandler);
    List<AsyncClientHttpRequestInterceptor> interceptors = new ArrayList<>();
    interceptors.add(this.metricsClientHttpRequestInterceptor);
    interceptors.addAll(restTemplate.getInterceptors());
    restTemplate.setInterceptors(interceptors);
}
 
Example #7
Source File: RestTemplatePostProcessor.java    From summerframework with Apache License 2.0 5 votes vote down vote up
@Override
public void customize(RestTemplate restTemplate) {
    if (restTemplate.getInterceptors().contains(this.metricsClientHttpRequestInterceptor)) {
        return;
    }
    UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler();
    templateHandler = this.metricsClientHttpRequestInterceptor.createUriTemplateHandler(templateHandler);
    restTemplate.setUriTemplateHandler(templateHandler);
    List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
    interceptors.add(this.metricsClientHttpRequestInterceptor);
    interceptors.addAll(restTemplate.getInterceptors());
    restTemplate.setInterceptors(interceptors);
}
 
Example #8
Source File: TestRestTemplateWrapper.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void dotNotSetUriTemplateHandlerWithUnderlying() {
  UriTemplateHandler uriTemplateHandler = mock(UriTemplateHandler.class);

  wrapper.setUriTemplateHandler(uriTemplateHandler);

  assertThat(wrapper.getUriTemplateHandler(), is(uriTemplateHandler));
  assertThat(wrapper.defaultRestTemplate.getUriTemplateHandler(), is(uriTemplateHandler));

  verify(underlying, never()).setUriTemplateHandler(uriTemplateHandler);
}
 
Example #9
Source File: RestTemplate.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Return the configured URI template handler.
 */
public UriTemplateHandler getUriTemplateHandler() {
	return this.uriTemplateHandler;
}
 
Example #10
Source File: CustomRestTemplate.java    From hesperides with GNU General Public License v3.0 4 votes vote down vote up
public CustomRestTemplate(Gson gson, UriTemplateHandler uriTemplateHandler) {
    this(gson, uriTemplateHandler, null);
}
 
Example #11
Source File: AsyncRestTemplate.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Return the configured URI template handler.
 */
public UriTemplateHandler getUriTemplateHandler() {
	return this.syncTemplate.getUriTemplateHandler();
}
 
Example #12
Source File: RestTemplate.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Return the configured URI template handler.
 */
public UriTemplateHandler getUriTemplateHandler() {
	return this.uriTemplateHandler;
}
 
Example #13
Source File: WxApiTemplate.java    From FastBootWeixin with Apache License 2.0 4 votes vote down vote up
public UriTemplateHandler getUriTemplateHandler() {
    return restTemplate.getUriTemplateHandler();
}
 
Example #14
Source File: WxApiTemplate.java    From FastBootWeixin with Apache License 2.0 4 votes vote down vote up
public void setUriTemplateHandler(UriTemplateHandler handler) {
    restTemplate.setUriTemplateHandler(handler);
}
 
Example #15
Source File: AsyncRestTemplate.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Return the configured URI template handler.
 */
public UriTemplateHandler getUriTemplateHandler() {
	return this.syncTemplate.getUriTemplateHandler();
}
 
Example #16
Source File: RestTemplate.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Return the configured URI template handler.
 */
public UriTemplateHandler getUriTemplateHandler() {
	return this.uriTemplateHandler;
}
 
Example #17
Source File: RestTemplateWrapper.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
public void setUriTemplateHandler(UriTemplateHandler handler) {
  super.setUriTemplateHandler(handler);
  defaultRestTemplate.setUriTemplateHandler(handler);
}
 
Example #18
Source File: AsyncRestTemplate.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Return the configured URI template handler.
 */
public UriTemplateHandler getUriTemplateHandler() {
	return this.syncTemplate.getUriTemplateHandler();
}
 
Example #19
Source File: RestTemplate.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Return the configured URI template handler.
 */
public UriTemplateHandler getUriTemplateHandler() {
	return this.uriTemplateHandler;
}
 
Example #20
Source File: AsyncRestTemplate.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Return the configured URI template handler.
 */
public UriTemplateHandler getUriTemplateHandler() {
	return this.syncTemplate.getUriTemplateHandler();
}
 
Example #21
Source File: AsyncRestTemplate.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Configure default URI variable values. This is a shortcut for:
 * <pre class="code">
 * DefaultUriTemplateHandler handler = new DefaultUriTemplateHandler();
 * handler.setDefaultUriVariables(...);
 *
 * AsyncRestTemplate restTemplate = new AsyncRestTemplate();
 * restTemplate.setUriTemplateHandler(handler);
 * </pre>
 * @param defaultUriVariables the default URI variable values
 * @since 4.3
 */
public void setDefaultUriVariables(Map<String, ?> defaultUriVariables) {
	UriTemplateHandler handler = this.syncTemplate.getUriTemplateHandler();
	Assert.isInstanceOf(AbstractUriTemplateHandler.class, handler,
			"Can only use this property in conjunction with a DefaultUriTemplateHandler");
	((AbstractUriTemplateHandler) handler).setDefaultUriVariables(defaultUriVariables);
}
 
Example #22
Source File: AsyncRestTemplate.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * This property has the same purpose as the corresponding property on the
 * {@code RestTemplate}. For more details see
 * {@link RestTemplate#setUriTemplateHandler}.
 * @param handler the URI template handler to use
 */
public void setUriTemplateHandler(UriTemplateHandler handler) {
	this.syncTemplate.setUriTemplateHandler(handler);
}
 
Example #23
Source File: RestTemplate.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Configure the {@link UriTemplateHandler} to use to expand URI templates.
 * By default the {@link DefaultUriTemplateHandler} is used which relies on
 * Spring's URI template support and exposes several useful properties that
 * customize its behavior for encoding and for prepending a common base URL.
 * An alternative implementation may be used to plug an external URI
 * template library.
 * @param handler the URI template handler to use
 */
public void setUriTemplateHandler(UriTemplateHandler handler) {
	Assert.notNull(handler, "UriTemplateHandler must not be null");
	this.uriTemplateHandler = handler;
}
 
Example #24
Source File: RestTemplate.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Set a custom {@link UriTemplateHandler} for expanding URI templates.
 * <p>By default, RestTemplate uses {@link DefaultUriTemplateHandler}.
 * @param handler the URI template handler to use
 */
public void setUriTemplateHandler(UriTemplateHandler handler) {
	Assert.notNull(handler, "UriTemplateHandler must not be null");
	this.uriTemplateHandler = handler;
}
 
Example #25
Source File: AsyncRestTemplate.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * This property has the same purpose as the corresponding property on the
 * {@code RestTemplate}. For more details see
 * {@link RestTemplate#setUriTemplateHandler}.
 * @param handler the URI template handler to use
 */
public void setUriTemplateHandler(UriTemplateHandler handler) {
	this.syncTemplate.setUriTemplateHandler(handler);
}
 
Example #26
Source File: AsyncRestTemplate.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Set a custom {@link UriTemplateHandler} for expanding URI templates.
 * <p>By default, RestTemplate uses {@link DefaultUriTemplateHandler}.
 * @param handler the URI template handler to use
 */
public void setUriTemplateHandler(UriTemplateHandler handler) {
	this.syncTemplate.setUriTemplateHandler(handler);
}
 
Example #27
Source File: RestTemplate.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Configure a strategy for expanding URI templates.
 * <p>By default, {@link DefaultUriBuilderFactory} is used and for
 * backwards compatibility, the encoding mode is set to
 * {@link EncodingMode#URI_COMPONENT URI_COMPONENT}. As of 5.0.8, prefer
 * using {@link EncodingMode#TEMPLATE_AND_VALUES TEMPLATE_AND_VALUES}.
 * <p><strong>Note:</strong> in 5.0 the switch from
 * {@link org.springframework.web.util.DefaultUriTemplateHandler
 * DefaultUriTemplateHandler} (deprecated in 4.3), as the default to use, to
 * {@link DefaultUriBuilderFactory} brings in a different default for the
 * {@code parsePath} property (switching from false to true).
 * @param handler the URI template handler to use
 */
public void setUriTemplateHandler(UriTemplateHandler handler) {
	Assert.notNull(handler, "UriTemplateHandler must not be null");
	this.uriTemplateHandler = handler;
}
 
Example #28
Source File: AsyncRestTemplate.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * This property has the same purpose as the corresponding property on the
 * {@code RestTemplate}. For more details see
 * {@link RestTemplate#setUriTemplateHandler}.
 * @param handler the URI template handler to use
 */
public void setUriTemplateHandler(UriTemplateHandler handler) {
	this.syncTemplate.setUriTemplateHandler(handler);
}
 
Example #29
Source File: RestTemplate.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Configure a strategy for expanding URI templates.
 * <p>By default, {@link DefaultUriBuilderFactory} is used and for
 * backwards compatibility, the encoding mode is set to
 * {@link EncodingMode#URI_COMPONENT URI_COMPONENT}. As of 5.0.8, prefer
 * using {@link EncodingMode#TEMPLATE_AND_VALUES TEMPLATE_AND_VALUES}.
 * <p><strong>Note:</strong> in 5.0 the switch from
 * {@link org.springframework.web.util.DefaultUriTemplateHandler
 * DefaultUriTemplateHandler} (deprecated in 4.3), as the default to use, to
 * {@link DefaultUriBuilderFactory} brings in a different default for the
 * {@code parsePath} property (switching from false to true).
 * @param handler the URI template handler to use
 */
public void setUriTemplateHandler(UriTemplateHandler handler) {
	Assert.notNull(handler, "UriTemplateHandler must not be null");
	this.uriTemplateHandler = handler;
}