解决 Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class

今天在使用springmvc 上传文件时出现如下错误

Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.multipart.commons.CommonsMultipartFile]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.web.multipart.commons.CommonsMultipartFile.<init>()
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:108)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:136)
    at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:101)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    ... 26 more
Caused by: java.lang.NoSuchMethodException: org.springframework.web.multipart.commons.CommonsMultipartFile.<init>()
    at java.lang.Class.getConstructor0(Class.java:2715)
    at java.lang.Class.getDeclaredConstructor(Class.java:1987)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:105)
    ... 37 more

controller方法写法

    @RequestMapping(value = "upload")
    public @ResponseBody
    String csvUpload(CommonsMultipartFile csvFile) throws Exception {

原因是controller 方法中参数没有使用添加注解@RequestParam

正确写法如下、

    @RequestMapping(value = "upload")
    public @ResponseBody
    String csvUpload(@RequestParam CommonsMultipartFile csvFile) throws Exception {

第二种方法就是使用对象类作为参数

    @RequestMapping(value = "upload")
    public @ResponseBody
    String csvUpload(UploadInfo uploadInfo) throws Exception {
        ……
    }
   
    public class UploadInfo {
        private CommonsMultipartFile csvFile;
       
        //get方法
       
        //set方法
    }

这样就不需要使用@RequestParam注解了。

来源://作者:/更新时间:2015-06-19
相关文章