Magno C
Magno C

Reputation: 2196

Where to put this code when moving to Spring Boot?

I have an Application with this code to adjust a "MultipartConfigElement":

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    private int maxUploadSizeInMb = 5 * 1024 * 1024; // 5 MB

    @Override
    protected Class<?>[] getRootConfigClasses () {
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses () {
        return new Class<?>[]{ WebConfig.class };
    }

    @Override
    protected String[] getServletMappings () {
        return new String[]{"/"};
    }   


    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {

        File uploadDirectory = new File(System.getProperty("java.io.tmpdir"));

        MultipartConfigElement multipartConfigElement =
                new MultipartConfigElement(uploadDirectory.getAbsolutePath(),
                        maxUploadSizeInMb, maxUploadSizeInMb * 2, maxUploadSizeInMb / 2);

        registration.setMultipartConfig(multipartConfigElement);


    }   

}

now I'm moving to SpringBoot created by https://start.spring.io/ :

@SpringBootApplication
public class HadesApplication {

    public static void main(String[] args) {
        SpringApplication.run(HadesApplication.class, args);
    }
}

I think I will not use AbstractAnnotationConfigDispatcherServletInitializer anymore so where to put my old code?

Upvotes: 0

Views: 426

Answers (2)

davidxxx
davidxxx

Reputation: 131456

1) Registering a DispatcherServlet is indeed not required any longer if you depend on the spring-boot-starter-web starter that will initialize the DispatcherServlet with standard values.

2) About the configuration for the Servlet application context specified :

@Override
protected Class<?>[] getServletConfigClasses () {
    return new Class<?>[]{ WebConfig.class };
}

You should move this code into a WebMvcConfigurer implementation such as :

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
     ....
}

3) About the multipart configuration, you can set it with Spring Boot application.properties :

# MULTIPART (MultipartProperties)

spring.servlet.multipart.enabled=true # Whether to enable support of multipart uploads.

spring.servlet.multipart.max-file-size=5MB # Max file size. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.

spring.servlet.multipart.location=${java.io.tmpdir} # Intermediate location of uploaded files.

Here is the actual properties reference.

Upvotes: 0

Darren Forsythe
Darren Forsythe

Reputation: 11411

Looking at that config I believe all settings can be done in the application.properties.

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

MULTIPART (MultipartProperties)

spring.servlet.multipart.enabled=true # Whether to enable support of multipart uploads.
spring.servlet.multipart.file-size-threshold=0 # Threshold after which files are written to disk. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.location= # Intermediate location of uploaded files.
spring.servlet.multipart.max-file-size=1MB # Max file size. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.max-request-size=10MB # Max request size. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.resolve-lazily=false # Whether to resolve the multipart request lazily at the time of file or parameter access.

Upvotes: 1

Related Questions