I am using springfox's swagger implementation. I would like to modify the swagger-ui.html to take a custom header value. How do I modify this file? Or tell spring fox to use an alternate file?
Asked
Active
Viewed 2.7k times
9
-
I was able to acheive this by following the instructions here: https://github.com/springfox/springfox/issues/1176 Basically, You can pull down the source and make whatever modifications you need to make. You don't need to create a web jar. just copy it into your resources and have your app handle it using resource handlers – mad_fox Mar 24 '16 at 15:04
3 Answers
4
Pull Swagger UI from github.
Copy & paste dist directory of swagger ui to resources directory
Replace the code with below in swagger config
replace
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
with
registry.addResourceHandler("**").addResourceLocations("classpath:/dist/");
Change the api docs path in index.html to your path
Replace
http://petstore.swagger.io/v2/swagger.json
With
http://localhost:8080/your_app/v2/api-docs
Use the below url pattern to see the ui page

pvrforpranavvr
- 2,708
- 2
- 24
- 34
-
thanks, that works, but deep linking doesnt work (I get error when sending link to specific opened endpoint in swagger). Any idea how to fix it? thanks! – Martin Čejka Mar 13 '19 at 09:41
-
UPDATE as of now: In step no. 3, the Line "registry.addResourceHandler("**").addResourceLocations("classpath:/dist/");" does not work, as now SpringBoot has default mappings of /static, /resources. /META-INF/resources. Thus, the /dist mapping does not work. Also, the "**" flag is also invalid, you have to provide valid html page name. Thanks – Utkarsh Mankad Dec 16 '20 at 06:19
3
If you're going to customize swagger-ui quite a bit, I would just recommend adding a private copy of swagger-ui to your web application resources and modify the ui based on your needs.
Swagger-ui bundled with springfox is optional.

Dilip Krishnan
- 5,417
- 3
- 37
- 53
-
how to understand 'quite a bit' ? what about changing the logo, require password to login to view and see different content based on different login? – actan Mar 14 '17 at 10:42
-
2
-
@actan. Were you able to change the swagger logo? I'm desperately looking for some clue to change the swagger logo in spring boot. – Avinash Mar 26 '21 at 05:24
-
@Avinash yes I copied the swagger static resource into project and customized whatever I need, logo was updated – actan Mar 30 '21 at 10:16
-
1
Yes, pull the swagger ui into your project and follow the steps here: https://github.com/springfox/springfox/issues/1176
Then edit the index.html and change where it points to the petstore to your endpoint. ( i.e. "http://localhost:8080/MyProjectName/v2/api-docs");
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "http://petstore.swagger.io/v2/swagger.json";

Oberst
- 31
- 4