Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
872 views
in Technique[技术] by (71.8m points)

java - How to Integrate Open API 3 with Spring project (not Spring Boot) using springdoc-openapi

My Existing Project is on Spring Framework not Spring Boot.

I want to integrate Open API 3 with it.

I want to integrate using springdoc-openapi not using Jersey.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The ui load for spring-mvc (5.3.1) using springdoc-openapi-ui 1.5.2 looks more simple:

build.gradle (gradle version 6.5)

implementation 'org.springdoc:springdoc-openapi-ui:1.5.2'

The spring-boot-autoconfigure and spring-boot are not needed explicitly in dependencies section cause org.springdoc:springdoc-openapi-ui:1.5.2 already has them both (version 2.4.0). You'll be surprised how many and what dependencies will be added to your final application.

OpenApiConfig.java

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"org.springdoc"})
@Import({org.springdoc.core.SpringDocConfiguration.class,
         org.springdoc.webmvc.core.SpringDocWebMvcConfiguration.class,
         org.springdoc.webmvc.ui.SwaggerConfig.class,
         org.springdoc.core.SwaggerUiConfigProperties.class,
         org.springdoc.core.SwaggerUiOAuthProperties.class,
         org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration.class})

class OpenApiConfig implements WebMvcConfigurer {
}

The OpenApiConfig package should be covered by component scan.

In case of Spring Security usage, you might add two url patterns and define the role in your code for the OpenAPI pages access.

<security:intercept-url pattern="/swagger*/**" access="ROLE_DEVELOPER"/>
<security:intercept-url pattern="/v3/api-docs" access="ROLE_DEVELOPER"/>

The urls to check:

http://localhost:8080/your_context_path/swagger-ui.html
http://localhost:8080/your_context_path/v3/api-docs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...