Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify static resource index.html in Spring Boot?

I made a small webapp and I want to add credits note at the bottom of the index page. It would be easy to remove the note from the html page in the WAR file. So I thought I could modify resource 'index.html' this way:

public class MainPageTransformer implements ResourceTransformer {
    @Override
    public Resource transform(HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain) throws IOException {
        String html = IOUtils.toString(resource.getInputStream(), UTF_8);
        html = html.replace("</body>", "<div style=\"position: absolute; bottom: 5px;\">Autor</div>\n</body>");
        return new ByteArrayResource(html.getBytes());
    }
}

... and:

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/client/index.html")
                .addResourceLocations("classpath:/static/client")
                .resourceChain(false)
                .addTransformer(new MainPageTransformer());
    }
}

After the MainPageTransformer.transform method is executed it throws exception:

java.io.FileNotFoundException: Byte array resource [resource loaded from byte array] cannot be resolved to absolute file path
at org.springframework.core.io.AbstractResource.getFile(AbstractResource.java:114)
at org.springframework.core.io.AbstractResource.getFileForLastModifiedCheck(AbstractResource.java:169)
at org.springframework.core.io.AbstractResource.lastModified(AbstractResource.java:153)
at org.springframework.web.servlet.resource.ResourceHttpRequestHandler.handleRequest(ResourceHttpRequestHandler.java:240)
at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:51)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)

What do I do wrong? Is there a better way to achieve my goal? Thanks a lot!

like image 349
Vojtech Avatar asked Mar 12 '23 17:03

Vojtech


1 Answers

I ran into this today, try using a org.springframework.web.servlet.resource.TransformedResource.

public class MainPageTransformer implements ResourceTransformer {
 @Override
 public Resource transform(HttpServletRequest request, Resource resource, ResourceTransformerChain transformerChain) throws IOException {
   String html = IOUtils.toString(resource.getInputStream(), UTF_8);
   html = html.replace("</body>", "<div style=\"position: absolute; bottom: 5px;\">Autor</div>\n</body>");
   return new TransformedResource(resource, html.getBytes());
  }
}
like image 120
kuhnroyal Avatar answered Apr 01 '23 03:04

kuhnroyal