简体   繁体   English

当应用程序部署为后台服务(jar)时,spring-boot提供静态内容

[英]spring-boot serve static content when app is deployed as a background service (jar)

I'm having problems in serving static content with spring-boot. 我在使用spring-boot提供静态内容时遇到问题。

I have configured static content to be served through application.properties file like so: 我已将静态内容配置为通过application.properties文件提供,如下所示:

static-location-path=/opt/myapp/static
spring.resources.static-locations=file:${static-location-path}/

Notice that it is outside of the jar file. 请注意,它在jar文件之外。 Now if I launch my application with mvn spring-boot plugin like so: 现在,如果我使用mvn spring-boot插件启动应用程序,如下所示:

mvn spring-boot:run -Drun.profiles=staging

Then the static files are served correctly, and I can access them. 然后可以正确提供静态文件,我可以访问它们。

But I do not want to run my application like this on the staging environment, but instead as a service, that can be automatically started on server boot. 但是我不想在登台环境中像这样运行我的应用程序,而是作为一种服务,可以在服务器启动时自动启动。 So I managed to build the application to a executable jar. 因此,我设法将应用程序构建为可执行jar。 Then I managed to launch it as an init.d service. 然后,我设法将其作为init.d服务启动。 The application is working otherwise correct, but it does no longer serve the static files. 该应用程序在其他方面可以正常运行,但是不再提供静态文件。 I checked, and the correct profile file (which determines which application.properties file should be used) should be loaded there correctly. 我检查了一下,正确的配置文件(确定应该使用哪个application.properties文件)应该正确地加载到那里。

Main question is: How can I serve static content with spring-boot when the application is launched as a init.d service (jar), and I'm using application.properties configuration to serve the static content? 主要问题是: 当应用程序作为init.d服务(jar)启动时,如何使用spring-boot来提供静态内容,而我正在使用application.properties配置来提供静态内容?

Maybe there's something I don't understand here. 也许有些东西我在这里不明白。 Supplementary questions related to this are: 与此相关的补充问题是:

  1. Are the different application.properties files (under resources folder) also bundled inside the jar? jar中是否还捆绑了不同的application.properties文件(在resources文件夹下)? Which would mean they should be found, and application would get the correct configuration values from there? 那意味着应该找到它们,应用程序将从那里获取正确的配置值?

  2. Can spring-boot serve static content at all when the application is bundled as jar and launched as a init.d service? 当应用程序捆绑为jar并作为init.d服务启动时,spring-boot可以完全提供静态内容吗?

  3. How could I debug the problem more? 我该如何进一步调试问题? I know where the app logs are being written, so that helps at least some. 我知道应将应用程序日志写入何处,因此至少可以有所帮助。

This is solved. 解决了。 The static content folder and files were not owned by the user which was used to run the service, so the user had no access to them. 静态内容文件夹和文件不属于用于运行服务的用户所有,因此用户无权访问它们。 Too bad the error received was unclear (404), instead of some permission error. 太糟糕了,收到的错误尚不清楚(404),而不是某些权限错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM