use*_*345 5 java spring http-status-codes exceptionhandler
我想根据响应对象错误动态返回 HTTPStatus 代码,如 400、400、404 等。我被提到了这个问题 -使用 spring 3 restful以编程方式更改 http 响应状态,但它没有帮助。
我有一个带有@ExceptionHandler方法的控制器类
@ExceptionHandler(CustomException.class)
@ResponseBody
public ResponseEntity<?> handleException(CustomException e) {
return new ResponseEntity<MyErrorResponse>(
new MyErrorResponse(e.getCode(), ExceptionUtility.getMessage(e.getMessage())),
ExceptionUtility.getHttpCode(e.getCode()));
}
Run Code Online (Sandbox Code Playgroud)
ExceptionUtility是一个类,我在其中使用了上面使用的两种方法(getMessage和getCode)。
public class ExceptionUtility {
public static String getMessage(String message) {
return message;
}
public static HttpStatus getHttpCode(String code) {
return HttpStatus.NOT_FOUND; //how to return status code dynamically here ?
}
}
Run Code Online (Sandbox Code Playgroud)
我不想检查 if 条件并相应地返回响应代码,有没有其他更好的方法来做到这一点?
第一种方法:
您可以在 customException 类中获取 HTTPStatus 字段。例如
class CustomException {
HttpStatus httpStatus;
....other fields
}
Run Code Online (Sandbox Code Playgroud)
您可以抛出如下错误:
throw new CustomException(otherField , HttpStatus.CREATED);
Run Code Online (Sandbox Code Playgroud)
在异常处理程序中,您可以执行以下操作:
@ExceptionHandler(CustomException.class)
@ResponseBody
public ResponseEntity<?> handleException(CustomException e) {
return new ResponseEntity<MyErrorResponse>(
new MyErrorResponse(e.getCode(), ExceptionUtility.getMessage(e.getMessage())),
e.getHttpStatus());
}
Run Code Online (Sandbox Code Playgroud)
其中,e.getHttpStatus()返回HTTPStatus。
第二种方法:
或者你可以为你的代码声明枚举,如下所示:
public enum ErrorCode {
MyErrorCode(HttpStatus.OK);
HttpStatus status;
//getter for httpstatus
}
Run Code Online (Sandbox Code Playgroud)
然后你可以修改你的异常处理程序
@ExceptionHandler(CustomException.class)
@ResponseBody
public ResponseEntity<?> handleException(CustomException e) {
return new ResponseEntity<MyErrorResponse>(
new MyErrorResponse(e.getCode(), ExceptionUtility.getMessage(e.getMessage())),
e.getHttpStatus());
}
Run Code Online (Sandbox Code Playgroud)
其中 e.getHttpStatus() 又是枚举上的方法调用,但为此,您必须更改自定义异常中的代码字段类型,如果您也不想这样做,那么您可以在枚举中编写一个辅助方法喜欢:
HttpStatus getHttpStatus(String code) {
return ErrorCode.valueOf(code.toUpperCase()).getHttpStatus();
}
Run Code Online (Sandbox Code Playgroud)
抱歉,我错过了上述方法中的空指针异常,但您可以根据需要进行修改,只是给出一个想法。:)
您需要为不同的异常定义不同的异常处理程序,然后使用@ResponseStatus如下:
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler({ UnAuthorizedException.class })
public @ResponseBody ExceptionResponse unAuthorizedRequestException(final Exception exception) {
return response;
}
@ResponseStatus(HttpStatus.CONFLICT)
@ExceptionHandler({ DuplicateDataException.class })
public @ResponseBody ExceptionResponse DuplicateDataRequestException(final Exception exception) {
return response;
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({ InvalidException.class })
public @ResponseBody ExceptionResponse handleInvalidException(final Exception exception) {
return response;
}
Run Code Online (Sandbox Code Playgroud)
这里的InvalidException.class、DuplicateDataException.class等是示例。您可以定义自定义异常并从控制器层抛出它们。例如,您可以定义 a并从异常处理程序UserAlreadyExistsException返回错误代码。HttpStatus.CONFLICT