In Spring 3 is it possible to dynamically set the reason of @ResponseStatus?

后端 未结 7 1938
别跟我提以往
别跟我提以往 2021-02-05 02:01

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

@ResponseStatus(value=HttpStatus.BAD_REQUEST, reason=\"Invalid parameter\")
         


        
7条回答
  •  别跟我提以往
    2021-02-05 02:23

    Annotations are meant to be static, and can't be set dynamically from your class. I suggest creating a subclass of your BadRequestException for every type of failure scenario and annotating them differently.

    This doesn't just serve as a workaround -- if you're hiding the details regarding what went wrong in the reason message, then you're losing flexibility because any code that catches a BadRequestException will have to deal with all failure scenarios the same way.

提交回复
热议问题