Logo

dev-resources.site

for different kinds of informations.

Serving static assets with Micronaut

Published at
2/1/2024
Categories
micronaut
java
Author
glaforge
Categories
2 categories in total
micronaut
open
java
open
Author
8 person written this
glaforge
open
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.

micronaut Article's
30 articles in total
Favicon
Pub-sub Redis in Micronaut
Favicon
Choosing the Right Java Microservices Framework: Spring Boot, Quarkus, Micronaut, and Beyond
Favicon
Micronaut vs. Spring Boot: A Detailed Comparison
Favicon
[Micronaut] Receiving Japanese(Shift-JIS) data as HTTPResponse
Favicon
[Micronaut] Try Cookie
Favicon
[Micronaut] Generating and downloading files as ZIP or JSON
Favicon
Sending files from Micronaut applications
Favicon
[Micronaut] Receiving multipart/form-data
Favicon
Try Micronaut and Doma2
Favicon
[Micronaut] Accessing SQL Server 2
Favicon
Serving static assets with Micronaut
Favicon
Run a Micronaut application on Tomcat
Favicon
[Micronaut] Accessing SQL Server 3
Favicon
[Micronaut] Accessing SQL Server 1
Favicon
Try Micronaut
Favicon
Adding AI to your Micronaut search service
Favicon
Swagger-Operator, let groovy operate your cluster
Favicon
Jugando con Mono y Flux (de Reactor)
Favicon
Meeting & Mastering Java
Favicon
Integration testing in Micronaut
Favicon
mapping OpenAPI formats
Favicon
Connecting Micronaut to MongoDB database using Kotlin
Favicon
πŸ₯‰ Micronaut: Top 5 Server-Side Frameworks for Kotlin in 2022
Favicon
AWS Lambda SnapStart - Part 2 Measuring Java 11 Lambda cold starts with Micronaut framework
Favicon
Integration Tests with Micronaut and Kotlin
Favicon
Mapping with SCHEMA
Favicon
annotation mapping & bean-validation
Favicon
Can Micronaut replace Spring Boot? Let's take a look at an example.
Favicon
Cloud Native Java: Integrating YugabyteDB with Spring Boot, Quarkus, and Micronaut
Favicon
Micronaut Pulsar

Featured ones: