Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[http] RestTemplate usage #118

Open
Alice52 opened this issue May 31, 2020 · 1 comment
Open

[http] RestTemplate usage #118

Alice52 opened this issue May 31, 2020 · 1 comment
Assignees
Labels
Common Http Java java implement important than extend TODO Tool language: tool issue

Comments

@Alice52
Copy link
Owner

Alice52 commented May 31, 2020

usage

  1. custom error handler: rest template default will throw xxException if response status is bot 200
@Component
public class RestTemplateErrorHandle extends DefaultResponseErrorHandler {
  @Override
  public void handleError(ClientHttpResponse response) {}
}
  1. if response is not json, rest template will throw RestClientException

// TODO: so need overwrite ResponseExtractor to get response firstly, but there are issue about InputStream read repeated.

  • RestTemplateProxy
@Component
public class RestTemplateProxy extends RestTemplate {

  @Override
  public <T> ResponseExtractor<ResponseEntity<T>> responseEntityExtractor(Type responseType) {
    return new NoExceptionRestTemplateResponseEntityExtractor<>(
        responseType, getMessageConverters());
  }
}
  • NoExceptionRestTemplateResponseEntityExtractor
public class NoExceptionRestTemplateResponseEntityExtractor<T>
    implements ResponseExtractor<ResponseEntity<T>> {
  private static final Logger log =
      LoggerFactory.getLogger(NoExceptionRestTemplateResponseEntityExtractor.class);
  private final HttpMessageConverterExtractor<T> delegate;

  NoExceptionRestTemplateResponseEntityExtractor(
      Type responseType, List<HttpMessageConverter<?>> converters) {
    if (responseType != null && Void.class != responseType) {
      this.delegate = new HttpMessageConverterExtractor<>(responseType, converters);
    } else {
      this.delegate = null;
    }
  }

  @Override
  public ResponseEntity<T> extractData(ClientHttpResponse response) throws IOException {
    if (this.delegate != null) {
      T body = null;
      try {
        // TODO: read response firstly, then call super method

        /*
        InputStream inputStream = response.getBody();
        BufferedInputStream buffInputStream = new BufferedInputStream(inputStream);
        buffInputStream.mark(inputStream.available() + 1);
        String content = new String(ByteStreams.toByteArray(buffInputStream), Charsets.UTF_8);
        buffInputStream.reset();
        */
        body = this.delegate.extractData(response);
      } catch (RuntimeException e) {
        return new ResponseEntity(null, response.getHeaders(), response.getStatusCode());
      }
      return new ResponseEntity<>(body, response.getHeaders(), response.getStatusCode());
    } else {
      return new ResponseEntity<>(response.getHeaders(), response.getStatusCode());
    }
  }
}
@Alice52 Alice52 added Help Wanted Extra attention is needed Java java implement important than extend Tool language: tool issue TODO Common labels May 31, 2020
@Alice52 Alice52 self-assigned this May 31, 2020
@Alice52 Alice52 added the Http label May 31, 2020
@Alice52
Copy link
Owner Author

Alice52 commented Oct 25, 2020

solution

  1. try to copy this method and add wanted code please.

@Alice52 Alice52 removed the Help Wanted Extra attention is needed label Oct 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Common Http Java java implement important than extend TODO Tool language: tool issue
Projects
None yet
Development

No branches or pull requests

1 participant