[Five: (mock data) springboot+mock integrated swaggerConfig]

Directory

  • 1. Demo of springboot
  • 2. Instance class
  • 3. Service class get request method
  • 4. Post request method of service class
  • 5. swaggerConfig interface document generation
  • Configuration dependencies

image.png

@SpringBootApplication
@ComponentScan("com.course")
public class Application {<!-- -->

    public static void main(String[] args) {<!-- -->
        SpringApplication.run(Application.class,args);
    }
}

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);
    }
}

2. Instance class

@Data
public class User {<!-- -->
    private String userName;
    private String password;
    private String name;
    private String age;
    private String sex;
}

3. Service class get request method

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 = "This is all my get methods")
public class MyGetMethod {<!-- -->

    @RequestMapping(value = "/getCookies", method = RequestMethod.GET)
    @ApiOperation(value = "Cookies can be obtained through this method", httpMethod = "GET")
    public String getCookies(HttpServletResponse response){<!-- -->
        //HttpServerletRequest class that holds request information
        //HttpServerletResponse class to install response information
        Cookie cookie = new Cookie("login","true");
        response.addCookie(cookie);
        return "Congratulations on successfully obtaining the cookie information";
    }

    /**
     * Require the client to carry cookies for access
     * This is a get request that requires cookie information to be accessed.
     */
    @RequestMapping(value = "/get/with/cookies", method = RequestMethod.GET)
    @ApiOperation(value = "Require the client to carry cookies to access", httpMethod = "GET")
    public String getWithCookies(HttpServletRequest request){<!-- -->
        Cookie[] cookies = request.getCookies();
        if(Objects.isNull(cookies)){<!-- -->
            return "You must bring cookie information";
        }
        for(Cookie cookie : cookies){<!-- -->
            if(cookie.getName().equals("login") & amp; & amp;
                    cookie.getValue().equals("true")){<!-- -->
                return "This is a get request that requires cookie information to be accessed!";
            }
        }

        return "You must bring cookie information";
    }

    /**
     * Develop a get request that requires parameters to be accessed.
     * The first implementation method url: key=value & amp;key=value
     * Let's simulate getting the product list
     */
    @RequestMapping(value = "/get/with/param", method = RequestMethod.GET)
    @ApiOperation(value = "get request method one that requires carrying parameters to access", httpMethod = "GET")
    public Map<String,Integer> getList(@RequestParam Integer start,
                                       @RequestParam Integer end){<!-- -->
        Map<String,Integer> myList = new HashMap<>();

        myList.put("shoes",400);
        myList.put("Dry noodles",1);
        myList.put("shirt",300);

        return myList;

    }

    /**
     * The second type of get request that requires parameter access
     * url:ip:port/get/with/param/10/20
     */
    @RequestMapping(value = "/get/with/param/{start}/{end}")
    @ApiOperation(value = "Second implementation of get request that requires parameters to be accessed", httpMethod = "GET")
    public Map myGetList(@PathVariable Integer start,
                          @PathVariable Integer end){<!-- -->

        Map<String,Integer> myList = new HashMap<>();

        myList.put("shoes",400);
        myList.put("Dry noodles",1);
        myList.put("shirt",300);

        return myList;

    }

}

4. Post request method of service class

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 = "This is all my post requests")
@RequestMapping("/v1")
public class MyPostMethod {<!-- -->

    //This variable is used to store our cookie information
    private static Cookie cookie;

    //The user successfully logs in to obtain cookies, and then accesses other interfaces to obtain the list.

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation(value = "Login interface, obtain cookie information after success", 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") & amp; & amp; password.equals("123456")){<!-- -->
            cookie = new Cookie("login","true");
            response.addCookie(cookie);
            return "Congratulations on your successful login!";
        }
        return "The username or password is wrong!";
    }
    @RequestMapping(value = "/getUserList", method = RequestMethod.POST)
    @ApiOperation(value = "Get user list",httpMethod = "POST")
    public String getUserList(HttpServletRequest request,
                            @RequestBody User u){<!-- -->

        User user;
        //Get cookies
        Cookie[] cookies = request.getCookies();
        //Verify whether cookies are legal
        for(Cookie c : cookies){<!-- -->
            if(c.getName().equals("login")
                     & amp; & amp; c.getValue().equals("true")
                     & amp; & amp; u.getUserName().equals("zhangsan")
                     & amp; & amp; u.getPassword().equals("123456")
                    ){<!-- -->
                user = new User();
                user.setName("lisi");
                user.setAge("18");
                user.setSex("man");
                return user.toString();
            }

        }

        return "Illegal parameter";
    }
}

5. swaggerConfig interface document generation

@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("My Interface Document")
                .contact(new Contact("dazhou","","[email protected]"))
                .description("This is the interface document generated by my swaggerui")
                .version("1.0.0.0")
                .build();

    }
}

Access address: http://localhost:8888/swagger-ui.html
image.png
image.png
image.png

Configuration dependencies

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.14</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.38</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>

    </dependencies>