Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
Error assembling WAR: webxml attribute is required
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
Problem Description
In the process of SpringMVC learning, using full annotation development, configuration file upload and download classes, such an error occurs
Cause Analysis and Solutions
1. Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
The problem shown is where the file upload parser is configured in the WebConfig class I configured
//配置文件上传解析器
@Bean
public CommonsMultipartResolver multipartResolver(){
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
return commonsMultipartResolver;
}
First of all, this problem is because in the pom.xml file, it is indeed related to the configuration of commons-fileupload. After adding this, reload it.
<!--添加fileupload依赖-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
2. Error assembling WAR: webxml attribute is required
After I solved the first step, this problem came out again. I wondered if it was because I added the pom.
So I am going to repackage, but, no, I don’t have a web.xml file at all. To
package a war project, it must point to a web.xml file. If there is no one, an error will be reported. The path of the web.xml file can be configured as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>webapps\WEB-INF\web.xml</webXml>
</configuration>
</plugin>
Since I use annotations for development and use annotation classes instead of configuration classes (web.xml, SpringMVC.xml), I need to use a plugin configuration in pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
</plugins>
</build>