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?

11
  • Do you depends too on the springfox-swagger-ui dependency? Commented Apr 5, 2017 at 15:30
  • @NicolasLabrot sure i added all necessary dependencies Commented Apr 5, 2017 at 15:31
  • Do your logs contain Mapped "{[/swagger-resources/configuration/ui]}" ? Commented Apr 5, 2017 at 15:34
  • @NicolasLabrot but i need to generate only json Commented Apr 5, 2017 at 15:36
  • >Do your logs contain Mapped "{[/swagger-resources/configuration/ui]}" ? – Nicolas Labrot 1 min ago don't contain Commented Apr 5, 2017 at 15:37

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the advice I runned with your config, but in logs i can't find Mapper /v2/api-docs :(
Could you set a breakpoint on return new Docket(DocumentationType.SWAGGER_2) line and run your application in debug ? Does your debugger stop?
Have you added @Import(SwaggerConfig.class) to your spring boot bootstrap class ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.