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

The following examples show how to use org.springframework.web.client.resttemplate#setErrorHandler() . 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: DCTMJacksonClient.java    From documentum-rest-client-java with Apache License 2.0 6 votes vote down vote up
@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
Source File: DCTMJaxbClient.java    From documentum-rest-client-java with Apache License 2.0 5 votes vote down vote up
@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 3
Source File: RestUtil.java    From cf-java-client-sap with 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 4
Source File: DefaultSkipperClientTests.java    From spring-cloud-skipper with Apache License 2.0 5 votes vote down vote up
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 5
Source File: DefaultSkipperClientTests.java    From spring-cloud-skipper with Apache License 2.0 5 votes vote down vote up
@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 6
Source File: SimpleRestTemplateBuilder.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
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 7
Source File: BaseRepositoryTest.java    From kurento-java with 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 8
Source File: WechatOAuth2Template.java    From spring-social-wechat with Apache License 2.0 5 votes vote down vote up
@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 9
Source File: RestClientTemplateTest.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
@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 10
Source File: ErrorHandlerIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 11
Source File: ErrorHandlerIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 12
Source File: RestClientTemplateTest.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
@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 13
Source File: RestUtil.java    From XPagesExtensionLibrary with 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 14
Source File: EchoCloudConfig.java    From Milkomeda with 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 15
Source File: ErrorHandlerIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 16
Source File: CustomRemoteTokenServices.java    From microservice-integration with MIT License 5 votes vote down vote up
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 17
Source File: App.java    From cerebro with 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
Source File: DefaultJwtBearerTokenResponseClient.java    From oauth2-protocol-patterns with Apache License 2.0 4 votes vote down vote up
public DefaultJwtBearerTokenResponseClient() {
	RestTemplate restTemplate = new RestTemplate(Arrays.asList(
			new FormHttpMessageConverter(), new OAuth2AccessTokenResponseHttpMessageConverter()));
	restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
	this.restOperations = restTemplate;
}
 
Example 19
Source File: WechatImpl.java    From spring-social-wechat with 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
Source File: Yahoo2Template.java    From cloudstreetmarket.com with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void configureRestTemplate(RestTemplate restTemplate) {
    restTemplate.setErrorHandler(errorHandler());
}