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

6
  • 1
    Does this answer your question? Spring Boot unable to serve static image from resource folder Commented Nov 17, 2021 at 15:16
  • no , i was reading that before making this post but still it didnt work Commented Nov 17, 2021 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: ? Commented Nov 17, 2021 at 16:36
  • because is external folder, like is outside src Commented Nov 17, 2021 at 16:37
  • And what is this /api prefix doing? Commented Nov 17, 2021 at 19:22

1 Answer 1

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); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.