[英]Spring Boot Exception Handling
我們正在為我們的應用程序使用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),則下面的解析方法會拋出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;
}
}
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
。
我在攔截器中進行了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());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.