Uso Spring WebMVC per fornire un'API REST. Uso metodi comeErrore di parsing dei parametri di cattura in Spring 3.0 WebMVC
@RequestMapping("/path({id}") void getById(@PathVariable("id") int id) {}
metodi.
Quando il client in modo non corretto mettere una stringa invece di un id intero nella query, ottengo un NumberFormatException come:
java.lang.NumberFormatException: For input string: "dojo"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:410)
at java.lang.Long.valueOf(Long.java:525)
at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:158)
at org.springframework.core.convert.support.StringToNumberConverterFactory$StringToNumber.convert(StringToNumberConverterFactory.java:59)
at org.springframework.core.convert.support.StringToNumberConverterFactory$StringToNumber.convert(StringToNumberConverterFactory.java:1)
at org.springframework.core.convert.support.GenericConversionService$ConverterFactoryAdapter.convert(GenericConversionService.java:420)
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:37)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:135)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:199)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:104)
at org.springframework.beans.SimpleTypeConverter.convertIfNecessary(SimpleTypeConverter.java:47)
at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:526)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolvePathVariable(HandlerMethodInvoker.java:602)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:289)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:163)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
La mia domanda è ora, come posso elegantemente prenderlo? So che Spring fornisce le annotazioni @ExeptionHandler, ma non voglio prendere la NFE in generale. Voglio essere in grado di catturare tutte le eccezioni di parsing per presentare un messaggio di errore piacevole al client.
Qualche idea?
Cheers,
gen
Ho effettuato alcune ricerche ma non sono riuscito a trovare una connessione tra PathVariable e BindingResult. Chiunque altro? – Jan
Non ce n'è, non funziona. – Affe