DEV Community

Guillaume Laforge
Guillaume Laforge

Posted on • Originally published at glaforge.dev on

Serving static assets with Micronaut

My go-to framework when developing Java apps or microservices is Micronaut. For the apps that should have a web frontend, I rarely use Micronaut Views and its templating support. Instead, I prefer to just serve static assets from my resource folder, and have some JavaScript framework (usually Vue.js) to populate my HTML content (often using Shoelace for its nice Web Components). However, the static asset documentationis a bit light on explanations. So, since I always forget how to configure Micronaut to serve static assets, I thought that would be useful to document this here.

In /src/main/resources/application.properties, I’m adding the following:

micronaut.router.static-resources.default.paths=classpath:public
micronaut.router.static-resources.default.mapping=/**
micronaut.router.static-resources.default.enabled=true

micronaut.server.cors.enabled=true

Enter fullscreen mode Exit fullscreen mode
  • The first line says that my resources will live in src/main/resources/public/.
  • The second line means the pattern will match recursively for sub-directories as well.
  • The enabled flag is to activate static serviing (not strictly needed as it’s supposed to be enabled by default).
  • I also enabled CORS (cross-origin resource sharing).

Then in src/main/resources/public/, I’ll have my index.html file, my css and js folders.

Top comments (1)

Collapse
 
insouciantqualms profile image
InsouciantQualms

+1. Love Micronaut. I have that same setup, but for some reason it is able to serve up smaller assets, like CSS and Javascript, but not larger ones like MP4 videos.