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, मार डाला जाता है के रूप में उम्मीद।स्प्रिंग
हालांकि, अगर मैं फोन http://example.com/aa/bb/QQQ मैं उम्मीद होती है कि समारोह handleTypeMismatchException
कहा जाता है, लेकिन इसके बजाय , handleException प्रकार TypeMismatchException
के अपवाद के साथ कहा जाता है।
एक बुरा वैकल्पिक हल यह handleException()
, अंदर अपवाद के प्रकार का परीक्षण करने और फोन handleTypeMismatchException
यदि अपवाद प्रकार TypeMismatchException
की है करने के लिए किया जाएगा होगा बनाने के लिए।
लेकिन अब यह क्यों काम करता है? अपवाद के प्रकार के अनुसार रनटाइम पर अपवादक को चुना जाता है? या यह संकलन समय पर चुना जाता है?
'http: // example.com/aa/bb/QQQ' –
फ़ंक्शन हैंडलएक्सप्शन (अंतिम अपवाद अपवाद) के मामले में अपवाद के साथ टाइप अपवाद को अपवाद के साथ कहा जा रहा है अपवाद के साथ कहा जा रहा है। –