Spring Boot Rest API - Неизвестное поле - Настройка сообщений об ошибках - PullRequest
0 голосов
/ 16 октября 2018

У нас есть rest api на основе весенней загрузки, который принимает http-запросы от нескольких потребителей в json.Он не может принимать запросы с неизвестными полями и должен дать ответ, который говорит, что это неверный запрос и содержательно описывает ошибку.Однако по соображениям безопасности нам нужно лишь предоставить им достаточно информации об ошибках .

Вот что у нас есть:

Чтобы достичь этого, вот что у нас естьсделано до сих пор:

Файл свойств приложения имеет следующее:
spring.jackson.deserialization.fail-on-unknown-properties=true

Обработка исключений была настроена примерно так (другие функции для краткости опущены):

@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class RestExceptionHandler extends ResponseEntityExceptionHandler {

    Logger logger = LoggerFactory.getLogger(RestExceptionHandler.class);

    @Override
    public ResponseEntity<Object> handleHttpMessageNotReadable(
            HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

        //this message can't give them info about known properties
        exceptionMessage = ex.getLocalizedMessage();
        logger.debug("exceptionMessage: " + ex.getLocalizedMessage());

        //ApiError is a custom object to encapsulate the information to be sent in the api response.
        ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, "HTTP message not readable", exceptionMessage);
        apiError.setHttpStatus(HttpStatus.BAD_REQUEST);
        apiError.setErrorMessage(errorMessage);

        return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getHttpStatus());
    }
}

Запрос json, содержащий unknown-field, приведет к следующему исключению:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "unknown-field" (class mypackage.MyDomain), not marked as ignorable (2 known properties: "known-field-1", "known-field-2"])

Мы не хотим предоставлять подробности об известных свойствах по соображениям безопасности (2 known properties: "known-field-1", "known-field-2"]).

Тело запроса:

{"known-field-1": 1, "unknown-field": 2}

Фактическое тело ответа:

{"status":"BAD_REQUEST","message":"HTTP message not readable","errors":[Unrecognized field "unknown-field" (class mypackage.MyDomain), not marked as ignorable (2 known properties: "known-field-1", "known-field-2"]}

Желаемое тело ответа:

{"status":"BAD_REQUEST","message":"HTTP message not readable","errors":["Unknown field: unknown-field"]}

Как дополнительно настроить это легко?

1 Ответ

0 голосов
/ 16 октября 2018

Это решило это:

@Override
public ResponseEntity<Object> handleHttpMessageNotReadable(
        HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

    String exceptionMessage = null;

    Throwable rootCause = ex.getRootCause();
    if(rootCause instanceof UnrecognizedPropertyException)
    {
        exceptionMessage = "Unknown field: " + ((UnrecognizedPropertyException) rootCause).getPropertyName();
        logger.debug("exceptionMessage: " + exceptionMessage);
    }

    ApiError apiError = 
      new ApiError(HttpStatus.BAD_REQUEST, "HTTP message not readable", exceptionMessage);

    return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
}
...