【问题标题】:Spring Boot Exception HandlingSpring Boot 异常处理
【发布时间】:2023-03-28 14:37:01
【问题描述】:

我们正在为我们的应用程序使用 Spring Boot。当基于 Accept-Language 标头调用 Rest 服务时,我试图在响应中返回本地化结果。例如,如果 Accept-Language 标头是:zh,ja;q=0.8,en。既然我们支持,那么回复将是中文的。

但如果 Accept-Language 标头是:zh1,ja;q=0.8,en。然后我得到如下所示的内部服务器错误,因为它无法调用@ExceptionHandler 我没有得到我喜欢的响应。以下是我得到的

{
  "timestamp": 1462213062585,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "java.lang.IllegalArgumentException",
  "message": "java.lang.IllegalArgumentException: range=zh1",
  "path": "/user/v1//paymentmethods/creditdebitcards"
}

相反,这是我想要抛出的,因为对于所有其他异常,我们会处理并抛出类似的响应。

{
  "operation": {
    "result": "ERROR",
    "errors": [
      {
        "code": "1000",
        "message": "An unidentified exception has occurred.",
        "field": ""
      }
    ],
    "requestTimeStampUtc": "2016-05-02T18:22:03.356Z",
    "responseTimeStampUtc": "2016-05-02T18:22:03.359Z"
  }
}

以下是我的类,如果标题错误(如 zh1,ja;q=0.8,en),则下面的 parse 方法会像上面一样抛出 500 错误。

public class SmartLocaleResolver extends AcceptHeaderLocaleResolver {

    @Autowired
    ExceptionHandling exceptionHandling;

    @Autowired
    MessageHandler    messageHandler;

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        try {
            List<LanguageRange> list = Locale.LanguageRange.parse(request.getHeader("Accept-Language"));
            if (!list.isEmpty()) {
                for (LanguageRange s : list) {
                    if (ApplicationConstants.LOCALE.contains(s.getRange())) {
                        return Locale.forLanguageTag(s.getRange());
                    }
                }
            }
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(e);
        }
        return request.getLocale();
    }

下面是ExceptionHandler类

@EnableWebMvc
@ControllerAdvice
public class ExceptionHandling extends ResponseEntityExceptionHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionHandling.class);

    @Autowired
    private MessageHandler      messageHandler;

    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler(value = { UnsupportedMediaTypeException.class, InvalidMediaTypeException.class })
    public void unsupportedMediaTypeException() {

    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(value = Exception.class)
    public @ResponseBody OperationsErrorBean handleglobalException(final HttpServletRequest request,
            final Exception ex) {
        LOGGER.error("Unhandled Exception Occurred: ", ex);
        return errorResponse("1000", messageHandler.localizeErrorMessage("error.1000"), "", request.getRequestURI(),
                request.getAttribute("startTime").toString());

    }
}

这是我的 ApplicationConfig.java 类

@Configuration
@ComponentScan("com.hsf")
@EnableWebMvc
public class ApplicationConfig extends WebMvcConfigurerAdapter {

    @Value("${spring.application.name}")
    String appName;

    @Bean
    public AlwaysSampler defaultSampler() {
        return new AlwaysSampler();
    }

    @Override
    public void addInterceptors(final InterceptorRegistry registry) {

        if (StringUtils.isNotBlank(appName)) {
            MDC.put("AppName", appName);

        } else {
            MDC.put("AppName", "APPNAME_MISSING");
        }

        registry.addInterceptor(new RequestInterceptor()).addPathPatterns("/user/v1/**");
    }

    @Bean
    public LocaleResolver localeResolver() {
        return new SmartLocaleResolver();
    }

    @Bean
    public DispatcherServlet dispatcherServlet() {
        final DispatcherServlet servlet = new DispatcherServlet();
        servlet.setDispatchOptionsRequest(true);
        return servlet;
    }

    @Bean
    public MessageSource messageSource() {
        final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames("classpath:i18n/messages");
        // If true, the key of the message will be displayed if the key is not
        // found, instead of throwing an exception
        messageSource.setUseCodeAsDefaultMessage(true);
        messageSource.setDefaultEncoding("UTF-8");
        // The value 0 means always reload the messages to be developer friendly
        messageSource.setCacheSeconds(10);
        return messageSource;
    }

}

【问题讨论】:

  • ExceptionHandling 类将处理 Exception.class,而您的程序会抛出 IllegalArgumentException。奇怪你如何捕获 IllegalArugmentException 然后新的相同异常?你应该重新投掷或根本不接住它。
  • @ExceptionHandler注解应该可以处理异常列表吧?
  • @MinhKieu 我已经尝试了所有这些,catch 块中的代码基本上是我试图改变我的响应。我已经尝试过你提到的东西。那不会引起任何问题。问题是在发出请求时,它会首先进入 SmartLocalResolver 类,然后调用 ExceptionHander 类。我的问题的解决方案是需要在 SmartLocalResolver 之前调用 ExceptionHandler 类。但我现在不确定如何。

标签: java rest exception-handling localization spring-boot


【解决方案1】:

unsupportedMediaTypeException方法的@ExceptionHandler注解不包含IllegalArgumentException,而是:

@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
@ExceptionHandler(value = { UnsupportedMediaTypeException.class, 
  InvalidMediaTypeException.class })
public void unsupportedMediaTypeException() { }

应该是:

@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
@ExceptionHandler(value = { UnsupportedMediaTypeException.class, 
  InvalidMediaTypeException.class, IllegalArgumentException.class })
public void unsupportedMediaTypeException() { }

此外,由于处理多种语言似乎是您的应用程序的要求之一,我建议为这种情况创建一个专用的 RuntimeException InvalidAcceptLanguageException,而不是为此使用通用的 IllegalArgumentException

【讨论】:

  • 如果我添加 IllegalArgumentException.class 它给我的状态为 415 不受支持的媒体类型,但是当我将主体添加到该方法 unsupportedMediaTypeException() 以获得有意义的响应时,它在日志中说失败了调用@ExceptionHandler 方法
  • 我添加了 ApplicationConfig.java 类。我的想法是先调用 SmartLocaleResolver,然后再调用 ExceptionHandling 类。所以我无法覆盖 500 错误并更改响应。
【解决方案2】:

我在拦截器中进行了 Accept-Language 检查,并在解析标头时抛出了我创建的自定义异常。所以我抛出了一个 400 Bad 请求,并给出了我想要显示的正确响应。

 public class RequestInterceptor extends HandlerInterceptorAdapter {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                    throws Exception {

                final String startTime = DateUtils.getUTCDate();

                request.setAttribute("startTime", startTime);


                **try {
                    Locale.LanguageRange.parse(request.getHeader("Accept-Language"));
                } catch (IllegalArgumentException e) {
                    throw new InvalidAcceptLanguageException();
                }**

                return true;

            }
        }

我在我的 ExceptionHandling 类中添加了一个方法来抛出 InvalidAcceptLanguageException。

@EnableWebMvc
@ControllerAdvice
public class ExceptionHandling extends ResponseEntityExceptionHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionHandling.class);


    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = InvalidAcceptLanguageException.class)
    @ResponseBody
    public OperationsErrorBean invalidAcceptLanguageException(final HttpServletRequest request, final Exception ex) {
        return errorResponse("N/A", "Accept-Language is not in correct format", "", request.getRequestURI(),
                request.getAttribute("startTime").toString());
    }
}

【讨论】:

    猜你喜欢
    • 2015-02-23
    • 2016-09-29
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    • 2017-09-14
    • 2021-07-10
    相关资源
    最近更新 更多