The first rule of thumb when building web apps is to keep Spring controllers as light as possible. We can think of them as converters between HTTP traffic and our system.
To do that, we need to create a separate ImageService, as shown here, and let it do all the work:
@Service public class ImageService { private static String UPLOAD_ROOT = "upload-dir"; private final ResourceLoader resourceLoader; public ImageService(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } ... }
This last Spring service can be described as follows:
- @Service: This indicates this is a Spring bean used as a service. Spring Boot will automatically scan this class and create an instance.
- UPLOAD_ROOT: This is ...