使用 Spring Data REST 处理自定义异常 (i18n) [英] Handling custom exceptions (i18n) with Spring Data REST

查看:52
本文介绍了使用 Spring Data REST 处理自定义异常 (i18n)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Spring Boot 1.5.4 与 Spring JPA、Spring Data REST、HATEOAS 一起使用...我正在寻找一种最佳实践(Spring 方式)来自定义异常 Spring Data REST 正在管理添加 i18n 支持.

我查看了类 MessageException (https://github.com/spring-projects/spring-data-rest/blob/master/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/ExceptionMessage.java) 作为起点.

典型的 Spring Data REST 异常非常好:

<代码> {"时间戳": "2017-06-24T16:08:54.107+0000",状态":500,"error": "内部服务器错误","异常": "org.springframework.dao.InvalidDataAccessApiUsageException","message": "org.hibernate.TransientPropertyValueException: 非空属性引用了一个瞬态值 - 在查询当前操作之前必须保存瞬态实例:com.test.server.model.workflows.WorkSession.checkPoint -> com.test.server.model.checkpoints.CheckPoint; 嵌套异常是 java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null 属性引用了一个瞬态值 - 在查询当前操作之前必须保存瞬态实例:com.test.server.model.workflows.WorkSession.checkPoint -> com.test.server.model.checkpoints.CheckPoint",路径":/api/v1/workSessions/start"}

我想做的是:

  1. 本地化错误和消息字段 (i18n)
  2. 可能将消息文本更改为其他内容(始终本地化)

我在 Spring Data REST 文档中没有找到任何关于如何自定义或本地化异常的参考(https://docs.spring.io/spring-data/rest/docs/current/reference/html/).我希望有一种优雅的方式来做到这一点.

我在我的 WebMvcConfigurerAdapter 中添加了这个:

@Bean公共 LocaleResolver localeResolver() {返回新的 SmartLocaleResolver();}公共类 SmartLocaleResolver 扩展 CookieLocaleResolver {@覆盖公共语言环境 resolveLocale(HttpServletRequest 请求){String acceptLanguage = request.getHeader("Accept-Language");if (acceptLanguage == null || acceptLanguage.trim().isEmpty()) {返回 super.determineDefaultLocale(request);}返回 request.getLocale();}}@豆公共 ResourceBundleMessageSource messageSource() {ResourceBundleMessageSource source = new ResourceBundleMessageSource();source.setBasenames("i18n/messages");//资源包名称source.setUseCodeAsDefaultMessage(true);返回源;}

我想我可以通过这种方式拦截异常:

 @ControllerAdvice(annotations = RepositoryRestController.class)公共类 GenericExceptionHandler {@ExceptionHandler公共响应实体句柄(异常 e,语言环境){//缺少部分...返回新的 ResponseEntity(exceptionMessageObject, new HttpHeaders(), httpStatus);}

有没有办法使用 Spring 最佳实践将所有内容放在一起?

解决方案

@ControllerAdvice(annotations = RepositoryRestController.class)公共类 GenericExceptionHandler {@自动连线私人消息源消息源;@ExceptionHandler//如果你在方法中不使用异常e,你可以删除它,只使用语言环境公共响应实体句柄(异常 e,语言环境){String errorMessage = messageSource.getMessage("error.message", new Object[]{},locale);//设置消息或返回它而不是exceptionMessageObjectexceptionMessageObject.setMessage(exceptionMessageObject);返回新的 ResponseEntity(exceptionMessageObject,新的 HttpHeaders(), httpStatus);}

参见 spring 文档 7.15.1 使用 MessageSource 进行国际化

<小时><块引用>

" 我应该如何创建 exceptionMessageObject 就像一个 Spring数据 REST 创建?"

创建您自己的错误包装器,例如:

public class CustomError {私有 HttpStatus 状态;私人字符串消息;private Exception originalException;//如果你需要它//获取设置器}

<块引用>

"如何为不同的异常设置不同的消息?我应该创建一个长 if else 链检查异常的类?"

创建解析器,

private String resolveExceptionToMessage(Exception exceptionio){//或放入map//String errorCode = map.get(excptio);//返回 messageSource.getMessage(errorCode , new Object[]{},locale);if(exceptio instanceof ....){return messageSource.getMessage("error.message.type1", new Object[]{},locale);}返回";}

或使用@ExceptionHandler({ CustomException1.class }) , @ExceptionHandler({ CustomException1.class })....的方法,并在每个方法中放入 errror.code ,所有其他部分都相似

 @ExceptionHandler({ CustomException1.class})公共响应实体 handleException1() {返回 createError(此异常的代码 1);}@ExceptionHandler({ CustomException2.class})公共响应实体 handleException2() {返回 createError(此异常的代码 2);}私有 ResponseEntity createError(String errorCode) {CustomError customError = new CustomError();customError.setHttpStatus(HttpStatus.BAD_REQUEST);String errorMessage = messageSource.getMessage(errorCode , new Object[]{},locale);customError.setMessage(errorMessage);customError.setOriginalException(e);返回新的 ResponseEntity(customError, new HttpHeaders(),customError.getStatus());}

<块引用>

如何设置httpStatus?我想使用默认状态 Spring DataREST 用于公共异常...

public ResponseEntity handle(Exception e, Locale locale) {CustomError customError = new CustomError();customError.setHttpStatus(HttpStatus.BAD_REQUEST);customError.setMessage(resolveExceptionToMessage(e));customError.setOriginalException(e);返回新的 ResponseEntity(customError, new HttpHeaders(),customError.getStatus());}

I'm using Spring Boot 1.5.4 with Spring JPA, Spring Data REST, HATEOAS... I'm looking for a best practice (Spring way) to customize exceptions Spring Data REST is managing adding the i18n support.

I looked at the class MessageException (https://github.com/spring-projects/spring-data-rest/blob/master/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/ExceptionMessage.java) as start point.

A typical Spring Data REST exception is very nice:

    {
    "timestamp": "2017-06-24T16:08:54.107+0000",
    "status": 500,
    "error": "Internal Server Error",
    "exception": "org.springframework.dao.InvalidDataAccessApiUsageException",
    "message": "org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved beforeQuery current operation : com.test.server.model.workflows.WorkSession.checkPoint -> com.test.server.model.checkpoints.CheckPoint; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved beforeQuery current operation : com.test.server.model.workflows.WorkSession.checkPoint -> com.test.server.model.checkpoints.CheckPoint",
    "path": "/api/v1/workSessions/start"
}

What I'm trying to do is:

  1. Localize error and message fields (i18n)
  2. possibly change the message text to something else (always localized)

I didn't find any reference in Spring Data REST doc about how to customize or localize exception (https://docs.spring.io/spring-data/rest/docs/current/reference/html/). I hope there is a elegant way to do that.

I added in my WebMvcConfigurerAdapter this:

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

public class SmartLocaleResolver extends CookieLocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String acceptLanguage = request.getHeader("Accept-Language");
        if (acceptLanguage == null || acceptLanguage.trim().isEmpty()) {
            return super.determineDefaultLocale(request);
        }
        return request.getLocale();
    }

}

@Bean
public ResourceBundleMessageSource messageSource() {
    ResourceBundleMessageSource source = new ResourceBundleMessageSource();
    source.setBasenames("i18n/messages"); // name of the resource bundle
    source.setUseCodeAsDefaultMessage(true);
    return source;
}

I guess I could be able to intercept exceptions in this way:

    @ControllerAdvice(annotations = RepositoryRestController.class)
public class GenericExceptionHandler {

    @ExceptionHandler
    public ResponseEntity handle(Exception e, Locale locale) {
          //missing part...
            return new ResponseEntity(exceptionMessageObject, new HttpHeaders(), httpStatus);
    }

Is there a way to put all together using Spring best practices?

解决方案

@ControllerAdvice(annotations = RepositoryRestController.class)
public class GenericExceptionHandler {
    @Autowired
    private MessageSource messageSource;

    @ExceptionHandler
    //if you don't use Exception e in method you can remove it , live only Locale
    public ResponseEntity handle(Exception e, Locale locale) {

            String errorMessage = messageSource.getMessage(
                                 "error.message", new Object[]{},locale);  
            //set message  or return it instead of exceptionMessageObject
            exceptionMessageObject.setMessage(exceptionMessageObject);

            return new ResponseEntity(exceptionMessageObject, 
                   new HttpHeaders(), httpStatus);
    }

see spring doc 7.15.1 Internationalization using MessageSource


" how I should create exceptionMessageObject to be like the one Spring Data REST creates? "

create you own error wraper like :

public class CustomError {
    private HttpStatus status;
    private String message;
    private Exception originalException;//if you need it        
    // getter setter
}

"How to have different messages for different exceptions? Should I create a long if else chain checking the class of the exception? "

create resolver ,

private String resolveExceptionToMessage(Exception exceptio){
    //or put in map<Exceptio,String error.message.type1> 
    // String errorCode = map.get(excptio);
    //eturn messageSource.getMessage(errorCode , new Object[]{},locale);
    if(exceptio instanceof ....){
        return messageSource.getMessage("error.message.type1", new Object[]{},locale);
    }
    return "";
}

or use methods with @ExceptionHandler({ CustomException1.class }) , @ExceptionHandler({ CustomException1.class }).... and do put in each method just errror.code , all other part are similar

 @ExceptionHandler({ CustomException1.class})
    public ResponseEntity handleException1() {
        return createError(code for this exceptio 1);
    }
    @ExceptionHandler({ CustomException2.class})
    public ResponseEntity handleException2() {
        return createError(code for this exceptio 2);
    }
    private ResponseEntity createError(String errorCode ) {
            CustomError customError = new CustomError();
            customError.setHttpStatus(HttpStatus.BAD_REQUEST);
            String errorMessage = messageSource.getMessage(
                                 errorCode , new Object[]{},locale); 

            customError.setMessage(errorMessage );
            customError.setOriginalException(e);
            return new ResponseEntity<Object>(customError, new HttpHeaders(), 
                          customError.getStatus());
    }

How set httpStatus? I would like use the default status Spring Data REST use for commons exceptions...

public ResponseEntity handle(Exception e, Locale locale) {
        CustomError customError = new CustomError();
        customError.setHttpStatus(HttpStatus.BAD_REQUEST);
        customError.setMessage(resolveExceptionToMessage(e));
        customError.setOriginalException(e);
        return new ResponseEntity<Object>(customError, new HttpHeaders(), 
                      customError.getStatus());
}

这篇关于使用 Spring Data REST 处理自定义异常 (i18n)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆