【问题标题】:Dynamic message for custom exception annotated as ResponseStatus自定义异常的动态消息,注释为 ResponseStatus
【发布时间】:2015-10-28 11:25:22
【问题描述】:

我正在尝试为我的自定义异常提供动态消息,如下面的代码 sn-p 所示:

@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Entity not found")
public class EntityNotFoundException extends RuntimeException {
    public EntityNotFoundException(String msg) {
        super(msg);
    }
}

但是当我抛出它时总是如下所示:

throw new EntityNotFoundException("User entity not found");

在浏览器中,我收到消息“找不到实体”而不是“找不到用户实体”。

如何做到这一点?

【问题讨论】:

    标签: spring spring-mvc


    【解决方案1】:

    我被困在这个问题上,但我只是删除了@ResponseStatus 的原因,它可以工作,所以你的代码应该是这样的:

    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    public class EntityNotFoundException extends RuntimeException {
    public EntityNotFoundException(String msg) {
        super(msg);
     }
    }
    

    现在您可以通过构造函数设置自定义消息

    【讨论】:

      猜你喜欢
      • 2014-08-11
      • 2014-05-29
      • 2015-11-04
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多