英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论