0

I'm trying to build a webapp with plain JavaEE. I'm rendering the frontend using FTL. For this I used jax-rs endpoints like the following:

@RequestScoped
@Path("/{lang}")
public class MarkupResource {


    @GET
    @Produces(MediaType.TEXT_HTML)
    public Response index(@PathParam("lang") String lang) {
        try (Writer stringWriter = new StringWriter()) {
            markupBuildService.buildIndex(stringWriter, lang);
            return Response.ok(stringWriter.toString()).type(MediaType.TEXT_HTML).build();
        } catch (IOException ioException){
            System.out.println(ioException.getMessage());
            //return 404 page
            return Response.serverError().entity(ioException.getMessage()).build();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            //return 500 page
            return Response.serverError().entity(e.getMessage()).build();
        }
    }

my static content (css, js ...) is located in the webapp folder.

now my problem kicks in: I have to use the generated JAXRSConfiguration

@ApplicationPath("/fe")
public class JAXRSConfiguration extends Application {

}

because when i'm not, it doesnt resolve my static content because it's looking for a jaxrs endpoint.

when forwarding my user via a webserver (nginx e.g) it pushes me to the correct domain (e.g. http:mypage.com/ pointing to localhost:8080/shop/fe/lang ) but then it's unable to resolve the path to my css & js.

I'm getting the contextPath via

public class FW {
    String contextPath0;
    String contextPath;

    FW(HttpServletRequest request) {
        contextPath0 = request.getContextPath();
        contextPath = contextPath0 + "/";
    }

    public String getContextPath() {
        return contextPath0;
    }

    public String url(String path) {
        if (path.startsWith("/")) return contextPath0 + path;
        return contextPath + path;
    }
}

and using it like this:

<link rel="stylesheet" type="text/css" href="${fw.contextPath}/css/style.css" />

Do you have an idea how to resolve this issue?

Thanks in advance!

enter image description here

R. Polito
  • 544
  • 6
  • 21
  • Not sure what server you're using, but Jersey and RESTEasy both have the option to configure the app as a servlet filter so that you can forward requests to static content when using /* as the api root path. https://stackoverflow.com/q/12422660/2587435 https://stackoverflow.com/q/30111978/2587435 . Or you might be able to use a url rewrite filter https://stackoverflow.com/a/45603322/2587435 – Paul Samsotha Apr 16 '21 at 23:14
  • I am using wildfly – R. Polito Apr 17 '21 at 18:03
  • Second or third link might be useful – Paul Samsotha Apr 17 '21 at 18:24
  • Thank you! I'll try it later – R. Polito Apr 17 '21 at 18:45

0 Answers0