Java Code Examples for org.springframework.web.client.RestTemplate#setErrorHandler()

The following examples show how to use org.springframework.web.client.RestTemplate#setErrorHandler() . 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
@SuppressWarnings("unchecked")
@Override
protected void initRestTemplate(RestTemplate restTemplate) {
    super.initRestTemplate(restTemplate);
    restTemplate.setErrorHandler(new DCTMJacksonErrorHandler(restTemplate.getMessageConverters()));
    for(HttpMessageConverter<?> c : restTemplate.getMessageConverters()) {
        if(c instanceof MappingJackson2HttpMessageConverter) {
            ((MappingJackson2HttpMessageConverter)c).getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        } else if(c instanceof FormHttpMessageConverter) {
            try {
                Field pcField = FormHttpMessageConverter.class.getDeclaredField("partConverters");
                pcField.setAccessible(true);
                List<HttpMessageConverter<?>> partConverters = ((List<HttpMessageConverter<?>>)pcField.get(c));
                for(HttpMessageConverter<?> pc : partConverters) {
                    if(pc instanceof MappingJackson2HttpMessageConverter) {
                        ((MappingJackson2HttpMessageConverter)pc).getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
                        break;
                    }
                }
            } catch (Exception e) {
                throw new IllegalStateException(e);
            }
        }
    }
}
 
Example 2
@Test
public void IntenalServerエラーの500が返ってくる() {
    thrown.expect(InternalServerErrorException.class);
    RestTemplate rt = new RestTemplate();
    rt.setErrorHandler(new RestClientResponseErrorHandler());
    template.setDelegate(rt);
    template.get(Target.target(baseUrl + "/status/server"), String.class);
}
 
Example 3
public CustomRemoteTokenServices(RestTemplate restTemplate) {
    this.restTemplate = restTemplate;
    restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
        @Override
        // Ignore 400
        public void handleError(ClientHttpResponse response) throws IOException {
            if (response.getRawStatusCode() != 400) {
                super.handleError(response);
            }
        }
    });
}
 
Example 4
@Test // SPR-15560
public void emptyPathSegments() throws Exception {

	RestTemplate restTemplate = new RestTemplate();
	restTemplate.setErrorHandler(NO_OP_ERROR_HANDLER);

	URI url = new URI("http://localhost:" + port + "//");
	ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);

	assertEquals(HttpStatus.OK, response.getStatusCode());
}
 
Example 5
Source Project: Milkomeda   File: EchoCloudConfig.java    License: MIT License 5 votes vote down vote up
@LoadBalanced
@Bean("echoCloudRestTemplate")
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public RestTemplate simpleRestTemplate(RestTemplateBuilder builder, ClientHttpRequestFactory factory) {
    RestTemplate restTemplate = builder.build();
    restTemplate.setRequestFactory(factory);
    restTemplate.setErrorHandler(new EchoResponseErrorHandler());
    return restTemplate;
}
 
Example 6
Source Project: XPagesExtensionLibrary   File: RestUtil.java    License: Apache License 2.0 5 votes vote down vote up
public RestTemplate createRestTemplate(HttpProxyConfiguration httpProxyConfiguration, boolean trustSelfSignedCerts) {
	RestTemplate restTemplate = new LoggingRestTemplate();
	restTemplate.setRequestFactory(createRequestFactory(httpProxyConfiguration, trustSelfSignedCerts));
	restTemplate.setErrorHandler(new CloudControllerResponseErrorHandler());
	restTemplate.setMessageConverters(getHttpMessageConverters());

	return restTemplate;
}
 
Example 7
@Test
public void URLが見つからないときに404が返ってくる() {
    thrown.expect(NotFoundException.class);
    RestTemplate rt = new RestTemplate();
    rt.setErrorHandler(new RestClientResponseErrorHandler());
    template.setDelegate(rt);
    template.get(Target.target(baseUrl + "/status/notfound"), String.class);
}
 
Example 8
@Test
public void responseBodyError() throws Exception {
	RestTemplate restTemplate = new RestTemplate();
	restTemplate.setErrorHandler(NO_OP_ERROR_HANDLER);

	URI url = new URI("http://localhost:" + port + "/response-body-error");
	ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);

	assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
}
 
Example 9
@Test
public void handlingError() throws Exception {
	RestTemplate restTemplate = new RestTemplate();
	restTemplate.setErrorHandler(NO_OP_ERROR_HANDLER);

	URI url = new URI("http://localhost:" + port + "/handling-error");
	ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);

	assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
}
 
Example 10
@Override
protected void initRestTemplate(RestTemplate restTemplate) {
    super.initRestTemplate(restTemplate);
    restTemplate.setErrorHandler(new DCTMJaxbErrorHandler(restTemplate.getMessageConverters()));
    for(HttpMessageConverter<?> c : restTemplate.getMessageConverters()) {
        if(c instanceof FormHttpMessageConverter) {
            ((FormHttpMessageConverter)c).addPartConverter(new Jaxb2RootElementHttpMessageConverter());
            break;
        }
    }
}
 
Example 11
@Override
protected RestTemplate createRestTemplate() {
	RestTemplate restTemplate = super.createRestTemplate();
	List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(3);
	converters.add(new FormHttpMessageConverter());
	converters.add(new FormMapHttpMessageConverter());
	converters.add(new WechatMappingJackson2HttpMessageConverter());
	restTemplate.setMessageConverters(converters);
	restTemplate.setErrorHandler(new WechatErrorHandler());
	return restTemplate;
}
 
Example 12
Source Project: kurento-java   File: BaseRepositoryTest.java    License: Apache License 2.0 5 votes vote down vote up
protected RestTemplate getRestTemplate() {
  RestTemplate restTemplate = new RestTemplate();
  restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
    @Override
    public void handleError(ClientHttpResponse response) throws IOException {
      log.error(response.getStatusText());
    }
  });
  return restTemplate;
}
 
Example 13
public RestTemplate build(int readTimeout){
	
	HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();  
       factory.setReadTimeout(readTimeout);//ms  
       factory.setConnectTimeout(3000);//ms 
       
       RestTemplate restTemplate = new RestTemplate(factory);
       List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
       interceptors.add(new RestTemplateAutoHeaderInterceptor());
       interceptors.add(new LoggingRequestInterceptor());
       restTemplate.setInterceptors(interceptors);
       //
       restTemplate.setErrorHandler(new CustomResponseErrorHandler());
	return restTemplate;
}
 
Example 14
@Test(expected = SkipperException.class)
public void testSkipperException() {
	RestTemplate restTemplate = new RestTemplate();
	restTemplate.setErrorHandler(new SkipperClientResponseErrorHandler(new ObjectMapper()));
	SkipperClient skipperClient = new DefaultSkipperClient("", restTemplate);

	MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate).build();
	mockServer.expect(requestTo("/release/status/mylog"))
			.andRespond(withStatus(HttpStatus.NOT_FOUND).body(ERROR2).contentType(MediaType.APPLICATION_JSON));

	skipperClient.status("mylog");
}
 
Example 15
private void testDeleteRelease(boolean deletePackage) {
	RestTemplate restTemplate = new RestTemplate();
	restTemplate.setErrorHandler(new SkipperClientResponseErrorHandler(new ObjectMapper()));
	SkipperClient skipperClient = new DefaultSkipperClient("", restTemplate);

	final MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
			MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));

	MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate).build();
	mockServer.expect(requestTo("/release/release1" + (deletePackage ? "/package" : "")))
			.andRespond(withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON));

	skipperClient.delete("release1", deletePackage);
}
 
Example 16
Source Project: cf-java-client-sap   File: RestUtil.java    License: Apache License 2.0 5 votes vote down vote up
public RestTemplate createRestTemplate(HttpProxyConfiguration httpProxyConfiguration, boolean trustSelfSignedCerts) {
    RestTemplate restTemplate = new LoggingRestTemplate();
    restTemplate.setRequestFactory(createRequestFactory(httpProxyConfiguration, trustSelfSignedCerts));
    restTemplate.setErrorHandler(new CloudControllerResponseErrorHandler());
    restTemplate.setMessageConverters(getHttpMessageConverters());

    return restTemplate;
}
 
Example 17
Source Project: cerebro   File: App.java    License: GNU Affero General Public License v3.0 4 votes vote down vote up
@Bean
public RestTemplate restTemplate() {
    RestTemplate restClient = new RestTemplate();
    restClient.setErrorHandler(new SeyrenResponseErrorHandler());
    return restClient;
}
 
Example 18
public DefaultJwtBearerTokenResponseClient() {
	RestTemplate restTemplate = new RestTemplate(Arrays.asList(
			new FormHttpMessageConverter(), new OAuth2AccessTokenResponseHttpMessageConverter()));
	restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
	this.restOperations = restTemplate;
}
 
Example 19
Source Project: spring-social-wechat   File: WechatImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected void configureRestTemplate(RestTemplate restTemplate) {
	restTemplate.setErrorHandler(new WechatErrorHandler());
	super.configureRestTemplate(restTemplate);
}
 
Example 20
@Override
protected void configureRestTemplate(RestTemplate restTemplate) {
    restTemplate.setErrorHandler(errorHandler());
}