如何通过Spring Boot将配置参数传递到前端?
我有一个问题。我创建了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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将在此处添加我的通知,因为如何将它们传递给UI的问题可能是有效的,但是结果可能对您来说是错误的。
在Spring Boot应用程序中,您使用
@configuration
使用的属性和加载的属性@configurationProperties(prefix =“ config.general”)
仅在春季上下文初始化中加载,该初始化发生在应用程序启动时。因此,考虑到这些属性已经存在于您的application.properties
文件中,您的前端将无法在某个地方持续存在更改的属性。1对于此要求,我看到的可能情况是,这些属性是从数据库中加载的,而不是
application.properties
,然后在编辑时,将编辑数据库记录以持久编辑。然后,您必须重新启动应用程序,或者尝试手动刷新春季上下文,以使这些属性生效。对于此解决方案,您只需从spring-boot
应用程序创建一个端点,该应用程序提供了数据库中存在的所有属性,然后再提供一个用于编辑这些属性的端点。就像简单的CRUD操作。2可能的情况,但可能有点过分杀伤是使用
cloud Config Server
。搜索Spring Cloud Bus
和Spring Actuator刷新
以查看如何将某些包装盒解决方案应用于需要动态属性的应用程序。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 yourapplication.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 yourspring-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 forspring cloud bus
andspring actuator refresh
to see how you can apply some out of the box solutions for an application that needs dynamic properties.