How do I load static resource from another folder using Spring Boot

57 Views Asked by At

I have the following structure

ui
  dist
    index.js
backend
  src
    main
      java
        org
          example
            Application.java

I am trying to present index.js as a static resource. I tried the following in my application...

public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/ui/*")
            .addResourceLocations("file:///../ui/dist");
}

But this doesn't host the file like I would expect.

How do I host a relative file as a static resource using Spring Boot?

1

There are 1 best solutions below

0
Jackie On

This worked for me...

String currentPath = new File(".").getAbsolutePath();
registry.addResourceHandler("/ui/**")
        .addResourceLocations("file:///"+currentPath+"/../ui/dist/");