在Spring 3中是否可以动态设置@ResponseStatus的原因? [英] In Spring 3 is it possible to dynamically set the reason of @ResponseStatus?

查看:333
本文介绍了在Spring 3中是否可以动态设置@ResponseStatus的原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的异常类,注释为返回给定的 HttpStatus

I have a custom exception class annotated to return a given HttpStatus:

@ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="Invalid parameter")
public class BadRequestException extends RuntimeException
{
  public BadRequestException(String msg)
  {
    super(msg);
  }
}

当我抛出来自我的控制器的BadRequestException 但原因始终是无效参数。有没有办法在这个类中设置返回的原因?我想传递一个字符串作为原因。

This works when I throw a BadRequestException from my controller but the reason is always "Invalid parameter" of course. Is there a way to set the returned reason in this class? I'd like to pass a string to be used as the reason.

谢谢!

推荐答案

你可以使用 response.sendError(HttpServletResponse.SC_BAD_REQUEST,无效的foo);

这篇关于在Spring 3中是否可以动态设置@ResponseStatus的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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