Skip to content
Advertisement

Getting error while configuring Swagger-ui with spring reactive

While integrating swagger-ui with a reactive spring project generated using JHipster 7.1.0 for Java 11. I added the below dependencies.

The application has below dependencies of swagger

POM dependencies

<dependency>
  <groupId>org.springdoc</groupId>
  <artifactId>springdoc-openapi-common</artifactId>
  <version>1.4.3</version>
</dependency>
<dependency>
  <groupId>org.springdoc</groupId>
  <artifactId>springdoc-openapi-webflux-core</artifactId>
  <version>1.4.3</version>
</dependency>
<dependency>
  <groupId>org.springdoc</groupId>
  <artifactId>springdoc-openapi-webflux-ui</artifactId>
  <version>1.4.3</version>
</dependency>

Apart from that we added a configuration SwaggerConfig.java

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import org.springdoc.core.GroupedOpenApi;
import org.springdoc.core.customizers.OpenApiCustomiser;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@OpenAPIDefinition(info = @Info(title = "Amazingbabbage", version = "1.0", description = "Documentation APIs v1.0"))
public class SwaggerConfig {
    @Bean
    public GroupedOpenApi employeeGroupApi() {
        return GroupedOpenApi.builder()
                .group("all")
                .pathsToMatch("/api/**")
                .build();
    }

    public OpenApiCustomiser getOpenApiCustomiser() {

        return openAPI -> openAPI.getPaths().values().stream().flatMap(pathItem ->
                        pathItem.readOperations().stream())
                .forEach(operation -> {
                    operation.addParametersItem(new Parameter().name("Authorization").in("header").
                            schema(new StringSchema().example("token")).required(true));
                    operation.addParametersItem(new Parameter().name("userId").in("header").
                            schema(new StringSchema().example("test")).required(true));

                });
    }
}

{
  "type" : "https://www.jhipster.tech/problem/problem-with-message",
  "title" : "Internal Server Error",
  "status" : 500,
  "detail" : "No primary or single public constructor found for interface org.springframework.http.server.reactive.ServerHttpRequest - and no default constructor found either",
  "path" : "/swagger-doc/swagger-ui.html",
  "message" : "error.http.500"
}

Please share your feedback or inputs if you have faced similar issues.

Advertisement

Answer

When you have both Spring MVC and Webflux are present, Spring Boot will configure your application to use Spring MVC only. (see : https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.spring-application.web-environment)

You need to ensure that the Spring MVC dependency is not present in your project.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement