简体   繁体   中英

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM