下面列出了org.springframework.http.client.AsyncClientHttpRequestInterceptor#org.springframework.web.util.UriTemplateHandler 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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.");
}
}
/**
* 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.");
}
}
@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");
}
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);
}
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);
}
@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);
}
@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);
}
@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");
}
/**
* Return the configured URI template handler.
*/
public UriTemplateHandler getUriTemplateHandler() {
return this.uriTemplateHandler;
}
/**
* Return the configured URI template handler.
*/
public UriTemplateHandler getUriTemplateHandler() {
return this.syncTemplate.getUriTemplateHandler();
}
/**
* Return the configured URI template handler.
*/
public UriTemplateHandler getUriTemplateHandler() {
return this.uriTemplateHandler;
}
/**
* Return the configured URI template handler.
*/
public UriTemplateHandler getUriTemplateHandler() {
return this.syncTemplate.getUriTemplateHandler();
}
@Override
public void setUriTemplateHandler(UriTemplateHandler handler) {
super.setUriTemplateHandler(handler);
defaultRestTemplate.setUriTemplateHandler(handler);
}
/**
* Return the configured URI template handler.
*/
public UriTemplateHandler getUriTemplateHandler() {
return this.uriTemplateHandler;
}
/**
* Return the configured URI template handler.
*/
public UriTemplateHandler getUriTemplateHandler() {
return this.syncTemplate.getUriTemplateHandler();
}
public void setUriTemplateHandler(UriTemplateHandler handler) {
restTemplate.setUriTemplateHandler(handler);
}
public UriTemplateHandler getUriTemplateHandler() {
return restTemplate.getUriTemplateHandler();
}
/**
* Return the configured URI template handler.
*/
public UriTemplateHandler getUriTemplateHandler() {
return this.uriTemplateHandler;
}
/**
* Return the configured URI template handler.
*/
public UriTemplateHandler getUriTemplateHandler() {
return this.syncTemplate.getUriTemplateHandler();
}
public CustomRestTemplate(Gson gson, UriTemplateHandler uriTemplateHandler) {
this(gson, uriTemplateHandler, null);
}
/**
* 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);
}
/**
* 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;
}
/**
* 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);
}
/**
* 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;
}
/**
* 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);
}
/**
* 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;
}
/**
* 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);
}
/**
* 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;
}
/**
* 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);
}