0

すべて、私のコントローラー:

@RequestMapping(method = RequestMethod.GET, value = "/search")
@ResponseBody
public CemeteryRestResponse<List<String>> search(
        @RequestParam("location") Location location) {
    CemeteryRestResponse<List<String>> restResponse = new CemeteryRestResponse<List<String>>();
    restResponse.setBody(new ArrayList<String>());
    Long a = Long.valueOf("aaaa");
    try {
        for (PublicCemetery cemetery : cemeteryDao.findByLocation(location)) {
            restResponse.getBody().add(cemetery.getNameCn());
        }
    } catch (Exception e) {
        try {
            throw new SQLException();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
    }
    restResponse.setSuccess(true);
    return restResponse;
}

同じコントローラー内の私の実行ハンドル メソッド:

@ExceptionHandler(value = { Exception.class, SQLException.class,
        IllegalArgumentException.class, NumberFormatException.class })
@ResponseBody
public CemeteryRestResponse<String> exceptionHandler(Exception e,
        SQLException e2, IllegalArgumentException e3,
        NumberFormatException e4) {
    CemeteryRestResponse<String> restResponse = new CemeteryRestResponse<String>();
    restResponse.setFailureMessageCn("data base exception");

    restResponse.setSuccess(false);
    return restResponse;
}

検索メソッド trhow SQLException および NumberFormatException @ExceptionHandler が処理できない場合。ありがとう!

4

2 に答える 2

0

(すべての例外) をキャッチExceptionしてから、 new をスローしSQLExceptionます。次に、すぐにその SQLException をキャッチし、そのスタック トレースを出力します。検索メソッドは決して例外をスローしません。

try-catch を削除する

throw new SQLException();

そしてそれはうまくいくはずです。ただし、例外処理を再検討してください (すべての例外をキャッチしてから、別の種類の例外をスローしないでください)。

于 2013-03-21T07:15:46.133 に答える
0

リクエスト ハンドラ メソッドからスローされる例外はありません。すべての例外を自分で処理しています。

例外ハンドラーは、リクエスト ハンドラーが Spring フレームワークに例外をスローした場合にのみ有効になります。

@RequestMapping(method = RequestMethod.GET, value = "/search")
@ResponseBody
public CemeteryRestResponse<List<String>> search(
        @RequestParam("location") Location location) throws Exception{
    CemeteryRestResponse<List<String>> restResponse = new CemeteryRestResponse<List<String>>();
    restResponse.setBody(new ArrayList<String>());
    Long a = Long.valueOf("aaaa");
    for (PublicCemetery cemetery : cemeteryDao.findByLocation(location)) {
        restResponse.getBody().add(cemetery.getNameCn());
    }
    restResponse.setSuccess(true);
    return restResponse;
}
于 2013-03-21T07:17:01.580 に答える