Search code examples
javamicronaut

Micronaut: Make files inside resources publicly available


I have a small micronaut application where I need to serve files inside resources. Those files should be publicly accessible, so if I enter the url for that file, it would open directly in the browser (they are small images).

I tried using

micronaut:
  application:
    name: myapp
  router.static-resources:
    enabled: true
    paths: classpath:data
    mapping: "/**"

but the response is always the same:

{
  "message": "Page Not Found",
  "_links": {
    "self": {
      "href": "/data/per.svg",
      "templated": false
    }
  }
}

What additional configuration do I need?


Solution

  • There are two issues in your configuration:

    1. You have micronaut.router.static-resources.enabled but it should be micronaut.router.static-resources.default.enabled. So the default is missing.
    2. You are mapping your static resources stored in the data directory in the class path to root / web path. So you can access that per.svg file on http://localhost:8080/per.svg.

    But it is better to use separate context then root to prevent conflict with controller paths. So, you can map it to static for example:

    micronaut:
      application:
        name: myapp
      router:
        static-resources:
          default:
            enabled: true
            mapping: "/static/**"
            paths: classpath:data
    

    Then you can access it on http://localhost:8080/static/per.svg