首页 > 代码库 > NumberFormatException: For input string: "null"
NumberFormatException: For input string: "null"
日志:
[INFO-2016/08/04/16/:21/:25]ProjectCommonFormController.(78) - 审批[同意]入参-[string]commonFormDtoStr ===={"commonForm":{"id":"1101115577946385"},"taskId":"322055","wfInstanceId":"1101115577859311","content":"","businessKey":"1101115577946385","projectId":"null","cityId":"null"} [ERROR-2016/08/04/16/:21/:25]XXXXFormController.(91) - 审批流程出错!For input string: "null" com.alibaba.fastjson.JSONException: For input string: "null" at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:467) at com.alibaba.fastjson.JSON.parseObject(JSON.java:206) at com.alibaba.fastjson.JSON.parseObject(JSON.java:166) at com.alibaba.fastjson.JSON.parseObject(JSON.java:287) at com.boa.weixin.controller.costmanager.expenses.ProjectCommonFormController.approvalCommonForm(ProjectCommonFormController.java:79) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606)
................... Caused by: java.lang.NumberFormatException: For input string: "null" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Long.parseLong(Long.java:441) at java.lang.Long.parseLong(Long.java:483) at com.alibaba.fastjson.util.TypeUtils.castToLong(TypeUtils.java:361) at com.alibaba.fastjson.parser.deserializer.LongFieldDeserializer.parseField(LongFieldDeserializer.java:44) at com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer.parseField(ASMJavaBeanDeserializer.java:81) at com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer$InnerJavaBeanDeserializer.parseField(ASMJavaBeanDeserializer.java:92) at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:231) at com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer.deserialze(ASMJavaBeanDeserializer.java:31) at Fastjson_ASM_CommonFormDto_67.deserialze(Unknown Source) at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:463)
分析:
在springmvc的使用中, 前后台传参时, 对后台的DTO对象进行参数绑定,如果是基本类型的包装类, 例如:Integer , Long等等...
开发时要注意控制前端的入参. 做好控制,避免传入"null", "undefined","none"类似的字符串与数值类型映射.
另外, 数字字符串可以转换成整型包装类, 类似: "1231231321" , "12"
字符类型的串可以转换成整型包装类, 类似: ‘a‘, ‘8‘, ‘#‘
其他的非数字字符串不可以转换成整型包装类,如按照常规的转换(Integer in = Integer.parseInt("null"))或抛出NumberFormatException类似的转换异常. 类似: "asdfasd", "123AA", "b"," "(空格串),""(注意是空串),
当自己需要进行类型转换时可以考虑先通过正则匹配来目标字符串是否满足转换条件, , 满足条件则 通过BigDecimal对象来构造,获取目标类型的取值.
步骤: 正则匹配 String str , if(满足转换正则条件){ BigDecimal data = http://www.mamicode.com/new BigDecimal(str); return data.IntegerValue(); }
spring的ConversionService可以简要的demo一下.
demo:
package com.boa.test;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
/**
* Created by boa on 2016/8/4.
@desc 用于测试""串转Integer对象的转换异常问题
*/
public class ExceptionForInputStringNull {
public static void main(String[] args) {
Person p = new Person();
String str = "";
//spirng的转换器可以将""转换成Integer的null对象.
ConversionService cs = new DefaultConversionService();
Integer integer = cs.convert(str, Integer.class);
//自行去把""转换成integer对象会报异常
//Integer integer = Integer.valueOf(str);
//apache的转换类也不行
//org.apache.commons.beanutils.Converter con = new IntegerConverter();
//Integer integer = (Integer)con.convert(Integer.class, str);
p.setAge(integer);
System.out.println(p.getAge());
}
}
class Person {
Integer age;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
来自为知笔记(Wiz)
NumberFormatException: For input string: "null"
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。