无法通过弹簧启动获得Swagger UI

时间:2017-05-19 07:10:53

标签: spring-boot swagger swagger-ui

这是我的配置类

@SpringBootApplication
@EnableJpaRepositories
@EnableTransactionManagement
@EnableSwagger2
public class SwaggerExampleApplication {

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

 @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select().apis(RequestHandlerSelectors.basePackage("com.example.controller")).paths(PathSelectors.any()).build().pathMapping("/");
    }

 private ApiInfo apiInfo() {
      return new ApiInfoBuilder()
      .title("Simple Hello App")
      .description("A simple calculator REST service made with Spring Boot in Java")
      .contact("lavya")
      .version("1.0")
      .build();
    }

}

这是我的资源类

@Component
@Path("/hello")
@Api(value="Test Hello",description="testing swagger")
public class ResourceController {

@GET
@Produces({MediaType.APPLICATION_JSON})
@ApiOperation(value="list employess",notes="list employees")
public List<Employee> displayDetails(){
    System.out.println("inside display");
    return Arrays.asList(new Employee(1,"ab","cdd","sass"));
    }

iam无法回复swagger ui,http://localhost:8080/swagger-ui.html运行应用程序,有人可以说明原因吗?

2 个答案:

答案 0 :(得分:1)

这对我有用:

@Configuration
@EnableSwagger2
@Profile({"!production"})
public class SwaggerConfiguration extends WebMvcConfigurerAdapter {

    @Autowired
    private ServletContext servletContext;

    @Bean
    public Docket api() {

        return new Docket(DocumentationType.SWAGGER_2)
                .pathProvider(new RelativePathProvider(servletContext) {
                    @Override
                    public String getApplicationBasePath() {
                        return "/";
                    }
                })
                .protocols(new HashSet<String>(Arrays.asList(protocols)))
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

答案 1 :(得分:-1)

试试这个。它对我有用。

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        // Make Swagger meta-data available via <baseURL>/v2/api-docs/
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        // Make Swagger UI available via <baseURL>/swagger-ui.html
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}