1

I have a spring boot application implementing a REST API. I have a POST endpoint which receives an object through @RequestBody, this object has several fields, some of type Long among them. The problem I'm facing is that when I receive an invalid request payload containing an alphabetic string as the value for a field of type long, the application is returning an HTTP 400 response with an empty payload, but I'd like to be able to customize that response (eg. via @ControllerAdvice) and provide some error description. However, I haven't managed to do it until now.

Request payload object:

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 class:

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

Sample request:

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"
}'

And its response:

< HTTP/1.1 400
< Content-Length: 0
< Date: Mon, 11 Mar 2019 16:53:40 GMT
< Connection: close

Any idea?

1 Answer 1

4

You are already extending ResponseEntityExceptionHandler so all you need is just to @Override handleHttpMessageNotReadable method:

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

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.