Spring boot with swagger UI gives empty page

时间:2016-10-20 13:11:04

标签: spring-boot swagger swagger-ui

I am trying to use Swagger UI for API documentation which is developed using Spring Boot framework.

1) Dependency in pom

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${springfox-version}</version>
    </dependency>
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-core</artifactId>
        <version>${swagger-core-version}</version>
    </dependency>




<properties>

    <springfox-version>2.5.0</springfox-version>
    <swagger-core-version>1.5.10</swagger-core-version>

</properties>

2) Docket configuration

@ComponentScan(basePackages = {"com.testApp.*"})
@Configuration
@EnableSwagger2
public class Application {

 @Bean
    public Docket api() { 

      return  new Docket(DocumentationType.SWAGGER_2)  
                .select().apis(
                      RequestHandlerSelectors.any())
                      .paths(PathSelectors.any())                             
                      .build(); 

    }


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

3) Resource configuration in com.testApp package

 @Path("/resources")
 @Api(value = "Test resource", produces = "application/json")
 public class MyResource  {

 @Autowired
 public SomeClass someclass;

/**
 * 
 * @param uriInfo
 * @return
 * @throws PlatformException
 */
@ApiOperation(value = "Gets a hello resource. World Version 1 (version in Accept Header)", response = String.class)
@ApiResponses(value = {
    @ApiResponse(code = 200, message = "Hello resource found"),
    @ApiResponse(code = 404, message = "Hello resource not found")
})

@GET
@Produces({ MediaType.APPLICATION_JSON })
public String loadResouces(@Context UriInfo uriInfo)  {
    //method defination
}

This service runs on port 9001. whenever hit localhost:8080/swagger-ui.html. It returns an empty page of swagger-ui. I've tried few properties of Docket like host, pathmapping etc. But I am not able to generate documentation using this.

2 个答案:

答案 0 :(得分:0)

To the best of my knowledge springfox-swagger2 supports APIs implemented using Spring MVC only.

If you prefer to implement the endpoints using JAX-RS but still use Swagger to document them, please take a look at this answer.

A "how-to" could be found at a blog I created sometime ago, Microservices using Spring Boot, Jersey Swagger and Docker

答案 1 :(得分:0)

我通过实现TypeResolver和RequestMappingHandlerMapping bean解决了类似的问题。 请尝试将以下bean添加到您的Application类。

    @Bean
    public TypeResolver typeResolver(){
        return new TypeResolver();
    }

    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping(){
        return new RequestMappingHandlerMapping();
    }