【发布时间】: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