Spring-Boot-项目中使用Swagger2

作者: 固安李庆海 | 来源:发表于2018-09-28 09:17 被阅读12次

    添加Swagger2依赖

    在pom.xml中加入Swagger2的依赖

    <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>
    

    创建Swagger2配置类

    在Application.java同级创建Swagger2的配置类Swagger2。

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @Configuration
    @EnableSwagger2
    public class Swagger2 {
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("你自己的外部接口包名称"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("词网Neo4j RESTful APIs")
                    .description("The Neo4j RESTful APIs description/")
                    .termsOfServiceUrl("")
                    .contact("李庆海")
                    .version("5.0")
                    .build();
        }
    }
    

    添加文档内容

    在完成了上述配置后,其实已经可以生产文档内容,但是这样的文档主要针对请求本身,而描述主要来源于函数等命名产生,对用户并不友好,我们通常需要自己增加一些说明来丰富文档内容。

    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import io.swagger.annotations.ApiParam;
    /**
     * 系统用户Controller
     * 
     * @author 李庆海
     *
     */
    @Api(value = "系统用户接口", tags = "系统管理")
    @RestController
    @RequestMapping("/v3/edu/users")
    public class UserController {
    
        @Autowired
        private UserService userService;
    
        /**
         * 添加用户,注册
         * 
         * @param loginName
         *            登录账号
         * @param userName
         *            用户名称
         * @param password
         *            登录密码
         * @param roleId
         *            用户角色
         * @return
         * @throws ResourceExistsException
         */
        @ApiOperation(value = "添加用户")
        @PostMapping("/")
        public JsonResult create(
                @ApiParam(name = "loginName", value = "登录账号", required = true) @RequestParam(required = true) @RequestBody String loginName,
                @ApiParam(name = "userName", value = "用户名称", required = true) @RequestParam(required = true) @RequestBody String userName,
                @ApiParam(name = "password", value = "登录密码", required = true) @RequestParam(required = true) @RequestBody String password,
                @ApiParam(name = "roleId", value = "用户角色编号", required = true) @RequestParam(required = true) @RequestBody String roleId)
                throws ResourceExistsException {
            boolean exists = this.userService.exists(loginName);
            if (exists) {
                throw new ResourceExistsException(loginName);
            }
            User user = userService.create(loginName, password, userName, roleId);
            return new JsonResult(user);
        }
    }
    

    查看API

    启动Spring Boot程序,访问:http://localhost:8080/swagger-ui.html

    微信截图_20180106154957.png

    API文档访问与调试

    Swagger除了查看接口功能外,还提供了调试测试功能,我们可以点击上图中右侧的Model Schema(黄色区域:它指明了数据结构),此时Value中就有了user对象的模板,我们只需要稍适修改,点击下方Try it out!按钮,即可完成了一次请求调用!可以通过几个GET请求来验证之前的POST请求是否正确。

    相关文章

      网友评论

        本文标题:Spring-Boot-项目中使用Swagger2

        本文链接:https://www.haomeiwen.com/subject/xwsuoftx.html