org.springframework.web.client.RestTemplate.setErrorHandler()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(169)

本文整理了Java中org.springframework.web.client.RestTemplate.setErrorHandler方法的一些代码示例,展示了RestTemplate.setErrorHandler的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RestTemplate.setErrorHandler方法的具体详情如下:
包路径:org.springframework.web.client.RestTemplate
类名称:RestTemplate
方法名:setErrorHandler

RestTemplate.setErrorHandler介绍

[英]Set the error handler.

By default, RestTemplate uses a DefaultResponseErrorHandler.
[中]

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Set the error handler.
 * <p>By default, AsyncRestTemplate uses a
 * {@link org.springframework.web.client.DefaultResponseErrorHandler}.
 */
public void setErrorHandler(ResponseErrorHandler errorHandler) {
  this.syncTemplate.setErrorHandler(errorHandler);
}

代码示例来源:origin: org.springframework/spring-web

/**
 * Set the error handler.
 * <p>By default, AsyncRestTemplate uses a
 * {@link org.springframework.web.client.DefaultResponseErrorHandler}.
 */
public void setErrorHandler(ResponseErrorHandler errorHandler) {
  this.syncTemplate.setErrorHandler(errorHandler);
}

代码示例来源:origin: spring-projects/spring-security-oauth

public RemoteTokenServices() {
  restTemplate = new RestTemplate();
  ((RestTemplate) restTemplate).setErrorHandler(new DefaultResponseErrorHandler() {
    @Override
    // Ignore 400
    public void handleError(ClientHttpResponse response) throws IOException {
      if (response.getRawStatusCode() != 400) {
        super.handleError(response);
      }
    }
  });
}

代码示例来源:origin: cloudfoundry/uaa

public RemoteTokenServices() {
  restTemplate = new RestTemplate();
  ((RestTemplate) restTemplate).setErrorHandler(new DefaultResponseErrorHandler() {
    @Override
    // Ignore 400
    public void handleError(ClientHttpResponse response) throws IOException {
      if (response.getRawStatusCode() != 400) {
        super.handleError(response);
      }
    }
  });
}

代码示例来源:origin: spring-projects/spring-security

public DefaultOAuth2UserService() {
  RestTemplate restTemplate = new RestTemplate();
  restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
  this.restOperations = restTemplate;
}

代码示例来源:origin: spring-projects/spring-security-oauth

@Override
public void setErrorHandler(ResponseErrorHandler errorHandler) {
  if (!(errorHandler instanceof OAuth2ErrorHandler)) {
    errorHandler = new OAuth2ErrorHandler(errorHandler, resource);
  }
  super.setErrorHandler(errorHandler);
}

代码示例来源:origin: spring-projects/spring-security

/**
 * Constructs a {@code CustomUserTypesOAuth2UserService} using the provided parameters.
 *
 * @param customUserTypes a {@code Map} of {@link OAuth2User} type(s) keyed by {@link ClientRegistration#getRegistrationId() Registration Id}
 */
public CustomUserTypesOAuth2UserService(Map<String, Class<? extends OAuth2User>> customUserTypes) {
  Assert.notEmpty(customUserTypes, "customUserTypes cannot be empty");
  this.customUserTypes = Collections.unmodifiableMap(new LinkedHashMap<>(customUserTypes));
  RestTemplate restTemplate = new RestTemplate();
  restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
  this.restOperations = restTemplate;
}

代码示例来源:origin: prontera/spring-cloud-rest-tcc

@LoadBalanced
@Bean
public RestTemplate restTemplate() {
  final RestTemplate template = new RestTemplate();
  template.setErrorHandler(new TccResponseErrorHandler());
  return template;
}

代码示例来源:origin: cloudfoundry/uaa

public RestAuthenticationManager() {
  RestTemplate restTemplate = new RestTemplate();
  // The default java.net client doesn't allow you to handle 4xx responses
  restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
  restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
    @Override
    protected boolean hasError(HttpStatus statusCode) {
      return statusCode.series() == HttpStatus.Series.SERVER_ERROR;
    }
  });
  this.restTemplate = restTemplate;
}

代码示例来源:origin: spring-io/initializr

@Bean
public RestTemplateCustomizer testRestTemplateCustomizer() {
  return (b) -> b.setErrorHandler(errorHandler);
}

代码示例来源:origin: spring-projects/spring-framework

@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());
}

代码示例来源:origin: spring-projects/spring-framework

@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());
}

代码示例来源:origin: spring-projects/spring-framework

@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());
}

代码示例来源:origin: spring-projects/spring-security

public DefaultAuthorizationCodeTokenResponseClient() {
  RestTemplate restTemplate = new RestTemplate(Arrays.asList(
      new FormHttpMessageConverter(), new OAuth2AccessTokenResponseHttpMessageConverter()));
  restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
  this.restOperations = restTemplate;
}

代码示例来源:origin: spring-projects/spring-security

public DefaultClientCredentialsTokenResponseClient() {
  RestTemplate restTemplate = new RestTemplate(Arrays.asList(
      new FormHttpMessageConverter(), new OAuth2AccessTokenResponseHttpMessageConverter()));
  restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
  this.restOperations = restTemplate;
}

代码示例来源:origin: spring-projects/spring-security-oauth

protected RestOperations getRestTemplate() {
  if (restTemplate == null) {
    synchronized (this) {
      if (restTemplate == null) {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setErrorHandler(getResponseErrorHandler());
        restTemplate.setRequestFactory(requestFactory);
        restTemplate.setInterceptors(interceptors);
        this.restTemplate = restTemplate;
      }
    }
  }
  if (messageConverters == null) {
    setMessageConverters(new RestTemplate().getMessageConverters());
  }
  return restTemplate;
}

代码示例来源:origin: spring-projects/spring-framework

@Before
public void setup() {
  requestFactory = mock(ClientHttpRequestFactory.class);
  request = mock(ClientHttpRequest.class);
  response = mock(ClientHttpResponse.class);
  errorHandler = mock(ResponseErrorHandler.class);
  converter = mock(HttpMessageConverter.class);
  template = new RestTemplate(Collections.singletonList(converter));
  template.setRequestFactory(requestFactory);
  template.setErrorHandler(errorHandler);
}

代码示例来源:origin: cloudfoundry/uaa

@Before
public void setErrorHandlerOnRestTemplate() {
  ((RestTemplate)serverRunning.getRestTemplate()).setErrorHandler(new OAuth2ErrorHandler(context.getResource()) {
    // Pass errors through in response entity for status code analysis
    @Override
    public boolean hasError(ClientHttpResponse response) throws IOException {
      return false;
    }
    @Override
    public void handleError(ClientHttpResponse response) throws IOException {
    }
  });
}

代码示例来源:origin: cloudfoundry/uaa

@Before
public void setUp() throws Exception {
  ((RestTemplate)serverRunning.getRestTemplate()).setErrorHandler(
    new UaaOauth2ErrorHandler(context.getResource(), HttpStatus.Series.SERVER_ERROR)
  );
}

代码示例来源:origin: cloudfoundry/uaa

@Before
public void createRestTemplate() throws Exception {
  client = serverRunning.getRestTemplate();
  ((RestTemplate)serverRunning.getRestTemplate()).setErrorHandler(new OAuth2ErrorHandler(context.getResource()) {
    // Pass errors through in response entity for status code analysis
    @Override
    public boolean hasError(ClientHttpResponse response) throws IOException {
      return false;
    }
    @Override
    public void handleError(ClientHttpResponse response) throws IOException {
    }
  });
}

相关文章