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.

原文链接:Serving static assets with Micronaut

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容