java - How to return FileSystemResource or json with Spring RestController? -
i have method download zip file , @ moment use filesystemresource it. problem extension controller, ccheck if file zip file , throw exception if isn't so. exception create json object error details , return it. problem type of rest api because have :
@requestmapping(value = "/zipdownload", produces="application/zip", method = requestmethod.get) public filesystemresource getzip(@requestparam(value="filepath", required=true) string filepath ) throws fileextensionexception{ return file.getzipfile(filepath); }
and services:
public filesystemresource getzipfile(string filename) throws fileextensionexception { string ext=filenameutils.getextension(filename); if (!ext.equals("zip")) throw new fileextensionexception(ext + " , not zip"); return new filesystemresource(new file(filename)); }
the exception
public class fileextensionexception extends exception { private static final long serialversionuid = 1l; public fileextensionexception(string message){ super("the selected file has different extension:" + message); } }
and exception controller
@controlleradvice public class errorcontroller { /** * * @param e: exception thrown * @return erroreresponse */ @exceptionhandler(value = exception.class) public @responsebody errorresponse errorhandler(exception e){ //make exception builderrorresponse return errorresponsebuilder.builderrorresponse(e); } }
the problem exception launched spring override mine:
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)
how can return zip file or json message in same method? problem , produces="application/zip", without recieve correct error json doesn't download file, otherwise vice versa thanks
edit
you can keep getzip
controller , change @exceptionhandler
way :
@exceptionhandler(exception.class) @responsestatus(httpstatus.bad_request) public @responsebody errorresponse errorhandler(exception e, httpservletrequest request) { request.setattribute(handlermapping.producible_media_types_attribute, collections.singleton(mediatype.application_json)); return errorresponsebuilder.builderrorresponse(e); }
here modify request , reset producible_media_types_attribute
parameter tell explicitly spring representation want use.
Comments
Post a Comment