javascript
ssm 返回json配置_摆脱困境:将运行时配置作为JSON返回
ssm 返回json配置
如果需要確定部署到遠程服務器的Spring Web應用程序的運行時配置,則需要讀取從遠程服務器找到的屬性文件。 這很麻煩。
幸運的是,有更好的方法。 這篇博客文章描述了我們如何
讓我們開始吧。
如果使用Spring Boot,則應使用Spring Boot Actuator 。 它提供了其他功能,可幫助您監視和管理Spring Boot應用程序。
如果您還沒有閱讀我的博客文章,標題為:《 從溝壑中反彈:將屬性值注入到配置Bean中》 , 那么您應該先閱讀它,然后再繼續閱讀此博客文章 。 它提供了有助于您理解此博客文章的其他信息。
將運行時配置寫入日志文件
通過執行以下步驟,我們可以將運行時配置寫入日志文件:
讓我們找出如何完成這些步驟。
首先 ,我們必須在WebProperties類中添加toString()方法,并使用ToStringBuilder類實現此方法。
完成此操作后, WebProperties類的源代碼如下所示:
import org.apache.commons.lang3.builder.ToStringBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;@Component public final class WebProperties {private final String protocol;private final String serverHost;private final int serverPort;@Autowiredpublic WebProperties(@Value("${app.server.protocol}") String protocol,@Value("${app.server.host}") String serverHost,@Value("${app.server.port}") int serverPort) {checkThatProtocolIsValid(protocol);this.protocol = protocol;this.serverHost = serverHost;this.serverPort = serverPort;}private void checkThatProtocolIsValid(String protocol) {if (!protocol.equalsIgnoreCase("http") && !protocol.equalsIgnoreCase("https")) {throw new IllegalArgumentException(String.format("Protocol: %s is not allowed. Allowed protocols are: http and https.",protocol));}}public String getProtocol() {return protocol;}public String getServerHost() {return serverHost;}public int getServerPort() {return serverPort;}@Overridepublic String toString() {return new ToStringBuilder(this).append("protocol", this.protocol).append("serverHost", this.serverHost).append("serverPort", this.serverPort).toString();} }其次 ,我們必須將toString()方法添加到ApplicationProperties類并使用ToStringBuilder類實現它。
在對ApplicationProperties類進行了這些更改之后,其源代碼如下所示:
import org.apache.commons.lang3.builder.ToStringBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;@Component public final class ApplicationProperties {private final String name;private final boolean productionModeEnabled;private final WebProperties webProperties;@Autowiredpublic ApplicationProperties(@Value("${app.name}") String name,@Value("${app.production.mode.enabled:false}") boolean productionModeEnabled,WebProperties webProperties) {this.name = name;this.productionModeEnabled = productionModeEnabled;this.webProperties = webProperties;}public String getName() {return name;}public boolean isProductionModeEnabled() {return productionModeEnabled;}public WebProperties getWebProperties() {return webProperties;}@Overridepublic String toString() {return new ToStringBuilder(this).append("name", this.name).append("productionModeEnabled", this.productionModeEnabled).append("webProperties", this.webProperties).toString();} }第三 ,啟動應用程序時,我們必須將運行時配置寫入日志文件。 我們可以按照以下步驟進行操作:
在對ApplicationProperties類進行了這些更改之后,其源代碼如下所示:
import org.apache.commons.lang3.builder.ToStringBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;@Component public final class ApplicationProperties {private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationProperties.class);private final String name;private final boolean productionModeEnabled;private final WebProperties webProperties;@Autowiredpublic ApplicationProperties(@Value("${app.name}") String name,@Value("${app.production.mode.enabled:false}") boolean productionModeEnabled,WebProperties webProperties) {this.name = name;this.productionModeEnabled = productionModeEnabled;this.webProperties = webProperties;}public String getName() {return name;}public boolean isProductionModeEnabled() {return productionModeEnabled;}public WebProperties getWebProperties() {return webProperties;}@Overridepublic String toString() {return new ToStringBuilder(this).append("name", this.name).append("productionModeEnabled", this.productionModeEnabled).append("webProperties", this.webProperties).toString();}@PostConstructpublic void writeConfigurationToLog() {LOGGER.info("Starting application by using configuration: {}", this);} }啟動Web應用程序時,我們應該從其日志文件中找到以下信息:
INFO - ApplicationProperties - Starting application by using configuration: net.petrikainulainen.spring.trenches.config.ApplicationProperties@254449bb[name=Configuration Properties example,productionModeEnabled=false,webProperties=net.petrikainulainen.spring.trenches.config.WebProperties@4e642ee1[protocol=http,serverHost=localhost,serverPort=8080] ]該信息寫在一行中,但是我對它進行了格式化,因為我想使其更易于閱讀。
將敏感信息(例如數據庫用戶的用戶名或數據庫用戶的密碼)寫入日志文件不是一個好主意。
現在,我們可以從其日志文件中找到Web應用程序的運行時配置。 這是對當前情況的改進,但是只有當我們已經在讀取日志文件時,它才能使我們的生活更輕松。
讓我們找出如何通過實現將運行時配置返回為JSON的控制器方法來使生活更加輕松的方法。
將運行時配置作為JSON返回
通過執行以下步驟,我們可以實現一種控制器方法,該方法將運行時配置作為JSON返回:
PropertiesController類的源代碼如下所示:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;@RestController final class PropertiesController {private final ApplicationProperties applicationProperties;@AutowiredPropertiesController(ApplicationProperties applicationProperties) {this.applicationProperties = applicationProperties;}@RequestMapping(value = "/config", method = RequestMethod.GET)ApplicationProperties getAppConfiguration() {return applicationProperties;} }當我們將GET請求發送到url'/ config'時,我們的控制器方法將返回以下JSON:
{"name":"Configuration Properties example","productionModeEnabled":false,"webProperties":{"protocol":"http","serverHost":"localhost","serverPort":8080} }我們不應該允許所有人訪問我們應用程序的配置。 如果這將是一個真實的應用程序,我們應確保只有管理員才能訪問此信息。
讓我們繼續并總結從這篇博客文章中學到的知識。
摘要
這篇博客文章告訴我們:
- 我們可以通過重寫配置bean類的toString()方法并將這些bean的屬性值注入到日志文件中后,將運行時配置寫入日志文件。
- 通過創建返回“根”配置bean對象的控制器方法,我們可以將運行時配置作為JSON返回。
- PS:您可以從Github獲得此博客文章的示例應用程序 。
翻譯自: https://www.javacodegeeks.com/2015/04/spring-from-the-trenches-returning-runtime-configuration-as-json.html
ssm 返回json配置
總結
以上是生活随笔為你收集整理的ssm 返回json配置_摆脱困境:将运行时配置作为JSON返回的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么叫真命题 真命题是什么意思
- 下一篇: 鸡头米煮多长时间为最佳 鸡头米煮熟最好的