ssingh
ssingh

Reputation: 1

resteasy-servlet-spring-boot-starter with springboot how to serve static content

I'm using resteasy-servlet-spring-boot-starter 6.2.0.Final with springboot 3.2.2. I'm not sure how can we serve static files from /META-INF/resources/api. https://docs.jboss.org/resteasy/docs/6.2.9.Final/userguide/ suggests that we can use filter org.jboss.resteasy.plugins.server.servlet.FilterDispatcher but it doesn't seem to help as the servlet HttpServlet30Dispatcher just sends not-found response back.

Tried using FilterDispatcher but the system doesn't have default servlet rather it has one produced by HttpServlet30Dispatcher() that just dispatches with exception message "Could not find resource for full path:......"

Upvotes: -1

Views: 34

Answers (1)

ssingh
ssingh

Reputation: 1

Instead of using Filter, I put the following code (needs improvements but it works) for serving static resources.

@Component
@Path("")
public class StaticResourcesResource
{
    @Inject
    ServletContext context;

    @GET
    //    @Path("{path: ^api\\/.*}")
    @Path("{path: ^api.*}")
    public Response staticResources(@PathParam("path") final String path)
    {

        // FIXME there must be better way to make this response
        String processedPath = null;
        if (path.equals("api") || path.equals("api/"))
        {
            processedPath = "api/index.html";
        }
        else
        {
            processedPath = path;
        }

        InputStream resource = context.getResourceAsStream(String.format("%s", processedPath));

        String contentType = null;
        if (processedPath.endsWith(".html"))
        {
            contentType = "text/html";
        } else if (processedPath.endsWith(".js"))
        {
            contentType = "application/javascript";
        }
        else if (processedPath.endsWith(".css"))
        {
            contentType = "text/css";
        }
        else if (processedPath.endsWith(".png"))
        {
            contentType = "image/png";
        }

        return Objects.isNull(resource)
                ? Response.status(NOT_FOUND).build()
                : Response.ok().entity(resource).type(contentType).build();
    }
}

Upvotes: 0

Related Questions