错误“java.lang.NumberFormatException:对于输入字符串:”“”将JSON数组传递给Spring-MVC

时间:2012-05-31 19:56:07

标签: java ajax json spring spring-mvc

我收到错误“java.lang.NumberFormatException:对于输入字符串:”“”将JSON数组传递给Spring-MVC ..

这是我的JSON JavaScript调用:

function doAjaxPost() {  

      var inData = {};

      inData.nameList = ['kurt','johnathan'];
      inData.educationList = ['GSM','HardKnocks'];

      htmlStr = JSON.stringify(inData);
      alert(htmlStr);

      $.post( contexPath + "/AddUser.htm", inData, function(outData, outStatus){
          alert(outStatus);

      });
};

这是我在Java中的bean:

public class User {

    private String name = null;
    private String education = null;

    private List nameList = new ArrayList();
    private List educationList = new ArrayList();

    public List getNameList() {
        return nameList;
    }

    public List getEducationList() {
        return educationList;
    }

    public void setNameList(List nameList) {
        this.nameList = nameList;
    }

    public void setEducationList(List educationList) {
        this.educationList = educationList;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEducation() {
        return education;
    }
    public void setEducation(String education) {
        this.education = education;
    }

}

这是我的Java控制器,Spring-MVC:

@RequestMapping(value="/AddUser.htm",method=RequestMethod.POST)
    public @ResponseBody JsonResponse addUser(@ModelAttribute(value="user") User user, BindingResult result ){
        JsonResponse res = new JsonResponse();

        if(!result.hasErrors()){
            res.setStatus("SUCCESS");
            res.setResult(userList);
        }else{
            res.setStatus("FAIL");
            res.setResult(result.getAllErrors());
        }

        return res;
    }

以下是我得到的错误:

SEVERE: Servlet.service() for servlet Spring MVC Dispatcher Servlet threw exception
java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:493)
    at java.lang.Integer.parseInt(Integer.java:514)
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:952)
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:896)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
    at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:673)
    at org.springframework.validation.DataBinder.doBind(DataBinder.java:569)
    at org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:190)
    at org.springframework.web.bind.ServletRequestDataBinder.bind(ServletRequestDataBinder.java:110)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.doBind(AnnotationMethodHandlerAdapter.java:712)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doBind(HandlerMethodInvoker.java:804)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:358)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:170)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:774)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:679)

1 个答案:

答案 0 :(得分:1)

我很确定你的bean中缺少泛型是问题所在:

private List nameList = new ArrayList();
private List educationList = new ArrayList();

应该是:

private List<String> nameList = new ArrayList<String>();
private List<String> educationList = new ArrayList<String>();
// change getters and setters also

JSON映射器将查看字段的泛型类型,以确定它是否可以映射值。