java - How to handle maximum file size Exception in Spring Boot? -
i using spring boot v1.2.5 creating rest application. while uploading images, have check maximum file size , provided property :
multipart.maxfilesize= 128kb
in application.properties. facility provided spring boot itself. check working properly. question is, how handle exception , return message user can understand ?
update 1----------
i wrote method within controller, intend handle multipartexception, using @exceptionhandler
. not seem work.
this code :
@exceptionhandler(multipartexception.class) @responsestatus(value = httpstatus.payload_too_large) public applicationerrordto handlemultipartexception(multipartexception exception){ applicationerrordto applicationerrordto = new applicationerrordto(); applicationerrordto.setmessage("file size exceeded"); logger.error("file size exceeded",exception); return applicationerrordto; }
update 2----------
after @luboskrnac pointed out, have managed come solution. can use responseentityexceptionhandler
here handle particular case. believe, have used defaulthandlerexceptionresolver
, responseentityexceptionhandler
allow return responseentity
, opposed former, methods of return modelandview
. have not tried though.
this final code i'm using handle multipartexception
:
@controlleradvice public class customresponseentityexceptionhandler extends responseentityexceptionhandler { private static final logger logger = logger.getlogger(customresponseentityexceptionhandler.class); @exceptionhandler(multipartexception.class) @responsestatus(value = httpstatus.payload_too_large) @responsebody public applicationerrordto handlemultipartexception(multipartexception exception){ applicationerrordto applicationerrordto = new applicationerrordto(); applicationerrordto.setmessage("file size exceeded"); logger.error("file size exceeded",exception); return applicationerrordto; } }
i using swagger developing/documenting rest apis. response upon uploading file exceeds size. thanks.
you can use regular spring mvc features
@exceptionhandler
methods ,@controlleradvice
.errorcontroller
pick unhandled exceptions.
as multipartexception
seem hapen before @controller/@exceptionhandler/@controlleradvice
features comes play, should use errorcontroller
handle it.
btw, found thread in meantime. may want take look.
Comments
Post a Comment