gpt4 book ai didi

java - 使用 RestTemplate 时如何映射 ResponseEntity 中的响应对象?

转载 作者:行者123 更新时间:2023-12-01 18:50:27 24 4
gpt4 key购买 nike

假设我有两个微服务 MS1 和 MS2,我正在使用 RestClient 从 MS1 向 MS2 进行调用。

MS1 中的代码:

public ResponseEntity<ResponeTypeOne> saveResponse(List<Review> request) {
String url = getReviewRestServiceEndPoint() + "/ms2/save/";
return restTemplate.postForEntity(url, request, ResponeTypeOne.class);
}

MS2中的代码:从MS2,我想发送不同的自定义错误对象(仅供引用:以防出现异常)

@ExceptionHandler(MyCustomException.class)
public ResponseEntity<ErrorOutputMessage> handleException(MyCustomException ex){
return new ResponseEntity<>(new ErrorOutputMessage(ex.getCode(),ex.getMessage()),HttpStatus.BAD_REQUEST);
}

谁能告诉我在以下情况下如何进行 ResponeTypeOneErrorOutputMessage 的映射:

  1. When fields in both objects are same.
  2. When fields in both objects are different.

最佳答案

RestTemplate 具有 DefaultResponseErrorHandler,它会在出现客户端/服务器错误(不成功状态代码)时引发异常。

关于java - 使用 RestTemplate 时如何映射 ResponseEntity 中的响应对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59745952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com