搭建springboot后,新建控制器后,访问控制器接口,控制台报错:
Unable to process parts as no multi-part configuration has been provided
报错为:无法处理部件,未提供多部件配置。
看错误中的 multi-part 中字眼就知道是上传文件的组件没有注入到springboot中自定义的servlet中去即可。
registrationBean.setMultipartConfig(multipartConfigElement);
完整代码:
在你的启动类Application中的自定义servlet中添加代码:
//自动注入springboot的默认上传配置
@Autowired
private MultipartConfigElement multipartConfigElement;
@Bean
public ServletRegistrationBean<DispatcherServlet> servletMvc() {
//注解扫描上下文
AnnotationConfigWebApplicationContext applicationContext
= new AnnotationConfigWebApplicationContext();
//扫描包
applicationContext.scan("com.xx.com");
//通过构造函数指定dispatcherServlet的上下文
DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);
//用ServletRegistrationBean包装servlet
ServletRegistrationBean<DispatcherServlet> registrationBean
= new ServletRegistrationBean<>(dispatcherServlet);
registrationBean.setLoadOnStartup(0);
registrationBean.setMultipartConfig(multipartConfigElement);
//指定name,如果不指定默认为dispatcherServlet
registrationBean.setName("rest");
return registrationBean;
}