springspring-mvcopenapispringdoc-openapi-ui

Set up OpenAPI (Swagger) UI 3 in Spring Framework 6


Tried a few suggestions in existing stackoverflow discussions but I can't even get the html page to serve, and almost all information on the web uses Spring Boot instead, does anyone have an article, video or a github repo where I can see how the correct configuration is set up? Using embedded jetty to serve if it matters


Solution

  • It's possible to take advantage of springdoc-openapi-starter-webmvc-ui package and however you're bootstrapping the spring app, to just register its usual autoconfig classes manually. Add these to app context: SpringDocConfiguration, SwaggerConfig, SpringDocWebMvcConfiguration, JacksonAutoConfiguration and these beans somewhere in your configuration:

    @Bean
    SpringDocConfigProperties springDocConfigProperties() {
        return new SpringDocConfigProperties();
    }
    
    @Bean
    SwaggerUiConfigProperties swaggerUiConfigProperties() {
        return new SwaggerUiConfigProperties();
    }
    
    @Bean
    SwaggerUiOAuthProperties swaggerUiOAuthProperties() {
        return new SwaggerUiOAuthProperties();
    }
    

    if overriding configureMessageConverters,

    converters.add(new ByteArrayHttpMessageConverter());
    

    is needed