どのように特定のエラーの代わりに、内部サーバーエラーを取得しますか?

マンシンパテル:

私は自分のコードの例外からカスタム例外をスローしていていても郵便配達に内部サーバーエラーを取得しています。私は私が投げていますどのような、有効なエラーメッセージとエラーコードを有するの例外を見てみたいです。あなたの誰もが、この時点で私を助けることができるかどうかは大きな助けになります。私はより良いエラーメッセージを得ることができる方法のように。コードスナップ下に添加します。前もって感謝します。

@Service
public class FetchActionImpl implements FetchAction {

    private static Logger log = LoggerFactory.getLogger(FetchActionImpl.class);

    @Autowired
    FetchActionServiceImpl fetchActionService;// = new FetchActionServiceImpl();

    @Override
    public FetchResponse fetchActionRequest(String caseId) throws BBWException,Exception{
        //String resp ="";
        log.info("fetchaction Request: {}",ApplicationConstants.LOG_ENTRY_MESSAGE);
        log.info("The caseId received from BRASS:\n {}",caseId);
        FetchResponse resp = null;
        try{
            if(true) {
                throw new BBWException("500","Test");
                }
            resp = fetchActionService.fetchIt(caseId);

            log.debug("fetchaction Response: {}",resp.toString());

        } 
        catch (BBWException be) {
            throw be;
        }
        catch (Exception e) {
            throw new BBWException("500",e.getMessage());
        }
        return resp;
        }

}


@Api
@Path("/fetch_service")
public interface FetchAction {

    @GET
    @Path("/fetchaction/caseid/{caseid}")
    @Produces({MediaType.APPLICATION_JSON})
    //@Consumes({MediaType.TEXT_XML})
    @ApiOperation(
            value = "Respond BRASS Request",
            notes = "Returns a XML object "                 
    )
    @ApiResponses(
            value = {
                    @ApiResponse(code = 404, message = "Service not available"),
                    @ApiResponse(code = 500, message = "Unexpected Runtime error")
                    })
    public FetchResponse fetchActionRequest(@PathParam("caseid") String caseid) throws BBWException, Exception;

}`

    public class BBWException extends Exception {

            private static final long serialVersionUID = -7987978270260338068L;

            private String errorCode;
            private String errorMessage;

            public BBWException(String errorCode, String errorMessage) {
                super(errorMessage);
                this.errorCode = errorCode;
                this.errorMessage = errorMessage;
            }

            public String getErrorCode() {
                return errorCode;
            }


            public void setErrorCode(String errorCode) {
                this.errorCode = errorCode;
            }


            public String getErrorMessage() {
                return errorMessage;
            }
        public void setErrorMessage(String errorMessage) {
            this.errorMessage = errorMessage;
        }

        @Override
        public String toString() {

            return (this.errorCode + " " + this.errorMessage);
        }
}
Pijotrek:

各時間(キャッチされない)例外は、SpringBootリターンを投げるありますHttp-500 Internal Server Error春に例外を処理する多くの方法があります。

のは、私は私のコントローラを持っていると私は暗黙的に例外をスローしましょう。

@RestController
public class HelloWorldController {

    @GetMapping("/hello-world")
    public String helloWorld() {
        throw new MyCustomException("I just can't say hello!");
    }
}

それはあなたと同じです - あなたは例外で何かを指定することができます。

最初:

それに対処する方法の一つは、とクラスを作成することである@ControllerAdvice注釈。

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(MyCustomException.class)
    public ResponseEntity<String> handlyMyCustomException(MyCustomException e) {
        logger.error("error occurred {}", e);
        return new ResponseEntity<>("Something happened: " + e.getMessage(), HttpStatus.I_AM_A_TEAPOT);
    }
}

あなたがいないneccessarily、(全体的に)お好みの例外をキャッチし、お好みのHTTP応答ステータスとメッセージを返すことが可能です。この方法 I_AM_A_TEAPOT

第二:

    @ExceptionHandler(MyCustomException.class)
        public ResponseEntity<String> handlyMyCustomException(MyCustomException e) {
            logger.error("error occurred {}", e);
            return new ResponseEntity<>("Something happened: " + e.getMessage(), HttpStatus.I_AM_A_TEAPOT);
        }

あなたはまたの注釈を付け唯一の方法で作成することができ@ExceptionHandler、あなたのコントローラクラスでは-が、これは世界的なものではなく、これだけ正確なコントローラ・コールのために働くために起こっています。

下記の結果:

それは結果です

三番:

例外に対処する別の方法は、独自のエラー作成することです.htmlファイルを。あなたがファイルを配置した場合resources/static/error/500.html、それときに返されるべきでHttp-500 Internal Server Errorスローされます。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=232492&siteId=1