rest - Spring MVC Spring Security and Error Handling -
i'm using responseentityexceptionhandler global handling error , working normal, except want handle wrong request spring. logic overriding handlenosuchrequesthandlingmethod should handle this, insted of handling get
http status 404 -
type status report
message
description requested resource not available.
apache tomcat/7.0.37
i got when enable debuging in console:
warn : org.springframework.web.servlet.pagenotfound - no mapping found http request uri
just clarify handling mean i'm returning json.
any idea how handle this?
the reason right there, in dispatcherservlet
class; sends error response without bothering call exception handler (by default).
since 4.0.0.release behaviour can changed throwexceptionifnohandlerfound parameter:
set whether throw nohandlerfoundexception when no handler found request. exception can caught handlerexceptionresolver or
@exceptionhandler
controller method.
xml configuration:
<servlet> <servlet-name>rest-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>throwexceptionifnohandlerfound</param-name> <param-value>true</param-value> </init-param> </servlet>
java-based configuration:
public class appinitializer extends abstractannotationconfigdispatcherservletinitializer { void customizeregistration(servletregistration.dynamic registration) { registration.setinitparameter("throwexceptionifnohandlerfound", "true"); } ... }
then nohandlerfoundexception
can handled this:
@controlleradvice public class globalexceptionhandler extends responseentityexceptionhandler { @override responseentity handlenohandlerfoundexception(nohandlerfoundexception ex, httpheaders headers, httpstatus status, webrequest request) { // return whatever want } }
Comments
Post a Comment