首先,我不知道标题是什么,因为我真的不明白问题的原因。我发现它真的很奇怪。
问题:
Error Description : java.lang.NumberFormatException: For input string: ""
调查结果:
当发送的请求值采用此格式nicknames: "Borgy, Frederick, Carlo"
时,此方法有效。
在服务器端,值变为["Borgy", "Frederick", "Carlo"]
,并将作为该值存储在DB中(json类型列)。
问题似乎与StringArrayConverter以及从UI /页面发送的数据(昵称)的格式有关。
如果我能将数据发送为:
,那真的很好nicknames[]: Borgy
nicknames[]: Frederick
nicknames[]: Carlo
然后它将作为["Borgy", "Frederick", "Carlo"]
存储在DB(json类型列)中。我认为改变将在StringArrayConverter上完成......不确定......只是一个疯狂的猜测。
注意:
没有涉及整数(或任何数字)转换的字符串,发现它为什么我得到数字格式异常很奇怪。
此外,当值昵称只有一个(例如:“['butitoy']”)时,它会起作用......但如果它更多那个(例如:“['butitoy',' gwapitoy']“),它将导致数字格式异常。
错误是自我解释的......但我真的不知道错误的方式和位置。我试过调试但它不会进入验证请求。字段昵称是String []类型,并且是MySQL JSON类型列,但我认为问题原因不在JSON类型列上。
案例:
我有一个简单的验证请求,有一个对象请求体。这是通过POST提交的。
这个项目是一个Spring MVC项目。
请求:
发布http://localhost:8080/admin/account-validation
控制器:
@RequestMapping(value = "/admin/account-validation", method = RequestMethod.POST)
@Layout(value = ConstantUtil.THYMELEAF_TEMPLATE_NONE)
public @ResponseBody ValidationResponse accountValidation(@Valid Account account,
BindingResult result) {
// It wont go inside here
ValidationResponse res = new ValidationResponse();
...
return res;
}
型号:
@Entity
@DynamicInsert
@DynamicUpdate
@Table(name = "account")
public class Account {
private String m_name;
private String m_code;
private String[] m_nicknames;
private Boolean m_deletedInd;
//Getters
...
//Take note MySQL JSON Type column, nullable
@Column(name = "nicknames", columnDefinition = "json")
@Convert(converter = StringArrayConverter.class)
public String[] getNicknames() {
return m_nicknames;
}
...
//Setters
...
}
转换器:
@Converter
public class StringArrayConverter implements AttributeConverter<String[], String> {
private final static Logger logger = LoggerFactory.getLogger(StringArrayConverter.class);
private final static ObjectMapper mapper = new ObjectMapper();
@Override
public String convertToDatabaseColumn(String[] arrayObj) {
if (arrayObj != null) {
try {
return mapper.writeValueAsString(arrayObj);
} catch (JsonProcessingException ex) {
logger.error(ex.getMessage());
}
}
return null;
}
@Override
public String[] convertToEntityAttribute(String jsonString) {
if (!Util.isEmptyOrNull(jsonString)) {
try {
return mapper.readValue(jsonString, String[].class);
} catch (IOException ex) {
logger.error("[STRING-ARRAY-CONVERTER-ERROR]: " + ex.getMessage());
}
}
return null;
}
}
申请表格数据:
name: Dummy
code: DUMMY
nicknames[]: Borgy
nicknames[]: Frederick
nicknames[]: Carlo
_csrf: a93731b5-83db-4bb4-83cc-ed5f91e13aac
来自日志的例外:
2018-04-27 22:37:46 [http-nio-8080-exec-101] ERROR c.p.exception.GlobalExceptionHandler -
-------- Error Start for URL (http://localhost:8080/admin/account-validation) -------------
2018-04-27 22:37:46 [http-nio-8080-exec-101] ERROR c.p.exception.GlobalExceptionHandler -
Error Description : java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at org.springframework.beans.AbstractNestablePropertyAccessor.processKeyedProperty(AbstractNestablePropertyAccessor.java:304)
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:289)
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:280)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95)
at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:859)
at org.springframework.validation.DataBinder.doBind(DataBinder.java:755)
at org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:192)
at org.springframework.web.bind.ServletRequestDataBinder.bind(ServletRequestDataBinder.java:106)
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.bindRequestParameters(ServletModelAttributeMethodProcessor.java:150)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:113)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.bihagay.iyot.ErrorHandlerFilter.doFilter(ErrorHandlerFilter.java:35)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.pac4j.springframework.security.web.CallbackFilter.doFilter(CallbackFilter.java:91)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:124)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
2018-04-27 22:37:46 [http-nio-8080-exec-101] ERROR c.p.exception.GlobalExceptionHandler -
-------- Error End for URL (http://localhost:8080/admin/account-validation) -------------
谢谢!