springdoc / springdoc-openapi-demos

Demo for OpenAPI 3 with spring-boot
http://springdoc.org
Apache License 2.0
493 stars 267 forks source link

Provide sample configurations for spring project (not spring boot) #67

Open kajaldhatrak opened 1 month ago

kajaldhatrak commented 1 month ago

Swagger ui not coming up in spring application. As per documentation, tried adding spring-boot,spring-boot-autoconfigure dependencies. also configure those classes which are not auto configure in spring project. Tried various combinations still swagger-ui is not coming up.

Sample project would be helpful as there is very less documentation for pure spring project.

micronaut commented 1 week ago

Are there any updates on this? I am having the same problem with a non-spring boot spring project. Thanks

kajaldhatrak commented 1 week ago

for following spring dependency it was throwing some parameter error which was caused by spring 6 deprecating the class which springdoc uses.

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-webmvc-core</artifactId>
    <version>1.8.0</version>
</dependency>

so here's what worked for me -

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.5.0</version>
</dependency>

also it needs following dependency since project is not spring boot project:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>3.3.1</version>
</dependency>

for configuration you need to add following configuration class:

@Configuration
@ComponentScan(
        basePackages = {"org.springdoc"}
)
@EnableWebMvc
@Import({SpringDocConfiguration.class,
        SpringDocWebMvcConfiguration.class,
        org.springdoc.webmvc.ui.SwaggerConfig.class,
        SwaggerUiConfigProperties.class,
        SwaggerUiOAuthProperties.class,
        JacksonAutoConfiguration.class})
public class OpenApiConfiguration {

    @Bean
    public OpenAPI openAPI() {
        return new OpenAPI();
    }

}

Also all methods need @ResponseBody as annotation. Methods without @ ResponseBody were not showing in swagger ui.