Vaadin, Spring-Boot and Vaadin add-ons

192 Views Asked by At

In a Vaadin 8.0 project together with Spring boot, I am trying to use a Vaadin Add-on (WTPdfViewer, https://github.com/WhitesteinTechnologies/wt-pdf-viewer) for previewing PDF files.

The problem I have is that when trying to view the PDF file, the add-on tries to get the associated javascript resource from what I think is the wrong path. Thus it tries to get it from

http://localhost:8080/my/uipath/APP/PUBLISHED/pdf.worker.js

instead of what I think is the correct:

http://localhost:8080/vaadinServlet/APP/PUBLISHED/pdf.worker.js

The non-spring boot version of the project works correctly, so I assume it has something to do with the way Vaadin add-ons work in a Spring-boot project. Any ideas?

2

There are 2 best solutions below

0
On

It seems that someone else had the same problem and posted the solution here: 404 for js files when using spring boot with vaadin

I am copying from that answer;

In short the solution is to create a controller that forwards the requests to the erroneous url to the correct one.

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class PdfJsRedirectController {
    private static final String WORKER_JS_INCORRECT_PATH = "/APP/PUBLISHED/pdf.worker.js";
    private static final String WORKER_JS_CORRECT_FORWARD_PATH = "forward:/vaadinServlet/APP/PUBLISHED/pdf.worker.js";

    @RequestMapping(value = WORKER_JS_INCORRECT_PATH)
    public String forwardWorkerJsRequestToVaadin() {
        return WORKER_JS_CORRECT_FORWARD_PATH;
    }
}

As for why this happens the linked answer provides a detailed explanation.

0
On

I assume it has something to do with the way Vaadin add-ons work in a Spring-boot project.

No it is not generic issue with add-ons with Spring Boot. This specific add-on has slight flaw, that it has hardcoded path to js-file here:

https://github.com/WhitesteinTechnologies/wt-pdf-viewer/blob/master/src/main/java/com/whitestein/vaadin/widgets/wtpdfviewer/client/WTPdfViewerWidget.java#L920

The workaround you have found helps with this issue.

Better approach would be to issue patch to the add-on and parametrize the path or do something other appropriate corrections.