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

The following examples show how to use org.springframework.web.client.RestTemplate#setErrorHandler() .
Example 1
Project: documentum-rest-client-java   File: DCTMJacksonClient.java    License: 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
Project: sinavi-jfw   File: RestClientTemplateTest.java    License: 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 3
Project: microservice-integration   File: CustomRemoteTokenServices.java    License: 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 4
Project: spring-analysis-note   File: ErrorHandlerIntegrationTests.java    License: 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 5
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
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
Project: sinavi-jfw   File: RestClientTemplateTest.java    License: 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 8
Project: java-technology-stack   File: ErrorHandlerIntegrationTests.java    License: 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 9
Project: java-technology-stack   File: ErrorHandlerIntegrationTests.java    License: 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 10
Project: documentum-rest-client-java   File: DCTMJaxbClient.java    License: 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 11
Project: spring-social-wechat   File: WechatOAuth2Template.java    License: 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 12
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
Project: jeesuite-libs   File: SimpleRestTemplateBuilder.java    License: 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 14
Project: spring-cloud-skipper   File: DefaultSkipperClientTests.java    License: 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 15
Project: spring-cloud-skipper   File: DefaultSkipperClientTests.java    License: 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 16
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
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
Project: oauth2-protocol-patterns   File: DefaultJwtBearerTokenResponseClient.java    License: 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
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
Project: cloudstreetmarket.com   File: Yahoo2Template.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void configureRestTemplate(RestTemplate restTemplate) {
    restTemplate.setErrorHandler(errorHandler());
}