Issue
i got this project structure :
api
src/main/java -some classes reports / -screenshots - imageOne.png
i want to serve the imageOne.png like this localhost:8080/imageOne.png
i got this configuration
@Configuration
public class Configurer implements WebMvcConfigurer {
private static final Logger LOG = LoggerFactory.getLogger(Configurer.class);
private final String RESOURCE_LOCATION = "file:/api/reports/screenshots";
private final String ANT_PATH_EXPRESSION="/**";
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler(ANT_PATH_EXPRESSION)
.addResourceLocations(RESOURCE_LOCATION);
}
/**
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(ApiRestApplication.class);
}
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
but is not working and everytime i do localhost:8080/imageOne.png is returning:
There was an unexpected error (type=Not Found, status=404).
i need help to find where i mess up in the code
Solution
was able to solve it by adding the folder to the buildpath in maven using ${basedir}/reports/screenshots to make it relative instead of static i tested it using different operative system. Here is the "code"
<build>
<finalName>somename</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>${basedir}/reports</directory>
</resource>
</resources>
and adding this on the application.properties
spring.resources.static-locations=classpath:/
and this is the custom configuration
//private final String RESOURCE_LOCATION = "classpath:/";
private final String ANT_PATH_EXPRESSION="reports/**";
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler(ANT_PATH_EXPRESSION) ;
//.addResourceLocations(RESOURCE_LOCATION);
}
Answered By - JcAm