mp_92
mp_92

Reputation: 51

Serve pdf as static resource from spring application running in docker container

The Pdf file is blank when I try to open to it from localhost/pdfs/filename.pdf, but when I do that for the picture it works (localhost/images/imagename.png) My app is running inside a docker container.

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

   @Autowired
   private ApplicationEventPublisher publisher;

   @Override
   public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/images/**").addResourceLocations("classpath:/static/images/").setCachePeriod(31556926);
    registry.addResourceHandler("/pdfs/**").addResourceLocations("classpath:/static/pdfs/").setCachePeriod(31556926);

   }
}

Both folders, images and pdfs, are inside src/main/resources/static folder.

Why does it work for the image file but it doesn't work for pdf file? Thanks.

Upvotes: 3

Views: 1761

Answers (1)

scalkyo
scalkyo

Reputation: 21

I had the same problem - it was caused by maven filtering the resources. Excluding *.pdf helped solve the problem:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <configuration>
        <delimiters>
            <delimiter>@</delimiter>
        </delimiters>
        <useDefaultDelimiters>false</useDefaultDelimiters>
        <nonFilteredFileExtensions>
            <nonFilteredFileExtension>pdf</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
    </configuration>
</plugin>

Upvotes: 2

Related Questions