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

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

創(chuàng)新互聯(lián)基于成都重慶香港及美國(guó)等地區(qū)分布式IDC機(jī)房數(shù)據(jù)中心構(gòu)建的電信大帶寬,聯(lián)通大帶寬,移動(dòng)大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業(yè)遂寧聯(lián)通機(jī)房報(bào)價(jià),主機(jī)托管價(jià)格性價(jià)比高,為金融證券行業(yè)服務(wù)器托管,ai人工智能服務(wù)器托管提供bgp線路100M獨(dú)享,G口帶寬及機(jī)柜租用的專業(yè)成都idc公司。

官方一篇文章很詳細(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.

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

使用

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

配置元數(shù)據(jù)文件位于jar下面。 META-INF/spring-configuration-metadata.json它們使用簡(jiǎn)單的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)行拓展。

下面簡(jiǎ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ái)系統(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>)。您可以使用此屬性來(lái)指導(dǎo)用戶可以輸入的值的類型。為了保持一致性,通過(guò)使用其包裝對(duì)應(yīng)項(xiàng)(例如,boolean變?yōu)閖ava.lang.Boolean)來(lái)指定基元的類型。請(qǐng)注意,此類可能是一個(gè)復(fù)雜類型,它從Stringas綁定的值轉(zhuǎn)換而來(lái)。如果類型未知,則可以省略。
descriptionString可以向用戶顯示的組的簡(jiǎn)短描述。如果沒(méi)有可用的描述,則可以省略。建議描述為簡(jiǎn)短段落,第一行提供簡(jiǎn)明摘要。描述中的最后一行應(yīng)以句點(diǎn)(.)結(jié)尾。
sourceTypeString貢獻(xiàn)此屬性的源的類名稱。例如,如果屬性來(lái)自帶注釋的類@ConfigurationProperties,則此屬性將包含該類的完全限定名稱。如果源類型未知,則可以省略。
defaultValueObject默認(rèn)值,如果未指定屬性,則使用該值。如果屬性的類型是數(shù)組,則它可以是值數(shù)組。如果默認(rèn)值未知,則可以省略。

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

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

對(duì)應(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ái)系統(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)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文題目:springboot之如何使用additional-spring-configuration-metadata.json自定義提示
文章轉(zhuǎn)載:http://muchs.cn/article40/ihgseo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、電子商務(wù)、網(wǎng)站收錄、企業(yè)網(wǎng)站制作、標(biāo)簽優(yōu)化企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站