首先,我不知道确切的标题是什么,因为我真的不明白问题的原因。我觉得真的很奇怪。
问题:
Error Description : java.lang.NumberFormatException: For input string: ""
调查结果:
当发送的请求值采用此格式时,此操作有效 nicknames: "Borgy, Frederick, Carlo"
.
在服务器端,值变为 ["Borgy", "Frederick", "Carlo"]
并将作为该值(json类型列)存储在db中。
问题似乎出在stringarrayconverter和从ui/页面发送的数据(昵称)的格式上。
如果我能将数据发送为:
nicknames[]: Borgy
nicknames[]: Frederick
nicknames[]: Carlo
然后它将存储在db(json类型列)中 ["Borgy", "Frederick", "Carlo"]
. 我认为改变将在stringarrayconverter上完成。。。不确定。。。只是胡乱猜测。
注:
不涉及字符串到整数(或任何数字)的转换,奇怪的是为什么我会得到数字格式异常。
另外,当值昵称只有一个时(例如:“['butitoy']”),它将工作。。。但是如果它不止一个(例如:“['butitoy','gwapitoy']”),它将导致数字格式异常。
这个错误是不言自明的。。。但我真的不知道错误是怎么回事。我试过调试,但它无法进入验证请求。字段昵称是string[]类型,是mysql json类型的列,但我认为问题的原因不在json类型列上。
案例:
我有一个简单的验证请求,它有一个对象请求主体。这是通过邮寄提交的。这个项目是一个springmvc项目。
请求:
岗位http://localhost:8080/管理员/帐户验证
控制器:
@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) -------------
谢谢!
暂无答案!
目前还没有任何答案,快来回答吧!