Utilizzando Spring 3, mi piace creare un gestore di eccezioni utilizzando il ExceptionHandler annotazione che gestirà "no pagina trovata (404)" richieste. Sto usando il seguente codice per fare questo. Ma quando indico un URL che non esiste, viene richiamato il gestore di eccezioni predefinito definito da Spring.Spring 3 - Creare ExceptionHandler per NoSuchRequestHandlingMethodException
Potrebbe essere che sto gestendo l'eccezione NoSuchRequestHandlingMethodException. Se lo è, allora quale eccezione dovrei registrare?
Volete dare un'occhiata al seguente codice e vedere cosa sto facendo male?
NOTA: Se cambio l'eccezione nel @ExceptionHandler a NullPointerException e creare un RequestMapping di gettare puntatore nullo, che funzionerà.
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class GeneralHandler {
private final Log logger = LogFactory.getLog(getClass());
@ExceptionHandler(NoSuchRequestHandlingMethodException.class)
public ModelAndView handleException (NoSuchRequestHandlingMethodException ex) {
ModelAndView mav = new ModelAndView();
logger.error("Exception found: " + ex);
return mav;
}
}
Grazie. Userò l'approccio HandlerExceptionResolver. – sgsweb
ha funzionato per te? Sto ancora avendo lo stesso problema – SJS