1

I'm running a Spring Boot application with Jetty server. The APIs are served by the Jersey servlet. The static contents are placed in src/main/static and src/main/private-static.

Issue#1 Static content in src/main/static is not being served

The content in src/main/static is public and can be accessed freely. According to this: https://docs.spring.io/spring-boot/docs/1.3.8.RELEASE/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-static-content Spring boot should automatically serve the static content, but it does not.

Issues#2 How to set the static content directory to src/main/private-static for this custom servlet that checks for authentication before serving a file.

I've created another servlet to serve static content with authentication

@Bean
public ServletRegistrationBean<HttpServlet> privateStaticContent() {
    return new ServletRegistrationBean<>(new HttpServlet() {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
            int status = isAuthenticated(req) ? 200 : 401;

            resp.setStatus(status);
        }
    }, "/private-static/*");
}

Basically this servlet should serve files from the directory src/main/private-static, but I'm not sure how and where to configure this.

Issue#3 To make Jersey serve at /api endpoint, I've added this in application.properties

server.servlet.context-path=/api

But this makes all the servlets to use this /api endpoint. How can I configure this only for Jersey servlet and not for others. Is there something like server.servlet.jersey.context-path=/api ?

user2354302
  • 1,833
  • 5
  • 23
  • 35

1 Answers1

0

src/main/java/<dir>/<static-content> is not something that maven or gradle would ever see.

the src/main/java tree is only used for *.java, nothing else.

perhaps you should be using src/main/resources/<dir>/<static-content> instead?

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • Yes, that's right. I made a typo in my question. I corrected it. Thank you – user2354302 Aug 26 '21 at 11:47
  • Have you specifically configured gradle or maven for `src/main/static` or `src/main/private-static`? As those are also not used by maven/gradle, and content within those directories won't be found by your classes. If you are packaging for jar, use `src/main/resources/static` or if you are packaging for war use `src/main/webapp/static` – Joakim Erdfelt Aug 26 '21 at 12:48