首页 文章

Java Spring应用程序可以利用swagger

提问于
浏览
0

我've been looking into swagger as a possible way to automatically generate documentation for my team' s api http://swagger.io/getting-started/ . 这似乎很有希望,但我发现他们的文档缺乏 .

话虽如此,我有一些非常基本的问题 .

  • 是否可以在Spring应用程序中使用swagger?我们的应用既不是运动衫也不是JAX-RS应用 . 有谁知道一个普通的Spring应用程序是否可以与swagger一起使用?如果是这样,可以提供一个或一组指令?

我找到了这个链接http://blog.zenika.com/index.php?post/2013/07/11/Documenting-a-REST-API-with-Swagger-and-Spring-MVC然而他们略过了关于设置属性文件的内容 .

  • swagger在他们的网站上引用了许多不同的工具swagger core,codegen,ui和swagger编辑器 . 是否只需要swagger-core来生成基本API文档,或者需要组合使用swagger工具?

3 回答

  • 1

    有一个专门用于support swagger frameworks的页面 .

    你在找什么是SpringFox .

    使用Spring构建的API的自动JSON API文档

    基本上,您使用像这样指向您的API上下文的Docket对象来应用配置 . 您需要花时间,因为有一些配置可以让它工作,而不是提到它根据您的应用程序需求进行扩展 .

    @Bean
        public Docket documentation() {
            return new Docket()
              .select()
                .apis(RequestHandlerSelectors.any())
                .paths(regex("/api/.*"))
                .build()
              .pathMapping("/")
              .apiInfo(metadata());
        }
    

    您可能已经知道,您可以在许多其他功能中添加注释,例如可能的HTTP响应代码等 . 确实很有希望 .

    @ApiOperation(value = "doStuff", nickname = "doStuff", response = DoStuffResult.class)
    @Responses({
        @ApiResponse(code =  404, message ="Not found", response = GenericError.class),
        @ApiResponse(code =  400, message ="Invalid input", response = GenericError.class)
    })
    @RequestMapping(method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<?> doStuff(@RequestBody DoStuffCommand command) {
        // Stuff
    }
    

    这是best example我可以在网上找到,非常简短和客观 .

  • 0

    对我来说工作:

    在pom中添加了以下依赖项:

    <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
        </dependency>
    

    然后将@ EnableSwagger2添加到spring配置文件和swagger UI所需的注册资源处理程序,如:

    @Configuration
    public class YourConfigFileHere extends WebMvcConfigurerAdapter {
    
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/**").addResourceLocations(
                "/resources/");
    
            registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
    
            registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        }
    
        // rest of the configuration
    }
    

    这将使基本的swagger UI启动并运行 . 如果要自定义,可以通过添加使用@ EnableSwagger2注释的swagger配置文件并使用@Import将其导入spring配置文件来实现 .

    对于本地环境,您可以访问swagger-ui:

    http://localhost:8080/{context-root}/swagger-ui.html
    
  • 1

    如上所述, springfox 是spring-mvc的一流招摇图书馆 . 您还可以使用swagger-samples存储库中演示的spring配置 .

    您可以通过访问http://editor.swagger.io并创建一个swagger定义来查看此示例 . 从那里,您可以下载一个支持swagger的spring-mvc服务器 .

相关问题