Search code examples
springswaggeropenapispringdoc-openapi-ui

OpenAPI with Spring Framework 6


I have a project which uses spring framework 6 (not spring boot). I just used @RestController, so I can return any type of Object. Now I want to document these rest apis. And for this, I tried to use OpenAPI.

I added implementation 'org.springdoc:springdoc-openapi-ui:1.7.0 and other related dependencies also which is latest one. Application builds and runs successfully. But the problem is when I try to access ui of swagger using http://localhost:8080/rest/swagger-ui.html I get a response like

No primary or single unique constructor found for interface javax.servlet.http.HttpServletRequest

Because Spring framework 6 uses Jakarta namespace and org.springdoc:springdoc-openapi-ui:1.7.0 uses javax namespace. And I cannot have both dependencies in my project. The thing is I cannot downgrade my application because of client's requirement.
Please tell if there is any solution.

Thanks in advance!!


Solution

  • In my case, It looks something like this

        @Configuration
        @EnableWebMvc
        @ComponentScan(basePackages = {"org.springdoc"})
        @Import({org.springdoc.core.configuration.SpringDocConfiguration.class, org.springdoc.webmvc.core.configuration.SpringDocWebMvcConfiguration.class,
                 org.springdoc.webmvc.ui.SwaggerConfig.class,
                 org.springdoc.core.properties.SwaggerUiConfigProperties.class,
                 org.springdoc.core.properties.SwaggerUiOAuthProperties.class,
                 org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration.class})
        public class WebMvcConfig implements WebSocketConfigurer, WebMvcConfigurer {
            @Bean
            public GroupedOpenApi dafault(){
                 return GroupedOpenApi.builder()
                    .group("all")
                    .packagesToScan(YOUR_PACKAGE)
                    .pathsToMatch(URL_PATH_MATCHER).build();
            @Bean
            public OpenAPI ApiInfo() {
                 return new OpenAPI()
                    .info(new Info().title(TITLE)
                            .description(DESCRIPTION)
                            .version(ANY_VERSION_NUMBER)
                            .contact(new Contact().name(CONTACT_NAME).email(CONTACT_EMAIL).url(CONTACT_URL))
                            .license(new License().name("Apache 2.0").url("http://springdoc.org")));
    

    This is just configuration. You need to add some annotation on your endpoints. Check open api documentation for detailed information.