0

I tried generate some api via swagger without spring boot but it doesn't work

my swagger controller class

@Configuration
@EnableSwagger2
@Controller
@RequestMapping("/srs/api")
public class SwaggerConfig extends WebMvcConfigurerAdapter {
    @Bean
    @RequestMapping(value = "/v2/api-docs", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Docket swaggerconf() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo("2.0"))
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo(String version) {
        return new ApiInfoBuilder()
                .title("API")
                .description("REST API")
                .version(version)
                .build();
    }
}

url is mapped

Mapped "{[/srs/api/v2/api-docs],methods=[GET],produces=[application/json]}" onto public springfox.documentation.spring.web.plugins.Docket com.my.applications.srs.rest.controllers.SwaggerConfig.swaggerconf()

But the documentation is not created did i miss something? may be I can use SpringBoot on server?

Amit
  • 30,756
  • 6
  • 57
  • 88
Ivan T
  • 3
  • 1
  • 6

1 Answers1

1

Could you try to update your swagger configuration to:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket swaggerconf() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo("2.0"))
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo(String version) {
        return new ApiInfoBuilder()
                .title("API")
                .description("REST API")
                .version(version)
                .build();
    }
}

Then import (if not already done) @Import this configuration into your Application like class.

When you bootstrap your application you should have a log like

 Mapped "{[/v2/api-docs],methods=[GET],produces=[application/json || application/hal+json]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)

And if you open the url /v2/api-docs you should get a json

Nicolas Labrot
  • 4,017
  • 25
  • 40