我有一个实现 REST API 的 Spring Boot 应用程序。我有一个 POST 端点,它通过 @RequestBody
接收一个对象,该对象有几个字段,其中一些字段的类型为 Long
。我面临的问题是,当我收到包含字母字符串作为 long 类型字段的值的无效请求负载时,应用程序将返回带有空负载的 HTTP 400 响应,但我希望能够自定义该响应(例如通过@ControllerAdvice)并提供一些错误描述。然而,直到现在我还没有做到。
请求负载对象:
public final class ExchangeRateDTO {
public final Long provider;
public final String from;
public final String to;
public final BigDecimal amount;
public final String date;
public ExchangeRateDTO(Long provider, String from, String to, BigDecimal amount, String date) {
this.provider = provider;
this.from = from;
this.to = to;
this.amount = amount;
this.date = date;
}
}
Controller :
@RestController
@RequestMapping("/v1/exchangerate")
public class ExchangeRateController {
private CommandBus commandBus;
@Autowired
public ExchangeRateController(CommandBus commandBus) {
this.commandBus = commandBus;
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
@Loggable(operationName="AddExchangeRateRequest")
public void create(@RequestBody ExchangeRateDTO exchangeRate) {
commandBus.dispatch(new AddExchangeRateCommand(exchangeRate.provider, exchangeRate.from, exchangeRate.to, exchangeRate.amount, exchangeRate.date));
}
}
ControllerAdvice 类:
@RestControllerAdvice
public class ExchangeRateStoreExceptionHandler extends ResponseEntityExceptionHandler {
private ErrorResponseAdapter errorResponseAdapter;
private ErrorStatusAdapter errorStatusAdapter;
public ExchangeRateStoreExceptionHandler() {
this.errorResponseAdapter = new ErrorResponseAdapter();
this.errorStatusAdapter = new ErrorStatusAdapter();
}
@ExceptionHandler({ValidationError.class})
protected ResponseEntity<ValidationErrorResponse> handleValidationError(ValidationError error) {
ValidationErrorResponse errorResponse = errorResponseAdapter.fromValidationError(error);
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler({DomainError.class})
protected ResponseEntity<ErrorResponse> handleDomainError(DomainError error) {
ErrorResponse errorResponse = errorResponseAdapter.fromDomainError(error);
HttpStatus errorStatus = errorStatusAdapter.fromDomainError(error);
return new ResponseEntity<>(errorResponse, errorStatus);
}
@ExceptionHandler({Exception.class})
protected ResponseEntity<ErrorResponse> handleAllOtherExceptions(Exception exception) {
String message = "There was an unexpected error. Please retry later.";
ErrorResponse errorResponse = new ErrorResponse(INTERNAL.toString(), message);
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
示例请求:
curl -vX POST http://localhost:8081/v1/exchangerate \
-H 'Content-Type: application/json' \
-d '{
"provider": 1,
"from": "USD",
"to": "EUR",
"amount": "as",
"date": "2018-11-22T00:00:00Z"
}'
及其响应:
< HTTP/1.1 400
< Content-Length: 0
< Date: Mon, 11 Mar 2019 16:53:40 GMT
< Connection: close
有什么想法吗?
最佳答案
您已经在扩展 ResponseEntityExceptionHandler所以你所需要的只是 @Override
handleHttpMessageNotReadable 方法:
@ControllerAdvice
public class ExchangeRateStoreExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
return new ResponseEntity<>(ex.getLocalizedMessage(), status);
}
}
关于java - Spring Boot 对无效请求的自定义错误响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55106844/