java - Exception with Spring ExceptionHandler and Rest response -
i'm using @exceptionhandler manage exception , return json response rest api throws exception. @ moment manage 2 exception, first resourcenotfoundexception , works second, fileextensionexception, doesn't work. throws exception in eclipse console , nothing rest response.
2015-09-21 09:09:05.197 error 7500 --- [nio-8080-exec-2] .m.m.a.exceptionhandlerexceptionresolver : failed invoke @exceptionhandler method: public matlab.errorresponse matlab.errorcontroller.errorhandler(java.lang.exception)
org.springframework.web.httpmediatypenotacceptableexception: not find acceptable representation @ org.springframework.web.servlet.mvc.method.annotation.abstractmessageconvertermethodprocessor.writewithmessageconverters(abstractmessageconvertermethodprocessor.java:173) @ org.springframework.web.servlet.mvc.method.annotation.abstractmessageconvertermethodprocessor.writewithmessageconverters(abstractmessageconvertermethodprocessor.java:101) @ org.springframework.web.servlet.mvc.method.annotation.requestresponsebodymethodprocessor.handlereturnvalue(requestresponsebodymethodprocessor.java:185) @ org.springframework.web.method.support.handlermethodreturnvaluehandlercomposite.handlereturnvalue(handlermethodreturnvaluehandlercomposite.java:71) @ org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:126) @ org.springframework.web.servlet.mvc.method.annotation.exceptionhandlerexceptionresolver.doresolvehandlermethodexception(exceptionhandlerexceptionresolver.java:362) @ org.springframework.web.servlet.handler.abstracthandlermethodexceptionresolver.doresolveexception(abstracthandlermethodexceptionresolver.java:60) @ org.springframework.web.servlet.handler.abstracthandlerexceptionresolver.resolveexception(abstracthandlerexceptionresolver.java:138) @ org.springframework.web.servlet.handler.handlerexceptionresolvercomposite.resolveexception(handlerexceptionresolvercomposite.java:74) @ org.springframework.web.servlet.dispatcherservlet.processhandlerexception(dispatcherservlet.java:1183) @ org.springframework.web.servlet.dispatcherservlet.processdispatchresult(dispatcherservlet.java:1020) @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:971) @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:893) @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:967) @ org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:858) @ javax.servlet.http.httpservlet.service(httpservlet.java:622) @ org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:843) @ javax.servlet.http.httpservlet.service(httpservlet.java:729) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:291) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.springframework.web.filter.hiddenhttpmethodfilter.dofilterinternal(hiddenhttpmethodfilter.java:77) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.springframework.web.filter.characterencodingfilter.dofilterinternal(characterencodingfilter.java:85) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.springframework.boot.context.web.errorpagefilter.dofilter(errorpagefilter.java:116) @ org.springframework.boot.context.web.errorpagefilter.access$000(errorpagefilter.java:60) @ org.springframework.boot.context.web.errorpagefilter$1.dofilterinternal(errorpagefilter.java:91) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.springframework.boot.context.web.errorpagefilter.dofilter(errorpagefilter.java:109) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:219) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:106) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:502) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:142) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:79) @ org.apache.catalina.valves.abstractaccesslogvalve.invoke(abstractaccesslogvalve.java:616) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:88) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:518) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1091) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:673) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1526) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.run(nioendpoint.java:1482) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(thread.java:745)
2015-09-21 09:09:05.201 error 7500 --- [nio-8080-exec-2] o.s.boot.context.web.errorpagefilter : forwarding error page request [/managmentfile/zipdownload] due exception [the selected file has different extension:xlsx , not zip]
matlab.fileextensionexception: selected file has different extension:xlsx , not zip @ matlab.fileservices.getzipfile(fileservices.java:46) @ matlab.ws.filemanagerimpl.getzip(filemanagerimpl.java:38) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) @ org.springframework.web.method.support.invocablehandlermethod.doinvoke(invocablehandlermethod.java:221) @ org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod.java:137) @ org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:110) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlemethod(requestmappinghandleradapter.java:776) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:705) @ org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:85) @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:959) @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:893) @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:967) @ org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:858) @ javax.servlet.http.httpservlet.service(httpservlet.java:622) @ org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:843) @ javax.servlet.http.httpservlet.service(httpservlet.java:729) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:291) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.springframework.web.filter.hiddenhttpmethodfilter.dofilterinternal(hiddenhttpmethodfilter.java:77) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.springframework.web.filter.characterencodingfilter.dofilterinternal(characterencodingfilter.java:85) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.springframework.boot.context.web.errorpagefilter.dofilter(errorpagefilter.java:116) @ org.springframework.boot.context.web.errorpagefilter.access$000(errorpagefilter.java:60) @ org.springframework.boot.context.web.errorpagefilter$1.dofilterinternal(errorpagefilter.java:91) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.springframework.boot.context.web.errorpagefilter.dofilter(errorpagefilter.java:109) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:219) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:106) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:502) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:142) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:79) @ org.apache.catalina.valves.abstractaccesslogvalve.invoke(abstractaccesslogvalve.java:616) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:88) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:518) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1091) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:673) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1526) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.run(nioendpoint.java:1482) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(thread.java:745)
these codes called webservices:
public filesystemresource getzipfile(string filename) throws fileextensionexception { string ext=filenameutils.getextension(filename); if (ext!= "zip") throw new fileextensionexception(ext + " , not zip"); return new filesystemresource(new file(filename)); }
the exception:
package matlab; public class fileextensionexception extends runtimeexception { private static final long serialversionuid = 1l; public fileextensionexception(string message){ super("the selected file has different extension:" + message); } }
restcontroller:
@requestmapping(value = "/files", method = requestmethod.get) public response<collection<filemodel>> getallfiles(@requestparam(value="path", defaultvalue="/home") string path) throws resourcenotfoundexception { collection<filemodel> result; result = file.getallfiles(path); return new response<collection<filemodel>>(httpstatus.ok.value(),result); } @requestmapping(value = "/zipdownload", produces="application/zip", method = requestmethod.get) @responsebody public filesystemresource getzip(@requestparam(value="filepath", required=true) string filepath ) throws fileextensionexception{ return file.getzipfile(filepath); }
resourcenotfoundexception
public class resourcenotfoundexception extends exception { private static final long serialversionuid = 1l; public resourcenotfoundexception(string path){ super("the specified path: "+ path +" doesn't exist"); } }
the errorcontrol
@controlleradvice public class errorcontroller { /** * * @param e: exception thrown * @return erroreresponse */ @exceptionhandler(exception.class) public @responsebody errorresponse errorhandler(exception e){ //make exception builderrorresponse return errorresponsebuilder.builderrorresponse(e); } }
the error build:
public class errorresponsebuilder { public errorresponsebuilder() { } /** * build exception response beginning exception * @param e exception thrown * @return errorresponse: response of exception */ public static errorresponse builderrorresponse(exception e){ stringwriter errors = new stringwriter(); e.printstacktrace(new printwriter(errors)); return new errorresponse(httpstatusmanager.gethttpcode(e),e.getclass().getname(),e.getmessage(),errors.tostring()); } }
where problem? thanks
try update spring configuration with:
<bean id="methodhandlerexceptionresolver" class="org.springframework.web.servlet.mvc.annotation.annotationmethodhandlerexceptionresolver"> <property name="messageconverters"> <list> <ref bean="stringhttpmessageconverter"/> <ref bean="jsonhttpmessageconverter"/> <ref bean="marshallinghttpmessageconverter"/> </list> </property> </bean>
Comments
Post a Comment