我确信必须有一种优雅的方式将Spring Boot configuration meta-data files转换为HTML(或降价或......)。虽然我可以自己编写解析器,但我确信这已经解决了。然而,到目前为止,我没有找到领先优势。
答案 0 :(得分:2)
Here是使用org.springframework.boot:spring-boot-configuration-processor
和org.freemarker:freemarker
库将Spring Boot配置元数据转换为HTML的简单示例。
使用spring-boot-configuration-processor
库中的类来解析JSON文件
File metadataFile = new File("spring-configuration-metadata.json");
ConfigurationMetadata metadata =
new JsonMarshaller().read(new FileInputStream(metadataFile));
将元数据转换为Java对象后,使用FreeMarker
模板化将其转换为HTML文件。它使用metadata.ftl
模板将metadata
对象转换为metadata.html
。
Configuration cfg = new Configuration(new Version(2, 3, 27));
// template is loaded from 'templates' folder under resources
cfg.setClassForTemplateLoading(
SpringConfigurationMetadataProcessorTest.class, "/templates");
cfg.setDefaultEncoding("UTF-8");
cfg.setLocale(Locale.US);
cfg.setTemplateExceptionHandler(
TemplateExceptionHandler.RETHROW_HANDLER);
Map<String, Object> input = new HashMap<String, Object>();
input.put("title", "Spring Configuration Metadata Example");
input.put("metadata", metadata);
input.put("GROUP", ItemMetadata.ItemType.GROUP);
input.put("PROPERTY", ItemMetadata.ItemType.PROPERTY);
Template template = cfg.getTemplate("metadata.ftl");
// write output into a file
Writer fileWriter = new FileWriter(new File("metadata.html"));
try {
template.process(input, fileWriter);
} finally {
fileWriter.close();
}
以下是示例metadata.ftl
,
<html>
<head>
<title>${title}</title>
</head>
<body>
<h1>${title}</h1>
<#if metadata.hints??>
<section>
<h1>Hints</h1>
<#list metadata.hints as hint>
<section>
<dl>
<dt><b>name: </b>${hint.name}</dt>
<#if hint.values?? && hint.values?size != 0>
<dt><b>values:</b></dt>
<#list hint.values as value>
<#if value.value??>
<dd><b>value: </b>${value.value}</dd>
</#if>
<#if value.description??>
<dd><b>description: </b>${value.description}</dd>
</#if>
<br/>
</#list>
</#if>
<#if hint.providers?? && hint.providers?size != 0>
<dt><b>providers:</b></dt>
<#list hint.providers as provider>
<#if provider.name??>
<dd><b>name: </b>${provider.name}</dd>
</#if>
<#if provider.parameters?? && provider.parameters?size != 0>
<dd><b>parameters:</b></dd>
<#list provider.parameters as key, value>
<dd> <b>${key}: </b>${value}
</dd>
</#list>
</#if>
<br/>
</#list>
</dl>
</#if>
</section>
</#list>
</section>
</#if>
<#if metadata.items??>
<section>
<h1>Groups</h1>
<#list metadata.items as item>
<#if item.isOfItemType(GROUP)>
<section>
<dl>
<#if item.sourceType??>
<dt><b>sourceType: </b> ${item.sourceType}</dt>
</#if>
<#if item.name??>
<dt><b>name: </b> ${item.name}</dt>
</#if>
<#if item.description??>
<dt><b>description: </b> ${item.description}</dt>
</#if>
<#if item.type??>
<dt><b>type: </b> ${item.type}</dt>
</#if>
<#if item.sourceMethod??>
<dt><b>sourceMethod: </b> ${item.sourceMethod}</dt>
</#if>
<#if item.defaultValue??>
<#if item.defaultValue?is_enumerable>
<dt><b>defaultValue: </b></dt>
<#list item.defaultValue as df>
<dd>${df}</dd>
</#list>
<#else>
<dt><b>defaultValue: </b> ${item.defaultValue}</dt>
</#if>
</#if>
<#if item.deprecation??>
<dt><b>deprecation: </b></dt>
<#if item.deprecation.reason??>
<dd><b>reason: </b> ${item.deprecation.reason}</dd>
</#if>
<#if item.deprecation.replacement??>
<dd><b>replacement: </b> ${item.deprecation.replacement}</dd>
</#if>
<#if item.deprecation.level??>
<dd><b>level: </b> ${item.deprecation.level}</dd>
</#if>
</#if>
</dl>
</section>
</#if>
</#list>
<h1>Properties</h1>
<#list metadata.items as item>
<#if item.isOfItemType(PROPERTY)>
<section>
<dl>
<#if item.sourceType??>
<dt><b>sourceType: </b> ${item.sourceType}</dt>
</#if>
<#if item.name??>
<dt><b>name: </b> ${item.name}</dt>
</#if>
<#if item.description??>
<dt><b>description: </b> ${item.description}</dt>
</#if>
<#if item.type??>
<dt><b>type: </b> ${item.type}</dt>
</#if>
<#if item.sourceMethod??>
<dt><b>sourceMethod: </b> ${item.sourceMethod}</dt>
</#if>
<#if item.defaultValue??>
<#if item.defaultValue?is_enumerable>
<dt><b>defaultValue: </b></dt>
<#list item.defaultValue as df>
<dd>${df}</dd>
</#list>
<#else>
<dt><b>defaultValue: </b> ${item.defaultValue}</dt>
</#if>
</#if>
<#if item.deprecation??>
<dt><b>deprecation: </b></dt>
<#if item.deprecation.reason??>
<dd><b>reason: </b> ${item.deprecation.reason}</dd>
</#if>
<#if item.deprecation.replacement??>
<dd><b>replacement: </b> ${item.deprecation.replacement}</dd>
</#if>
<#if item.deprecation.level??>
<dd><b>level: </b> ${item.deprecation.level}</dd>
</#if>
</#if>
</dl>
</section>
</#if>
</#list>
</section>
</#if>
</body>
</html>