溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

springboot之如何使用additional-spring-configuration-metadata.json自定義提示

發(fā)布時(shí)間:2021-09-05 10:29:37 來源:億速云 閱讀:347 作者:小新 欄目:大數(shù)據(jù)

這篇文章主要介紹springboot之如何使用additional-spring-configuration-metadata.json自定義提示,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

官方一篇文章很詳細(xì)講解了Configuration Metadata的作用。 有興趣的小伙伴可以查看下(配置元數(shù)據(jù))。

Configuration Metadata

Appendix B. Configuration Metadata
Spring Boot jars include metadata files that provide details of all supported configuration properties. The files are designed to let IDE developers offer contextual help and “code completion” as users are working with application.properties or application.yml files.

The majority of the metadata file is generated automatically at compile time by processing all items annotated with @ConfigurationProperties. However, it is possible to write part of the metadata manually for corner cases or more advanced use cases.

簡介說明配置additional-spring-configuration-metadata.json文件后,在開發(fā)人員的IDE工具使用個(gè)人編寫的配置讀取很有效的在application.propertiesapplication.yml文件下完成提示。

使用

1. 元數(shù)據(jù)格式

配置元數(shù)據(jù)文件位于jar下面。 META-INF/spring-configuration-metadata.json它們使用簡單的JSON格式,其中的項(xiàng)目分類在“groups”或“properties”下,其他值提示分類在“hints”下,如下例所示:

{"groups": [
	{
		"name": "server",
		"type": "org.springframework.boot.autoconfigure.web.ServerProperties",
		"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
	}
	...
],"properties": [
	{
		"name": "server.port",
		"type": "java.lang.Integer",
		"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
	}
	...
],"hints": [
	{
		"name": "spring.jpa.hibernate.ddl-auto",
		"values": [
			{
				"value": "none",
				"description": "Disable DDL handling."
			},
			{
				"value": "validate",
				"description": "Validate the schema, make no changes to the database."
			}
		]
	}
]}

2.properties提示編寫

當(dāng)然了我們需要編寫META-INF/additional-spring-configuration-metadata.json進(jìn)行拓展。

下面簡單創(chuàng)建一個(gè)starter使用additional-spring-configuration-metadata.json進(jìn)行提示。

resources/META-INF目錄下創(chuàng)建additional-spring-configuration-metadata.json

springboot之如何使用additional-spring-configuration-metadata.json自定義提示

內(nèi)容大致如下:

{"properties": [
    {
      "name": "swagger.basePackage",
      "type": "java.lang.String",
      "description": "文檔掃描包路徑。"
    },
    {
      "name": "swagger.title",
      "type": "java.lang.String",
      "defaultValue": "平臺系統(tǒng)接口詳情",
      "description": "title 如: 用戶模塊系統(tǒng)接口詳情。"
    },
    {
      "name": "swagger.description",
      "type": "java.lang.String",
      "defaultValue": "在線文檔",
      "description": "服務(wù)文件介紹。"
    },
    {
      "name": "swagger.termsOfServiceUrl",
      "type": "java.lang.String",
      "defaultValue": "https://www.test.com/",
      "description": "服務(wù)條款網(wǎng)址。"
    },
    {
      "name": "swagger.version",
      "type": "java.lang.String",
      "defaultValue": "V1.0",
      "description": "版本。"
    }
]}

大家參考下面properties表格進(jìn)行配置上的理解。

名稱類型目的
nameString屬性的全名。名稱采用小寫的周期分隔形式(例如server.address)。此屬性是強(qiáng)制性的。
typeString屬性的數(shù)據(jù)類型的完整簽名(例如java.lang.String),但也是完整的泛型類型(例如java.util.Map<java.util.String,acme.MyEnum>)。您可以使用此屬性來指導(dǎo)用戶可以輸入的值的類型。為了保持一致性,通過使用其包裝對應(yīng)項(xiàng)(例如,boolean變?yōu)閖ava.lang.Boolean)來指定基元的類型。請注意,此類可能是一個(gè)復(fù)雜類型,它從Stringas綁定的值轉(zhuǎn)換而來。如果類型未知,則可以省略。
descriptionString可以向用戶顯示的組的簡短描述。如果沒有可用的描述,則可以省略。建議描述為簡短段落,第一行提供簡明摘要。描述中的最后一行應(yīng)以句點(diǎn)(.)結(jié)尾。
sourceTypeString貢獻(xiàn)此屬性的源的類名稱。例如,如果屬性來自帶注釋的類@ConfigurationProperties,則此屬性將包含該類的完全限定名稱。如果源類型未知,則可以省略。
defaultValueObject默認(rèn)值,如果未指定屬性,則使用該值。如果屬性的類型是數(shù)組,則它可以是值數(shù)組。如果默認(rèn)值未知,則可以省略。

deprecation每個(gè)properties元素的屬性中包含的JSON對象可以包含以下屬性:

名稱類型目的
levelString棄用級別,可以是warning(默認(rèn))或error。當(dāng)屬性具有warning棄用級別時(shí),它仍應(yīng)綁定在環(huán)境中。但是,當(dāng)它具有error棄用級別時(shí),該屬性不再受管理且不受約束。
reasonString該屬性被棄用的原因的簡短描述。如果沒有可用的原因,可以省略。建議描述為簡短段落,第一行提供簡明摘要。描述中的最后一行應(yīng)以句點(diǎn)(.)結(jié)尾。
replacementString替換此不推薦使用的屬性的屬性的全名。如果此屬性沒有替換,則可以省略。

對應(yīng)的@ConfigurationProperties類如下:

@Data
@ConfigurationProperties(SwaggerProperties.PREFIX)
public class SwaggerProperties {

  public static final String PREFIX = "swagger";

  /**
   * 文檔掃描包路徑
   */
  private String basePackage = "";

  /**
   * title 如: 用戶模塊系統(tǒng)接口詳情
   */
  private String title = "平臺系統(tǒng)接口詳情";

  /**
   * 服務(wù)文件介紹
   */
  private String description = "在線文檔";

  /**
   * 服務(wù)條款網(wǎng)址
   */
  private String termsOfServiceUrl = "https://www.test.com/";

  /**
   * 版本
   */
  private String version = "V1.0";

}

現(xiàn)在就可以在application.properties文件里嘗試提示。 springboot之如何使用additional-spring-configuration-metadata.json自定義提示

以上是“springboot之如何使用additional-spring-configuration-metadata.json自定義提示”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI