6

I'm not able to display my spring doc in swagger-ui. This is my configuration :

springdoc:
  # api-docs:
    # enabled: true
    # path: /v3/api-docs/swagger-config
  swagger-ui:
    # path: /swagger-ui.html
    disable-swagger-default-url: true
    # config-url: /v3/api-docs/swagger-config
    # url: /v3/api-docs

I enter this URL to access to the interface : http://localhost:8080/swagger-ui.html

swagger ui empty

The problem is that it is not displaying the api-docs which is loaded from :

"http://localhost:8080/v3/api-docs/swagger-config"

Status : 200.

But if i enter this in the url field, it works.. my goal is not to enter this each time..

swagger ui working

I tested everything possible.. i don't understand why it does not work

EDIT : swagger-config

{"configUrl":"/v3/api-docs/swagger-config","oauth2RedirectUrl":"http://localhost:8080/swagger-ui/oauth2-redirect.html","url":"/v3/api-docs","validatorUrl":""}{"configUrl":"/v3/api-docs/swagger-config","oauth2RedirectUrl":"http://localhost:8080/swagger-ui/oauth2-redirect.html","url":"/v3/api-docs","validatorUrl":""}

network tab

Kévin
  • 497
  • 10
  • 37
  • Can you please share the full URL you are accessing, the http console log in network tab and the dependency you added for swagger. Also this might help you https://stackoverflow.com/questions/71857622/java-spring-boot-with-swagger-failed-to-load-remote-configuration/71859359#comment126984457_71859359 – GJohannes Apr 14 '22 at 10:47
  • swagger-config : http://localhost:8080/v3/api-docs/swagger-config docs config : http://localhost:8080/v3/api-docs – Kévin Apr 14 '22 at 11:35
  • A picture about the http repsonse in your network tab of the browser would also be helpful. seeing what is actally beeing requested – GJohannes Apr 14 '22 at 19:48
  • @GJohannes I have added the picture – Kévin Apr 15 '22 at 14:35
  • Hi @Kévin, were you able to get the solution to this or we need to resort to the only way of passing the path in the search bar. Seems like there is no solution to this? Adding springdoc.swagger-ui.url and springdoc.api-docs.path does not work in my case as well. – Biswarup Nath Mar 02 '23 at 11:43

4 Answers4

6

I had a similar issue and was able to reproduce it by updating the configuration in the springdoc demo project 'springdoc-openapi-spring-boot-2-webmvc'. I had the following properties defined:

springdoc.api-docs.path=/test/v3/api-docs
sprindoc.swagger-ui.config-url=/test/v3/api-docs/swagger-config
springdoc.swagger-ui.path=/test/swagger-ui.html

With that configuration I able to reproduce the problem the OP reported. What fixed it for me was to add an additional property:

springdoc.swagger-ui.url=/test/v3/api-docs

Hope this helps.

Jacob Ven
  • 77
  • 1
  • No it does not work, you can see that what you say is what is commented in my OP. Same behaviour, i need to tape /v3/api-docs in the search bar and after that it works.. :( – Kévin Apr 22 '22 at 06:50
0

Had a similar issue while migrating froms springfox to springdoc, the difference was that when testing locally everything was fine, but remotely it was the same problem.

You pointing at /v3/api-docs and /v3/api-docs/swagger-config helped.
I looked at the response for swagger-config and saw it was the main page of my app instead of a JSON object with the configurations in it.
Then I looked and saw there was a security rule with exceptions for specific urls, including swagger ones. There were springfox ones I didn't need anymore, but no swagger-config. Updating that resolved the problem.

Hope this helps someone.

Stav Noy
  • 419
  • 4
  • 14
0

I worked by that:

springdoc:
  swagger-ui:
    # swagger-ui地址
    path: /springdoc/swagger-ui.html
    enabled: true
    # 配置本地访问页面(注释)
    #config-url: /springdoc/api-docs/swagger-config
    # 取消默认Swagger访问页面
    disable-swagger-default-url: true
    # 修复Failed to load remote configuration.
    url: /springdoc/api-docs
  api-docs:
    path: /springdoc/api-docs

Otherwise, I do other thing at ResponseBodyAdvice

@RestControllerAdvice
@Slf4j
public class UnitedResponseAdvice implements ResponseBodyAdvice<Object> {

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        if (body != null && !(body instanceof ResponseVo) && !(body instanceof byte[])) {
            // 放行Swagger相关
            if (body instanceof TreeMap && ((TreeMap)body).containsKey("oauth2RedirectUrl")) {
                return body;
            }
            return ResponseVo.message(ResponseCode.SUCCESS.val(), ResponseCode.SUCCESS.des(), body);
        }
        return body;
    }
}
-2

Just add the springdoc.swagger-ui.path = /myPath to the application.properties and then navigate to localhost:8080/mypath and it should auto redirect you.

  • no way of addressing the pointed issue. The issue is not about the failed redirection but rather not auto loading the API config to display API definitions properly. – malik_cesur Jun 07 '23 at 07:29