Skip to content

Commit 90d06c0

Browse files
committed
Customizing swagger static resources
1 parent c82b67d commit 90d06c0

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
122 KB
Loading

src/docs/asciidoc/faq.adoc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,3 +936,64 @@ More precisely, this the exhaustive list of spring-boot versions against which `
936936
|`2.0.x`, `1.5.x` | `1.0.0`+
937937

938938
|===
939+
940+
=== Customizing swagger static resources
941+
942+
You can customize swagger documentation static resources located in `META-INF/resources/webjars/swagger-ui/{swagger.version}/`. The list of resources includes:
943+
944+
- `index.html`
945+
- `swagger-ui-bundle.js`
946+
- `swagger-ui.css`
947+
- `swagger-ui-standalone-preset.js`
948+
- `swagger-ui.css.map`
949+
- `swagger-ui-bundle.js.map`
950+
- `swagger-ui-standalone-preset.js.map`
951+
- `favicon-32x32.png`
952+
953+
To do this, you need to extend the implementation of `SwaggerIndexPageTransformer`
954+
955+
[source,java]
956+
----
957+
public class SwaggerCodeBlockTransformer
958+
extends SwaggerIndexPageTransformer {
959+
// < constructor >
960+
@Override
961+
public Resource transform(HttpServletRequest request,
962+
Resource resource,
963+
ResourceTransformerChain transformer)
964+
throws IOException {
965+
if (resource.toString().contains("swagger-ui.css")) {
966+
final InputStream is = resource.getInputStream();
967+
final InputStreamReader isr = new InputStreamReader(is);
968+
try (BufferedReader br = new BufferedReader(isr)) {
969+
final String css = br.lines().collect(Collectors.joining());
970+
final byte[] transformedContent = css.replace("old", "new").getBytes();
971+
return new TransformedResource(resource, transformedContent);
972+
} // AutoCloseable br > isr > is
973+
}
974+
return super.transform(request, resource, transformer);
975+
}
976+
977+
}
978+
----
979+
980+
Next, add transformer `@Bean` to your `@Configuration`
981+
982+
[source,java]
983+
----
984+
@Configuration
985+
public class OpenApiConfig {
986+
@Bean
987+
public SwaggerIndexTransformer swaggerIndexTransformer(
988+
SwaggerUiConfigProperties a,
989+
SwaggerUiOAuthProperties b,
990+
SwaggerUiConfigParameters c,
991+
SwaggerWelcomeCommon d) {
992+
return new SwaggerCodeBlockTransformer(a, b, c, d);
993+
}
994+
}
995+
----
996+
997+
Illustrative example
998+
999+
image::images/static_content_transformation.png[Illustrative example]

0 commit comments

Comments
 (0)