开发者

Setting the status code message of a HTTP ResponseCode thrown by an @ResponseCode annotate Exception

开发者 https://www.devze.com 2023-04-07 08:39 出处:网络
I am currently trying to set the message of a HTTP Status Code thrown by an @ResponseCode annotated Exception.

I am currently trying to set the message of a HTTP Status Code thrown by an @ResponseCode annotated Exception. I have defined the exception:

@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public final class BadRequestException extends IllegalArgumentException {
/**
 * The serial version UID.
 */
private static final long   serialVersionUID    = -6121234578480284282L;

public BadRequestException() {
    super();
}

public BadRequestException(String message, Throwable cause) {
    super(message, cause);
}

public BadRequestException(String message) {
    super(message);
}

public BadRequestException(Throwable cause) {
    super(cause);
}

}

If the exception is thrown I throw it again in my @ExceptionHandler annotate method:

@ExceptionHandler(RuntimeException.class)
public String handleRuntimeException(Exception e, HttpSession session) {
        if (e instanceof BadRequestException) {
            throw (BadRequestException)e;
        }
        return FAILURE;
    }

I generally throw this exception in this way:

if (result.hasErrors()) {
  throw new BadRequestException("Bad Request Message.");
}

The HTTP Status Code always returns only "HTTP Status 4开发者_运维知识库00 -" without a message is set.

THX!


Annotate your exception handler with the @ResponseStatus. Then created a basic error/exception view and pass the exception stack trace or whatever to that view

0

精彩评论

暂无评论...
验证码 换一张
取 消