没有找到默认构造函数异常

时间:2016-10-18 16:23:27

标签: java spring jersey default-constructor

我正在使用java spring 4框架开发一个Web应用程序。有一次,我正在使用由球衣提供的FormDataContentDisposition类。这个类在我的REST调用的端点上使用,如下所示,

@RequestMapping(value = "/createArticle/", method = RequestMethod.POST)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public ResponseEntity<Void> createNewArticle(@FormDataParam("file") InputStream uploadedInputStream,
                                             @FormDataParam("file") FormDataContentDisposition fileDetails)

当我进行REST调用时,Apache Tomcat发出异常说, “无法实例化[com.sun.jersey.core.header.FormDataContentDisposition]:找不到默认构造函数”

由于FormDataContentDisposition没有默认构造函数,我认为这个例外也会出现,同样根据这个artcle http://javarevisited.blogspot.in/2014/01/why-default-or-no-argument-constructor-java-class.html,应该有一个默认的构造函数。我无法创建默认构造函数,因为此类(FormDataContentDisposition)位于jar文件中。

帮助我克服这个问题

1 个答案:

答案 0 :(得分:0)

I think you are correct in the cause of the problem. There is no way in which you can instantiate an object of this class without arguments to the constructor, so you need to figure out, in Spring 4, how to feed an argument to the construction of that class and do so.

The article you refer to delivers the opinion that ALL classes should have a default constructor. It is no kind of rule in Java; I don't even think it qualifies as best practice. I also don't think the article has any bearing on your problem.