応答オブジェクトのエラーに従って、400、400、404 などの HTTPStatus コードを動的に返したいと考えています。私はこの質問に言及されました-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()));
}
ExceptionUtility
getMessage
は、上記で使用した 2 つのメソッド (および)を持つクラス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 ?
}
}
if 条件をチェックインしてそれに応じて応答コードを返したくありません。これを行うための他のより良い方法はありますか?