Tôi đã cố gắng sử dụng CommonsMultipartResolver trong Boot dịch ứng dụng cũ của tôi (WAR) để khởi động, và ngay bây giờ nó có đoạn mã sau:Làm thế nào để sử dụng CommonsMultipartResolver trong mùa xuân Boot
@Configuration
public class TestConfig {
@Bean
public FilterRegistrationBean openEntityManagerFilterRegistrationBean() {
// Set upload filter
final MultipartFilter multipartFilter = new MultipartFilter();
final FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(multipartFilter);
filterRegistrationBean.addInitParameter("multipartResolverBeanName", "commonsMultipartResolver");
return filterRegistrationBean;
}
@Bean
public CommonsMultipartResolver commonsMultipartResolver() {
final CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
commonsMultipartResolver.setMaxUploadSize(-1);
return commonsMultipartResolver;
}
}
Đây có phải là một cách đúng đắn trong Khởi động, khiến tôi thấy một số thuộc tính được áp dụng trong application.properties. Họ sẽ có cùng một mục đích hơn là xác định một FilterRegistrationBean?
# MULTIPART (MultipartProperties)
multipart.enabled=true
multipart.file-size-threshold=0 # Threshold after which files will be written to disk.
multipart.location= # Intermediate location of uploaded files.
multipart.max-file-size=1Mb # Max file size.
multipart.max-request-size=10Mb # Max request size.
Có thể cung cấp bất kỳ mẫu nào làm cách nào để sử dụng không? Cảm ơn.
Bằng cách này, Nó cố gắng để thiết lập thuộc tính "multipart.enabled = true" và tôi đã nhận:
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'enabled' of bean class [org.springframework.boot.autoconfigure.web.MultipartProperties]: Bean property 'enabled' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1076)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:927)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95)
at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:749)
at org.springframework.validation.DataBinder.doBind(DataBinder.java:645)
at org.springframework.boot.bind.RelaxedDataBinder.doBind(RelaxedDataBinder.java:121)
at org.springframework.validation.DataBinder.bind(DataBinder.java:630)
at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:253)
at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:227)
at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:296)
... 73 common frames omitted
Thực tế cấu hình đó là thiếu sót, tên của bean phải là 'multipartResolver' thay vì' comonsMultipartResolver' nếu bạn muốn tích hợp đúng với 'DispatcherServlet'. Ngoài ra bộ lọc không bắt buộc (chỉ khi bạn kết hợp nó với Spring Security). –
Thực sự không phải là thuộc tính 'đã bật' trong lớp đó, tuy nhiên thuộc tính phải khớp với [this] (https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src /main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java#L51) để bật/tắt cấu hình nhiều phần. Vì vậy, về cơ bản tôi xem xét rằng một vấn đề trong phần cấu hình nhiều phần của Spring Boot. Ngoài ra không phải tất cả các thuộc tính đều cần phải có một lớp tương ứng để được ánh xạ tới, cũng có các phương tiện khác của việc sử dụng các thuộc tính được định nghĩa trong tệp 'application.properties'. –
Nếu bạn xem tài liệu về Spring Boot http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html có thuộc tính "multipart.enabled = true" và có nội dung giá trị được bật. Tại sao nó tồn tại nếu nó không hợp lệ như vậy? –