【问题标题】:Spring Boot and SwaggerSpring Boot 和 Swagger
【发布时间】:2017-06-29 10:32:52
【问题描述】:

我有一个 Spring Boot 1.5.1RELEASE 应用程序,我正在尝试使用 Swagger。我添加了依赖项:

compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.5.0'
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.5.0'compile('org.springframework.boot:spring-boot-devtools')
compile("org.springframework.boot:spring-boot-starter-web:1.5.1RELEASE)

然后是 /config 类:

@Configuration
@EnableSwagger2
public class SwaggerConfig {    
    @Bean
    public Docket api() {    
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                        .paths(PathSelectors.any()).build();           
    }    

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                        .title("My API Documentation ")
                        .description("Swagger API Documentation provided for your viewing pleasure")
                        .version("1.0")
                        .build();
    }
}

我有一个 GET 端点

@Api(value = "/words", description = "Words API", produces = "application/json")
@RestController
@RequestMapping("/words")
public class WordsController {

@ApiOperation(value = "getAllWords", nickname = "getAllWords",
              response = ResponseEntity.class, httpMethod = "GET")
@ApiResponses(value = {
                @ApiResponse(code = 200, message = "Success", response = ResponseEntity.class),
                @ApiResponse(code = 500, message = "Failure")
                })
@RequestMapping( method = RequestMethod.GET)
@ResponseBody
    public ResponseEntity getAllWords() {
...}

但是当我在http://localhost:3000/swagger-ui.html 访问我的 api 时(我的服务器配置为在端口 3000 上运行,我只看到绿色的招摇栏

我根据几个教程构建了这个,但我不明白为什么我的 Swagger 文档没有被展示

【问题讨论】:

    标签: spring-mvc swagger-ui swagger-2.0


    【解决方案1】:

    在你的配置类中似乎有问题,试试下面的招摇配置类

    import static com.google.common.base.Predicates.or;
    import static springfox.documentation.builders.PathSelectors.regex;
    import com.google.common.base.Predicate;
    
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    /**
     * Sample Swgger config.
     *
     * @author Eranga
     */
    @Configuration
    @EnableSwagger2
    public class MySwaggerConfig  {
    
        /**
         * Grouping only words api end points.
         *
         * @return the docket
         */
        @Bean
        public Docket myApi() {
            return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().paths(myApiPath()).build();
        }
    
        /**
         * Filtering only media activity api end points.
         *
         * @return the predicate
         */
        private Predicate<String> myApiPath() {
            return or(regex("//words.*"));
        }
    
        /**
         * Api info.
         *
         * @return the api info
         */
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder().title("My API title")
                    .description(
                            "my Api desc")
                    .termsOfServiceUrl("http://erangakodikara.blogspot.com/").contact("Eranga")
                    .license("Apache License Version 2.0")
                    .licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE").version("2.0").build();
        }
    }
    

    【讨论】:

    • 我想我已经通过将我的 SwaggerConfig 移动到我的主类来修复它,所以由于某种原因这个@Configuration 没有被拾取
    猜你喜欢
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 2017-02-19
    • 2014-05-20
    • 2023-01-04
    • 2016-02-13
    • 2021-12-31
    • 1970-01-01
    相关资源
    最近更新 更多