OpenAPI与Spring框架6

huangapple go评论64阅读模式
英文:

OpenAPI with Spring Framework 6

问题

我有一个项目,使用的是Spring Framework 6(不是Spring Boot)。我只是使用了@RestController,所以我可以返回任何类型的对象。现在我想要为这些REST API编写文档。为此,我尝试使用OpenAPI。

我添加了依赖implementation 'org.springdoc:springdoc-openapi-ui:1.7.0'以及其他相关的依赖项,这些都是最新的版本。应用程序构建和成功运行。但问题是,当我尝试访问Swagger的UI界面时,使用http://localhost:8080/rest/swagger-ui.html,我收到了以下响应:

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

因为Spring Framework 6使用Jakarta命名空间,而org.springdoc:springdoc-openapi-ui:1.7.0使用javax命名空间。我不能同时在我的项目中使用这两个依赖项。问题是,由于客户的要求,我不能降级我的应用程序。
请告诉我是否有任何解决方案。

提前感谢!

英文:

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!!

答案1

得分: 0

在我的情况下,它看起来像这样

@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")));
    }
}

这只是配置。您需要在您的端点上添加一些注解。查看开放式API文档以获取详细信息。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月1日 20:23:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定