java - @ExceptionHandler not catching exceptions being thrown from Spring Formatters -
i have controller serves rest web service , 1 of methods expecting model in model have fellow member variable of type myobjectid
.
public class mymodel { private myobjectid objectid; public myobjectid getmyobjectid() { homecoming objectid; } public void setmyobjectid(final myobjectid objectid) { this.objectid = objectid; } }
i have custom implementation of formatter
(org.springframework.format) myobjectid , parse method:
@override public myobjectid parse(final string text, final locale locale) throws parseexception { // if string invalid throw illegalargumentexception homecoming myobjectidconvertor.convert(text); }
in spring-servlet.xml registered formatter:
<bean id="conversionservice" class="org.springframework.format.support.formattingconversionservicefactorybean"> <property name="formatters"> <set> <bean class="com.foo.myobjectidformatter"/> </set> </property> </bean>
now problem when exception thrown formatter not beingness caught of @exceptionhandler
methods. hence response beingness output in format:
org.springframework.validation.beanpropertybindingresult: 1 errors field error in object 'filtermodel' on field 'myobjectid': rejected value [foo123123]; codes [typemismatch.filtermodel.myobjectid,typemismatch.myobjectid,typemismatch.com.foo.myobjectid,typemismatch]; arguments [org.springframework.context.support.defaultmessagesourceresolvable: codes [filtermodel.myobjectid,myobjectid]; arguments []; default message [myobjectid]]; default message [failed convert property value of type 'java.lang.string' required type 'com.myobjectid' property 'myobjectid'; nested exception org.springframework.core.convert.conversionfailedexception: failed convert type java.lang.string type com.myobjectid value 'foo123123'; nested exception java.lang.numberformatexception: input string: "foo123123"]
how can create type of exception begin caught @exceptionhandler
. 1 of exception handler methods:
@exceptionhandler(exception.class) @responsebody public responseentity<string> handleexception(final exception ex) { log.error("error handling request", ex); homecoming new responseentity<>(stringutils.hastext(ex.getmessage()) ? ex.getmessage() : "error handling request", httpstatus.bad_request); }
i had same situation
i don't handle formatter
exception in @controller
checking following:
codes [typemismatch.filtermodel.myobjectid,typemismatch.myobjectid,typemismatch.com.foo.myobjectid,typemismatch];
i suggest work around validationmessages.properties
(you need configure localvalidatorfactorybean
, reloadableresourcebundlemessagesource
)
for illustration have
typemismatch.person.datebirth=invalid data, format date of birth incorrect!
then assuming if wise , friendly user writes invalid date according formatter, dateformatter
class, in case throws exception, since have defined code typemismatch.person.datebirth
error message show in web form.
observe our codes similar in way. think spring looks somewhere if of codes
exists show appropriate message, since have none code exception stack trace remains, in case how have code , customized message, presented in web form.
i didn't test yet how customized error message should fit through rest
. in case if formatter
throws error sure code
exists in validationmessages.properties
file.
java spring rest spring-mvc
No comments:
Post a Comment