Home > Enterprise >  Spring boot 3 Swagger not working with updated version
Spring boot 3 Swagger not working with updated version

Time:02-04

Trying to generate swagger UI but not able to generate using spring boot 3.0.2 and java 17.0.2. Below is my details

Gradle dependency

implementation "io.springfox:springfox-boot-starter:3.0.0"

Swagger Configuration

@SpringBootApplication
@ComponentScan({"com.bl.*"})
@EnableJpaRepositories(basePackages = { "com.bl.entity.repository" })
@EntityScan({"com.bl.entity"})
public class BlApiUiApplication {

    public static void main(String[] args) {
        SpringApplication.run(BlApiUiApplication.class, args);
    }

    @Bean
    Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("UI Details")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API")
                .description("UI")
                .licenseUrl("URL").version("1.0").build();
    }
}   

Controller

@RestController
@RequestMapping("/v0")
@Api(value = "API")
public class UIController {

    private final Logger logger = LoggerFactory.getLogger(UIController.class);

    @ApiOperation(value = "isRunning", notes = "To check whether service is running or not")
    @GetMapping(value = "/isRunning", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> test() {
        return new ResponseEntity<>("Service is running.", HttpStatus.OK);
    }

    @ApiOperation(value = "/user/login", notes = "To login user")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "Successful"),
            @ApiResponse(code = 500, message = "Internal server error"),
            @ApiResponse(code = 1001, message = "Application specific error.") })
    @PostMapping(value = "/user/login", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<BaseGatewayResponse> login(@RequestBody final UserLoginRequest requestDTO) {
        logger.info("Login request: {}", requestDTO);
        UserLoginResponse responseDTO = userGatewayService.login(requestDTO);
        logger.info("Exit Login response: {} for request: {}", responseDTO, requestDTO);
        return new ResponseEntity<>(responseDTO, HttpStatus.OK);
    }

After running its not working getting below error.

Swagger URL : http://localhost:8080/BLApiUI/swagger-ui/index.html

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Feb 03 22:59:52 IST 2023
There was an unexpected error (type=Not Found, status=404).

CodePudding user response:

Recently we have upgraded to Java 17, We have added 'org.springdoc:springdoc-openapi-ui:1.6.12' dependency as no other old swagger related dependency were working and Swagger Configuration class looks simple for us now

'''@Configuration
public class SwaggerConfiguration{
   @Bean
   public GroupedOpenApi publicApi(){
    GroupedOpenApi.builder().group("springshop-publish").pathsToMatch("/**").build();
   }
}'''

CodePudding user response:

in case you want to try "openapi", simply add annotation "OpenAPIDefinition" to Application class, no other code change is needed.

implementation "org.springdoc:springdoc-openapi-ui:1.6.12"

springboot 2.7.0
java 17

@OpenAPIDefinition
public class MyApplication

Swagger URL : http://localhost:8080/swagger-ui/index.html#/
  • Related