gpt4 book ai didi

java - Spring RestTemplate : Overridding ResponseErrorHandler not working when set in Configuration class

转载 作者:太空宇宙 更新时间:2023-11-04 10:29:28 24 4
gpt4 key购买 nike

我有 RestTemplate,我正在重写 ResponseErrorHandler,如下面的代码所示

@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
restTemplate.setErrorHandler(new RsponseError());
return restTemplate;
}

我的 ResponseError 类是这样的

public class RsponseError implements ResponseErrorHandler{

@Override
public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
log.info("statuscode {}",clientHttpResponse.getStatusCode());
return true;
}

@Override
public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {

log.info("status code {}",clientHttpResponse.getStatusCode());
log.info("status text {}",clientHttpResponse.getStatusText());

}

如果我 Autowiring RestTemplate 并调用休息服务并生成错误,则 ResponseError 不起作用。但是,如果我不在配置中设置错误处理程序,并在 Autowiring RestTemplate 后设置它,如下所示的类,它将起作用。

@Autowired
private RestTemplate restTemplate;


@Override
public ResponseEntity<ClassExampleResponse> invoker(ClassExample classExample,
String uri) throws Exception {
final RequestEntity<ClassExample> requestEntity = new RequestEntity<>(classExample, requestHeader(), HttpMethod.POST, new URI("https://somelink"));
restTemplate.setErrorHandler(new RsponseError());
final ResponseEntity<ClassExampleResponse> responseEntity = restTemplate.exchange(requestEntity, ClassExampleResponse.class);
return responseEntity;
}

为什么是restTemplate.setErrorHandler(new RsponseError());在配置类中设置时不起作用?感谢您提前提供帮助。

最佳答案

您可以使用@ControllerAdvice让您的所有问题消失!

这是一个简短的示例,应该可以解决您的问题。它获取 bean 验证的输出并将其显示为 JSON。

你可以看一下我之前的一个回答。这将涵盖其工作原理的一些细节。 Bean validation in DropWizard with custom Json output

@ExceptionHandler(MethodArgumentNotValidException.class)
@Order(1)
public ResponseEntity<ValidationErrors> handleUnexpectedException(MethodArgumentNotValidException errors) {

ValidationErrors validationErrors = new ValidationErrors();

for (FieldError fe : errors.getBindingResult().getFieldErrors()) {

String field = fe.getField();
String msg = fe.getDefaultMessage();

validationErrors.add(field, msg);
}
return new ResponseEntity<ValidationErrors>(validationErrors, HttpStatus.UNPROCESSABLE_ENTITY);
}

public class ValidationErrors {

public List<ValidationObject> errors = new ArrayList<ValidationObject>();

public void add(String field, String error){
errors.add(new ValidationObject(field, error));
}
}


public class ValidationObject {

private String field, error;

public ValidationObject(String field, String error){
this.field = field;
this.error = error;
}

public String getField() {
return field;
}

public void setField(String field) {
this.field = field;
}

public String getError() {
return error;
}

public void setError(String error) {
this.error = error;
}
}

关于java - Spring RestTemplate : Overridding ResponseErrorHandler not working when set in Configuration class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50181289/

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