I'm trying to implement handling requests for static resourses in Spring + Angular project. Angular app is running on http://localhost:4200 when Spring app is running on http://localhost:8080.
So when I need to get an image, angular sends a request which looks like this http://localhost:4200/screenShots/image-name.jpg. To handle these kind of requests I've created WebConfig class:
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String path = "file:///" + *path to folder with images on my PC*
registry.addResourceHandler("/screenShots/**")
.addResourceLocations(path);
}
}
And it doesn't work, I get 404 (Not Found) error. I've experimented with addind and removing @EnableWebMvc and @CrossOrigin annotations, tried to add addCorsMappings() method in my class like that, nothing helped.
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/screenShots/**")
.allowedOrigins("http://localhost:4200");
}
However when I put http://localhost:8080/screenShots/image-name.jpg (changed :4200 to :8080) string in Google address bar, it returns me my image as intended, so I wonder is there a way to add some kind of @CrossOrigin annotation alternative for resourse handlers cuz I haven't found any solution in the Internet yet. Thanks!
Github repo for those, who are curious (only gameImagesRelease branch contains current piece of code): https://github.com/Vladyslav-Bahlai/OktenGames/tree/gameImagesRelease