Micronaut:使资源文件内部的文件公开可用

huangapple go评论67阅读模式
英文:

Micronaut: Make files inside resources publicly available

问题

以下是翻译好的内容:

我有一个小的 Micronaut 应用程序,我需要在 resources 内提供文件。这些文件应该是公开可访问的,因此如果我输入该文件的 URL,它会在浏览器中直接打开(它们是小图片)。

我尝试使用以下配置:

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

但是响应始终是相同的:

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

我需要哪些额外的配置?

英文:

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?

答案1

得分: 6

你的配置中存在两个问题:

  1. 你使用了micronaut.router.static-resources.enabled,但应该使用micronaut.router.static-resources.default.enabled。所以缺少了default部分。
  2. 你正在将存储在类路径下的data目录中的静态资源映射到根目录*/的 web 路径。因此,你可以通过http://localhost:8080/per.svg访问该per.svg*文件。

但最好使用与根路径不同的单独上下文,以避免与控制器路径冲突。因此,你可以将其映射到static,例如:

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

然后你可以通过http://localhost:8080/static/per.svg来访问它。

英文:

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

huangapple
  • 本文由 发表于 2020年10月1日 09:19:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64147833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定