• spring boot整合swagger ui (RESTFUL接口的文档在线自动生成+功能测试功能软件,前后端分离快速开发)


      swagger ui可以通过来拦截controller层,生成请求API,并将其展示在浏览器当中。我们可以直接通过浏览器来查看和调试接口。

     1 添加maven依赖

            <!-- Swagger -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.6.0</version>
            </dependency>
    
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.6.0</version>
            </dependency>

    2 增加swagger配置类

    package com.springboot.config;
    
    import static springfox.documentation.builders.PathSelectors.regex;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    /**
     * SwaggerConfig
     */
    @Configuration
    @EnableSwagger2
    public class Swagger2Configuration {
    
        /**
         * 
         * @return
         */
        @Bean
        public Docket accessHello() {
            return new Docket(DocumentationType.SWAGGER_2).groupName("hello")// 定义组
                    .select() // 选择那些路径和api会生成document
                    .apis(RequestHandlerSelectors.basePackage("com.springboot.controller")) // 拦截的包路径
                    .paths(regex("/hello/.*"))// 拦截的接口路径
                    .build(); // 创建
                    //.apiInfo(apiInfo()); // 配置说明
        }
        
        /**
         * 
         * @return
         */
        @Bean
        public Docket accessUser() {
            return new Docket(DocumentationType.SWAGGER_2).groupName("user")// 定义组
                    .select() // 选择那些路径和api会生成document
                    .apis(RequestHandlerSelectors.basePackage("com.springboot.controller")) // 拦截的包路径
                    .paths(regex("/user/.*"))// 拦截的接口路径
                    .build(); // 创建
                    //.apiInfo(apiInfo()); // 配置说明
        }
    
    }

    3 增加测试controller层

    package com.springboot.controller;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.springboot.model.User;
    import com.springboot.util.ApiJSONUtil;
    import com.springboot.util.JsonReturn;
    
    import io.swagger.annotations.ApiOperation;
    
    @RestController
    @RequestMapping("/user")
    public class UserController {
        
        Map<String,User> maps = new HashMap<String,User>();
        
        @ApiOperation(value="添加",notes="用户添加操作")
        @PostMapping("/add")
        public String add(@RequestBody User user) throws Exception{
            maps.put(user.getUuid(), user);
            JsonReturn jsonReturn = new JsonReturn(true);
            return ApiJSONUtil.objectToJsonStr(jsonReturn);
        }
        
        
        @ApiOperation(value="删除",notes="用户添加操作")
        @PostMapping("/delete/{uuid}")
        public String add(@PathVariable String uuid)  throws Exception{
            maps.remove(uuid);
            JsonReturn jsonReturn = new JsonReturn(true);
            return ApiJSONUtil.objectToJsonStr(jsonReturn);
        }
        
        @ApiOperation(value="修改",notes="用户添加操作")
        @PostMapping("/update")
        public String delete(@RequestBody  User user)  throws Exception{
            maps.put(user.getUuid(), user);
            JsonReturn jsonReturn = new JsonReturn(true);
            jsonReturn.setObj(user);
            return ApiJSONUtil.objectToJsonStr(jsonReturn);
        }
        
        @ApiOperation(value="查询",notes="用户添加操作")
        @PostMapping("/query")
        public String query(@RequestBody  Map<String,String> map)  throws Exception{
            User user = maps.get(map.get("uuid"));
            JsonReturn jsonReturn = new JsonReturn(true);
            jsonReturn.setObj(user);
            return ApiJSONUtil.objectToJsonStr(jsonReturn);
        }
    }

    4 输入 http://localhost:8080/swagger-ui.html#/查看效果

     

      

  • 相关阅读:
    使用shell生成excel
    linux逻辑卷扩容
    nginx异常访问处理
    云主机上的k8s集群通信故障
    【转】 Pro Android学习笔记(七九):服务(4):远程服务的实现
    老李分享:系统可用性评估
    Develoment 和 Production 模式的区分打包
    TS基础学习笔记
    flanneld 安装
    Execl文件批量转csv文件
  • 原文地址:https://www.cnblogs.com/zhangjunqing/p/7712668.html
Copyright © 2020-2023  润新知