1

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

JcAm
  • 59
  • 1
  • 9
  • 1
    Does this answer your question? [Spring Boot unable to serve static image from resource folder](https://stackoverflow.com/questions/41691770/spring-boot-unable-to-serve-static-image-from-resource-folder) – Alien Nov 17 '21 at 15:16
  • no , i was reading that before making this post but still it didnt work – JcAm Nov 17 '21 at 15:25
  • In the above link/answer,the path to the resource folder is prefixed with `classpath:` - in your code above is prefixed with `file:` - why are you using `file:` ? – blurfus Nov 17 '21 at 16:36
  • because is external folder, like is outside src – JcAm Nov 17 '21 at 16:37
  • And what is this `/api` prefix doing? – k-wasilewski Nov 17 '21 at 19:22
  • is the parent folder, api main folder inside api we got reports folder and inside reports we got screenshots folder with images also inside api we got src/main/java – JcAm Nov 17 '21 at 20:07

1 Answers1

1

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);
       
    }

JcAm
  • 59
  • 1
  • 9