Paul Paul - 2 years ago 226
Java Question

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

I have a custom exception class annotated to return a given


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

This works when I throw a
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.


Answer Source

You can use response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid foo");

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download