import org.springframework.beans.TypeMismatchException;
import javax.annotation.*;
import javax.servlet.http.*;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping(value = "/aa")
public class BaseController {
@RequestMapping(value = "/bb/{number}", method = RequestMethod.GET, produces = "plain/text")
public void test(@PathVariable final double number, final HttpServletResponse response) throws IOException {
throw new MyException("whatever");
}
@ResponseBody
@ExceptionHandler(MyException.class)
public MyError handleMyException(final MyException exception, final HttpServletResponse response) throws IOException {
...
}
@ResponseBody
@ExceptionHandler(TypeMismatchException.class)
public MyError handleTypeMismatchException(final TypeMismatchException exception, final HttpServletResponse response) throws IOException {
...
}
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
@ExceptionHandler
public MyError handleException(final Exception exception) throws IOException {
...
}
}
http://example.com/aa/bb/20 を呼び出すと、期待通りに関数handleMyExceptionが実行されます。SpringのExceptionHandler
しかし、私はhttp://example.com/aa/bb/QQQ を呼び出す場合、私は機能handleTypeMismatchException
が呼び出されることを期待する、 代わりに、handleExceptionはタイプTypeMismatchException
を除いて、呼ばれています。
それは、handleException()
内の例外の種類をテストすること と例外がタイプTypeMismatchException
であればhandleTypeMismatchException
を呼ぶだろうだろう作るために厄介な問題を回避します。
しかし、今はなぜ機能するのですか? 実行時に例外ハンドラが例外の種類に応じて選択されていますか? またはコンパイル時に選択されていますか?
「http:// example.com/aa/bb/QQQ」の場合にスローされる例外 –
function handleException(最終例外例外)が呼び出されており、例外はTypeMismatchExceptionのインスタンスです。 –