0

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

Akash
  • 11
  • 3
  • the actual problem is no problem: just use https://springdoc.org/v2/ (for jakarta servlet api) ... but "not spring boot" is not true, though – xerx593 Jun 01 '23 at 12:59
  • @xerx593 My project is not a spring boot project so I can't use what you suggested. – Akash Jun 01 '23 at 13:05
  • No, no problem, (though it is now called "starter") the dependencies don't differ much (despite the version) from v1! so , when `org.springdoc:springdoc-openapi-ui:1.7.0` was "ok" up to some point, `springdoc-openapi-starter-webmvc-ui:2.1.0` won't break it – xerx593 Jun 01 '23 at 13:26
  • 1
    @xerx593 Hey! Thanks for help. Now it's working. Can you help me with one another thing. I changed the default swagger ui path to /rest/swagger-ui.html and it redirects me to /swagger-ui/index.html. But I want it to redirect to /rest/swagger-ui/index.html. Basically I want that whenever it redirects to any url, it should add /rest prefix to that url. – Akash Jun 02 '23 at 05:52

1 Answers1

0

@Stalowy, 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.

Akash
  • 11
  • 3