2

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?

Alejandro
  • 766
  • 9
  • 24

1 Answers1

6

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

cgrim
  • 4,890
  • 1
  • 23
  • 42
  • I get the error: `Message: Unrecognizable resource path: "classpath:data"` even though inside my JAR there definitely is the data directory containing the static files. – GACy20 Mar 15 '21 at 14:27