【问题标题】:Get HttpServletRequest into Spring ResponseEntityExceptionHandler获取 HttpServletRequest 到 Spring ResponseEntityExceptionHandler
【发布时间】:2018-10-19 15:05:11
【问题描述】:

我有一个使用 Spring Boot 2.0.1 制作的 REST 服务器。

我正在自定义扩展 ResponseEntityExceptionHandler 的异常处理。

这是我的课

@RestControllerAdvice
public class ApplicationExceptionHandler extends ResponseEntityExceptionHandler {
    private Logger log = LogManager.getLogger();

    @Autowired
    private MessageSource messageSource;

    private MessageSourceAccessor messageSourceAccessor = null;

    @PostConstruct
    public void postConstruct() {
        Assert.notNull(messageSource, "MessageSource must not be null!");
        this.messageSourceAccessor = new MessageSourceAccessor(messageSource);
    }

    /**
     * Mapping each constraint name with the corrispondent exception code -> message
     */
    private static Map<String, ExceptionCode> constraintCodeMap = new HashMap<String, ExceptionCode>() {
        private static final long serialVersionUID = -628747907324708275L;
        {
            put("account_username", ExceptionCode.ACCOUNT_DUPLICATE_USERNAME);
            put("paymenttype_code", ExceptionCode.PAYMENTTYPE_DUPLICATE_CODE);
            put("ticketblock_number_type", ExceptionCode.TICKETBLOCK_DUPLICATE_CODE);
            put("ticket_number", ExceptionCode.TICKET_DUPLICATED_CODE);
            put("licenseplate_plate", ExceptionCode.LICENSEPLATE_DUPLICATED);
        }
    };

    @Override
    protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers,
            HttpStatus status, WebRequest request) {
        return response(HttpStatus.BAD_REQUEST, new HttpHeaders(),
                buildGenericError(ex, request, HttpStatus.BAD_REQUEST, LocaleContextHolder.getLocale()));
    }

    @Override
    protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpHeaders headers,
            HttpStatus status, WebRequest request) {
        return response(HttpStatus.BAD_REQUEST, new HttpHeaders(),
                buildGenericError(ex, request, HttpStatus.BAD_REQUEST, LocaleContextHolder.getLocale()));
    }

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

        if (ExceptionUtils.getRootCauseMessage(ex).contains("Duplicate entry")) {
            /**
             * Custom errors and messages for DataIntegrityViolationException checked against the list of indexes names
             */
            return response(HttpStatus.CONFLICT, new HttpHeaders(),
                    buildIntegrityError(ex, request, HttpStatus.CONFLICT, LocaleContextHolder.getLocale()));
        } else {
            return response(HttpStatus.BAD_REQUEST, new HttpHeaders(),
                    buildGenericError(ex, request, HttpStatus.BAD_REQUEST, LocaleContextHolder.getLocale()));
        }
    }

    /**
     * @see {@link RepositoryRestExceptionHandler}
     * 
     * @param ex
     * @param request
     * @param locale
     * @return
     * @throws Exception
     */
    @ExceptionHandler(DataIntegrityViolationException.class)
    public ResponseEntity<?> handleConflictException(DataIntegrityViolationException ex, HttpServletRequest request, Locale locale)
            throws Exception {
        /**
         * Keep the default Exception format for Violation exception @see {@link RepositoryRestExceptionHandler}
         */
        if (ex instanceof RepositoryConstraintViolationException) {
            return response(HttpStatus.BAD_REQUEST, new HttpHeaders(),
                    new RepositoryConstraintViolationExceptionMessage((RepositoryConstraintViolationException) ex, messageSourceAccessor));
        }

        /**
         * Custom errors and messages for DataIntegrityViolationException checked against the list of indexes names
         */
        return response(HttpStatus.CONFLICT, new HttpHeaders(), buildIntegrityError(ex, request, HttpStatus.CONFLICT, locale));
    }

    /**
     * Handle the exception when the file size is bigger than the maximum set in the configuration
     * 
     * @param ex
     * @param request
     * @param locale
     * @return
     * @throws Exception
     */
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ResponseEntity<?> handleFileUpload(MaxUploadSizeExceededException ex, HttpServletRequest request, Locale locale)
            throws Exception {
        log.error(String.format("Received a file too big from %s. Error: %s", AppUtils.getRemoteIp(request),
                ExceptionUtils.getRootCauseMessage(ex)));
        return response(HttpStatus.BAD_REQUEST, new HttpHeaders(),
                buildIntegrityError(ex, request, HttpStatus.BAD_REQUEST, LocaleContextHolder.getLocale()));
    }

    /**
     * Build a JSON integrity error compliant to the standard exception
     * 
     * @param exception
     * @param request
     * @param httpStatus
     * @param message
     * @return
     */
    private JsonException buildIntegrityError(final Throwable exception, final HttpServletRequest request, final HttpStatus httpStatus,
            Locale locale) {
        return buildIntegrityError(exception, request.getRequestURI(), httpStatus, locale);
    }

    private JsonException buildIntegrityError(final Throwable exception, final WebRequest request, final HttpStatus httpStatus,
            Locale locale) {
        return buildIntegrityError(exception, "", httpStatus, locale);
    }

    /**
     * Build a JSON integrity error compliant to the standard exception
     * 
     */
    private JsonException buildIntegrityError(final Throwable exception, String requestUri, final HttpStatus httpStatus, Locale locale) {
        String finalMessage = "";
        String rootMsg = ExceptionUtils.getRootCauseMessage(exception);
        Optional<Map.Entry<String, ExceptionCode>> entry = constraintCodeMap.entrySet().stream()
                .filter((it) -> rootMsg.contains(it.getKey())).findAny();
        if (entry.isPresent()) {
            finalMessage = messageSource.getMessage(entry.get().getValue().getCode(), new Object[] {}, locale);
        } else {
            finalMessage = messageSource.getMessage(ExceptionCode.INTEGRITY_VIOLATION.getCode(), new Object[] { rootMsg }, locale);
        }
        JsonException jsonException = new JsonException();
        jsonException.setError(httpStatus.getReasonPhrase());
        jsonException.setStatus(httpStatus.value());
        jsonException.setException(exception.getClass().getName());
        jsonException.setMessage(finalMessage);
        jsonException.setPath(requestUri);

        return jsonException;
    }

    /**
     * Build a JSON integrity error compliant to the standard exception
     * 
     * @param exception
     * @param request
     * @param httpStatus
     * @param message
     * @return
     */
    private JsonException buildGenericError(final Throwable exception, final HttpServletRequest request, final HttpStatus httpStatus,
            Locale locale) {
        String rootMsg = ExceptionUtils.getRootCauseMessage(exception);
        String finalMessage = messageSource.getMessage(ExceptionCode.INTERNAL_ERROR.getCode(), new Object[] { rootMsg }, locale);

        JsonException jsonException = new JsonException();
        jsonException.setError(httpStatus.getReasonPhrase());
        jsonException.setStatus(httpStatus.value());
        jsonException.setException(exception.getClass().getName());
        jsonException.setMessage(finalMessage);
        jsonException.setPath(request.getRequestURI());

        return jsonException;
    }

    private JsonException buildGenericError(final Throwable exception, final WebRequest request, final HttpStatus httpStatus,
            Locale locale) {
        String rootMsg = ExceptionUtils.getRootCauseMessage(exception);
        String finalMessage = messageSource.getMessage(ExceptionCode.INTERNAL_ERROR.getCode(), new Object[] { rootMsg }, locale);

        JsonException jsonException = new JsonException();
        jsonException.setError(httpStatus.getReasonPhrase());
        jsonException.setStatus(httpStatus.value());
        jsonException.setException(exception.getClass().getName());
        jsonException.setMessage(finalMessage);
        jsonException.setPath("");

        return jsonException;
    }

    private static <T> ResponseEntity<T> response(HttpStatus status, HttpHeaders headers, T body) {

        Assert.notNull(headers, "Headers must not be null!");
        Assert.notNull(status, "HttpStatus must not be null!");

        return new ResponseEntity<T>(body, headers, status);
    }
}

我想重写 HttpMessageNotReadableException 的行为,但我必须重写 handleHttpMessageNotReadable 方法,因为它是由超类管理的异常,我无法创建使用 @ExceptionHandler 注释的方法。

问题在于该方法公开了WebRequest 而不是HttpServletRequest。我需要一个HttpServletRequest 来获取客户端的远程IP 地址。

有没有办法做我在课堂上为DataIntegrityViolationException 所做的事情,在那里我可以获得HttpServletRequest

【问题讨论】:

  • ((NativeWebRequest) webRequest).getNativeRequest() 将为您提供实际请求。

标签: java spring spring-mvc spring-boot


【解决方案1】:

为了获取HttpServletRequest,你可以这样做:

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
            .getRequest();

希望这会有所帮助。

【讨论】:

  • 您认为有更优雅的方式吗?或者这是完成我想做的最好的方式?
  • @drenda 这将为您提供整个请求范围内的 HttpServletRequest 对象,我认为它足够有用和优雅。 :)
猜你喜欢
  • 1970-01-01
  • 2021-08-17
  • 2013-09-12
  • 1970-01-01
  • 2010-10-08
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
  • 2018-02-07
相关资源
最近更新 更多