• 【五:(mock数据)springboot+mock集成swaggerConfig】


    image.png

    @SpringBootApplication
    @ComponentScan("com.course")
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class,args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    1、springboot 的Demo

    @Controller
    @EnableAutoConfiguration
    public class SampleController {
    
        @RequestMapping("/")
        @ResponseBody
        String home() {
            return "Hello World!";
        }
        public static void main(String[] args) throws Exception {
            SpringApplication.run(SampleController.class, args);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2、实例类

    @Data
    public class User {
        private String userName;
        private String password;
        private String name;
        private String age;
        private String sex;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3、服务类 get请求方法

    
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.web.bind.annotation.*;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Objects;
    
    @RestController
    @Api(value = "/",tags = "这是我全部的get方法")
    public class MyGetMethod {
    
        @RequestMapping(value = "/getCookies",method = RequestMethod.GET)
        @ApiOperation(value = "通过这个方法可以获取到Cookies",httpMethod = "GET")
        public String getCookies(HttpServletResponse response){
            //HttpServerletRequest 装请求信息的类
            //HttpServerletResponse  装响应信息的类
            Cookie cookie = new Cookie("login","true");
            response.addCookie(cookie);
            return "恭喜你获得cookies信息成功";
        }
    
        /**
         * 要求客户端携带cookies访问
         * 这是一个需要携带cookies信息才能访问的get请求
         */
        @RequestMapping(value = "/get/with/cookies",method = RequestMethod.GET)
        @ApiOperation(value = "要求客户端携带cookies访问",httpMethod = "GET")
        public String getWithCookies(HttpServletRequest request){
            Cookie[] cookies = request.getCookies();
            if(Objects.isNull(cookies)){
                return "你必须携带cookies信息来";
            }
            for(Cookie cookie : cookies){
                if(cookie.getName().equals("login") &&
                        cookie.getValue().equals("true")){
                    return "这是一个需要携带cookies信息才能访问的get请求!";
                }
            }
    
            return "你必须携带cookies信息来";
        }
    
        /**
         * 开发一个需要携带参数才能访问的get请求。
         * 第一种实现方式 url: key=value&key=value
         * 我们来模拟获取商品列表
         */
        @RequestMapping(value = "/get/with/param",method = RequestMethod.GET)
        @ApiOperation(value = "需求携带参数才能访问的get请求方法一",httpMethod = "GET")
        public Map<String,Integer> getList(@RequestParam Integer start,
                                           @RequestParam Integer end){
            Map<String,Integer> myList = new HashMap<>();
    
            myList.put("鞋",400);
            myList.put("干脆面",1);
            myList.put("衬衫",300);
    
            return  myList;
    
        }
    
        /**
         * 第二种需要携带参数访问的get请求
         * url:ip:port/get/with/param/10/20
         */
        @RequestMapping(value = "/get/with/param/{start}/{end}")
        @ApiOperation(value = "需求携带参数才能访问的get请求的第二种实现",httpMethod = "GET")
        public  Map myGetList(@PathVariable Integer start,
                              @PathVariable Integer end){
    
            Map<String,Integer> myList = new HashMap<>();
    
            myList.put("鞋",400);
            myList.put("干脆面",1);
            myList.put("衬衫",300);
    
            return  myList;
    
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85

    4、服务类的post请求方法

    package com.course.server;
    
    
    import com.course.bean.User;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.web.bind.annotation.*;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @RestController
    @Api(value = "/",tags = "这是我全部的post请求")
    @RequestMapping("/v1")
    public class MyPostMethod {
    
        //这个变量是用来装我们cookies信息的
        private static Cookie cookie;
    
        //用户登陆成功获取到cookies,然后再访问其他接口获取到列表
    
        @RequestMapping(value = "/login",method = RequestMethod.POST)
        @ApiOperation(value = "登陆接口,成功后获取cookies信息",httpMethod = "POST")
        public String login(HttpServletResponse response,
                            @RequestParam(value = "userName",required = true) String userName,
                            @RequestParam(value = "password",required = true)  String password){
            if(userName.equals("zhangsan") && password.equals("123456")){
                cookie = new Cookie("login","true");
                response.addCookie(cookie);
                return "恭喜你登陆成功了!";
            }
            return "用户名或者是密码错误!";
        }
        @RequestMapping(value = "/getUserList",method = RequestMethod.POST)
        @ApiOperation(value = "获取用户列表",httpMethod = "POST")
        public String getUserList(HttpServletRequest request,
                                @RequestBody User u){
    
            User user;
            //获取cookies
            Cookie[] cookies = request.getCookies();
            //验证cookies是否合法
            for(Cookie c : cookies){
                if(c.getName().equals("login")
                        && c.getValue().equals("true")
                        && u.getUserName().equals("zhangsan")
                        && u.getPassword().equals("123456")
                        ){
                    user = new User();
                    user.setName("lisi");
                    user.setAge("18");
                    user.setSex("man");
                    return  user.toString();
                }
    
            }
    
            return "参数不合法";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    5、swaggerConfig 接口文档生成

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
        @Bean
        public Docket api(){
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .pathMapping("/")
                    .select()
                    .paths(PathSelectors.regex("/.*"))
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return  new ApiInfoBuilder().title("我的接口文档")
                    .contact(new Contact("dazhou","","42197393@qq.com"))
                    .description("这是我的swaggerui生成的接口文档")
                    .version("1.0.0.0")
                    .build();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    访问地址:http://localhost:8888/swagger-ui.html
    image.png
    image.png
    image.png

    配置依赖

    <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            <dependency>
                <groupId>io.springfoxgroupId>
                <artifactId>springfox-swagger-uiartifactId>
                <version>2.6.1version>
            dependency>
    
            <dependency>
                <groupId>io.springfoxgroupId>
                <artifactId>springfox-swagger2artifactId>
                <version>2.6.1version>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>1.16.14version>
            dependency>
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>fastjsonartifactId>
                <version>1.2.38version>
            dependency>
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
            dependency>
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>1.3.0version>
            dependency>
    
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    Springmvc的视图(转发视图,重定向视图,thymeleafview)
    小程序订单中心path设置本次审核不通过,审核原因:小程序尚未发布,无法审核。
    功率谱分析笔记-------脑电相关
    【面试宝典】39道C++内存管理高频题库整理(附答案背诵版)
    解决开源项目资金问题的 8 种方法
    Linux常用系统性能监控命令
    基于JAVA音乐播放器计算机毕业设计源码+系统+数据库+lw文档+部署
    13-Linux之rpm软件管理
    vue+elementUI 设置el-descriptions固定长度并对齐
    解决安装sentry执行install.sh卡住的问题
  • 原文地址:https://blog.csdn.net/Leoon123/article/details/133936809