org.springframework.web.client.DefaultResponseErrorHandler#org.springframework.web.util.DefaultUriTemplateHandler源码实例Demo

下面列出了org.springframework.web.client.DefaultResponseErrorHandler#org.springframework.web.util.DefaultUriTemplateHandler 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@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");
}
 
源代码2 项目: spring4-understanding   文件: RestTemplateTests.java
@Test
public void getForObjectWithCustomUriTemplateHandler() throws Exception {

	DefaultUriTemplateHandler uriTemplateHandler = new DefaultUriTemplateHandler();
	uriTemplateHandler.setParsePath(true);
	template.setUriTemplateHandler(uriTemplateHandler);

	URI expectedUri = new URI("http://example.com/hotels/1/pic/pics%2Flogo.png/size/150x150");
	given(requestFactory.createRequest(expectedUri, HttpMethod.GET)).willReturn(request);

	given(request.getHeaders()).willReturn(new HttpHeaders());
	given(request.execute()).willReturn(response);
	given(errorHandler.hasError(response)).willReturn(false);

	given(response.getStatusCode()).willReturn(HttpStatus.OK);
	given(response.getHeaders()).willReturn(new HttpHeaders());
	given(response.getBody()).willReturn(null);

	Map<String, String> uriVariables = new HashMap<String, String>(2);
	uriVariables.put("hotel", "1");
	uriVariables.put("publicpath", "pics/logo.png");
	uriVariables.put("scale", "150x150");

	String url = "http://example.com/hotels/{hotel}/pic/{publicpath}/size/{scale}";
	template.getForObject(url, String.class, uriVariables);

	verify(response).close();
}
 
源代码3 项目: mojito   文件: SmartlingClientConfiguration.java
public OAuth2RestTemplate smartlingRestTemplate() {
    OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(smartling(), new DefaultOAuth2ClientContext());

    RestTemplateUtils restTemplateUtils = new RestTemplateUtils();
    restTemplateUtils.enableFeature(oAuth2RestTemplate, DeserializationFeature.UNWRAP_ROOT_VALUE);

    AccessTokenProviderChain accessTokenProviderChain = new AccessTokenProviderChain(Arrays.asList(
            new SmartlingAuthorizationCodeAccessTokenProvider())
    );
    oAuth2RestTemplate.setAccessTokenProvider(accessTokenProviderChain);

    DefaultUriTemplateHandler defaultUriTemplateHandler = new DefaultUriTemplateHandler();
    defaultUriTemplateHandler.setBaseUrl(baseUri);

    oAuth2RestTemplate.setUriTemplateHandler(defaultUriTemplateHandler);

    oAuth2RestTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
        @Override
        public void handleError(ClientHttpResponse response) throws IOException {
            try {
                super.handleError(response);
            } catch (HttpClientErrorException e) {
                if (resttemplateLogger.isDebugEnabled()) {
                    resttemplateLogger.debug(e.getResponseBodyAsString());
                }
                throw e;
            }
        }
    });

    return oAuth2RestTemplate;
}
 
源代码4 项目: mojito   文件: EvolveConfiguration.java
RestTemplate evolveRestTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    DefaultUriTemplateHandler defaultUriTemplateHandler = new DefaultUriTemplateHandler();
    defaultUriTemplateHandler.setBaseUrl(evolveConfigurationProperties.getUrl());
    restTemplate.setUriTemplateHandler(defaultUriTemplateHandler);

    restTemplate.getInterceptors().add(new ClientHttpRequestInterceptor() {
        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            request.getHeaders().add(HttpHeaders.AUTHORIZATION, evolveConfigurationProperties.getToken());
            return execution.execute(request, body);
        }
    });

    restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
        @Override
        public void handleError(ClientHttpResponse response) throws IOException {
            try {
                super.handleError(response);
            } catch (HttpServerErrorException | HttpClientErrorException e) {
                resttemplateLogger.debug(e.getResponseBodyAsString());
                throw e;
            }
        }
    });

    return restTemplate;
}
 
@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");
}