如何通过Spring Boot将配置参数传递到前端?

发布于 2025-01-28 02:11:11 字数 1622 浏览 1 评论 0原文

我有一个问题。我创建了2个配置文件。我已经可以在application.properties中使用参数。我的目标是能够在前端显示和编辑参数。就像任何应用程序的普通设置一样。不幸的是,我没有确切的想法如何实现。

有人对我有一个想法,如何通过Restapi将配置发送到前端?

我的应用程序。

#ConfigMaintenance
config.maintenance.next-Available-Update-Version=1.28.5
config.maintenance.next-Available-Update-Date= 18.05.2022
config.maintenance.automatic-update= true
config.maintenance.update-Api-Token= sk021=!2jsn?=jacmw
config.maintenance.latest-Version= 1.28.4

#ConfigGeneral
config.general.volume=56
config.general.dark-Mode=false
config.general.night-Mode=false
config.general.fps=144
config.general.screen-Resolution=1920 x 1080
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import lombok.Data;

@Configuration
@ConfigurationProperties(prefix = "config.general")
@Data
public class ConfigGeneral {
    private int volume;
    private Boolean nightModus;
    private Boolean darkMode;
    private int fps;
    private String screenResolution;

}
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import lombok.Data;

@Configuration
@ConfigurationProperties(prefix = "config.maintenance")
@Data
public class ConfigMaintenance {
    private String nextAvailableUpdateVersion;
    private String nextAvailableUpdateDate;
    private Boolean automaticUpdate;
    private String updateApiToken;
    private Boolean latestVersion;

}

基本上我会对解决方案感兴趣,但是一个好的报告甚至只是建议对我来说就足够了:)。

提前致谢

I have a question. I have created 2 config files. I can already use the parameters in the application.properties. My goal is to be able to display and edit the parameters later in the frontend. Like normal settings of any application. Unfortunately, I do not have an exact idea how I can implement this.

Does anyone have an idea for me how I can send my config via RestApi to the frontend?

My application.properties

#ConfigMaintenance
config.maintenance.next-Available-Update-Version=1.28.5
config.maintenance.next-Available-Update-Date= 18.05.2022
config.maintenance.automatic-update= true
config.maintenance.update-Api-Token= sk021=!2jsn?=jacmw
config.maintenance.latest-Version= 1.28.4

#ConfigGeneral
config.general.volume=56
config.general.dark-Mode=false
config.general.night-Mode=false
config.general.fps=144
config.general.screen-Resolution=1920 x 1080
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import lombok.Data;

@Configuration
@ConfigurationProperties(prefix = "config.general")
@Data
public class ConfigGeneral {
    private int volume;
    private Boolean nightModus;
    private Boolean darkMode;
    private int fps;
    private String screenResolution;

}
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import lombok.Data;

@Configuration
@ConfigurationProperties(prefix = "config.maintenance")
@Data
public class ConfigMaintenance {
    private String nextAvailableUpdateVersion;
    private String nextAvailableUpdateDate;
    private Boolean automaticUpdate;
    private String updateApiToken;
    private Boolean latestVersion;

}

Basically I would be interested in a solution, but a good report or even just a suggestion would be enough for me :).

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野鹿林 2025-02-04 02:11:11

我创建了2个配置文件。我已经可以在
application.properties。我的目标是能够显示和编辑
后方的参数。像任何正常设置
应用程序。

我将在此处添加我的通知,因为如何将它们传递给UI的问题可能是有效的,但是结果可能对您来说是错误的。

在Spring Boot应用程序中,您使用@configuration使用的属性和加载的属性
@configurationProperties(prefix =“ config.general”)仅在春季上下文初始化中加载,该初始化发生在应用程序启动时。因此,考虑到这些属性已经存在于您的application.properties文件中,您的前端将无法在某个地方持续存在更改的属性。

1对于此要求,我看到的可能情况是,这些属性是从数据库中加载的,而不是application.properties,然后在编辑时,将编辑数据库记录以持久编辑。然后,您必须重新启动应用程序,或者尝试手动刷新春季上下文,以使这些属性生效。对于此解决方案,您只需从spring-boot应用程序创建一个端点,该应用程序提供了数据库中存在的所有属性,然后再提供一个用于编辑这些属性的端点。就像简单的CRUD操作。

2可能的情况,但可能有点过分杀伤是使用cloud Config Server。搜索Spring Cloud BusSpring Actuator刷新以查看如何将某些包装盒解决方案应用于需要动态属性的应用程序。

I have created 2 config files. I can already use the parameters in the
application.properties. My goal is to be able to display and edit the
parameters later in the frontend. Like normal settings of any
application.

I will add my notice here, because the question of how to pass them to UI may be valid but the outcome may be wrong for you.

In a Spring Boot application, the properties that you use and load via @Configuration
@ConfigurationProperties(prefix = "config.general") are loaded only during spring context initialization which happens when the application starts up. So considering that those properties already exist in your application.properties file, your Frontend would not be able to persist the changed properties somewhere.

1 Possible scenario I see for this requirement, is that those properties are loaded from Database and not application.properties, and then when edited the database records are edited to be persistent. Then you have to either restart the application or try to manually refresh the spring context for those properties to take effect. For this solution you could just create an endpoint from your spring-boot application that delivers all properties that exist in database and then another endpoint to edit those properties. Like simple crud operations.

2 Possible scenario but may be a bit overkill is to use cloud config server. Search for spring cloud bus and spring actuator refresh to see how you can apply some out of the box solutions for an application that needs dynamic properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文