英文:
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
你的配置中存在两个问题:
- 你使用了
micronaut.router.static-resources.enabled
,但应该使用micronaut.router.static-resources.default.enabled
。所以缺少了default
部分。 - 你正在将存储在类路径下的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:
- You have
micronaut.router.static-resources.enabled
but it should bemicronaut.router.static-resources.default.enabled
. So thedefault
is missing. - 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论