SpringBoot集成SwaggerUI

1.引入jar包

    <properties>
        <swagger.version>2.6.1</swagger.version>
    </properties>
 
    <dependencies>    
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>
 
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>
    </dependencies>

2.新建配置文件

在main/java目录下,新建com.course.config目录以存放配置文件,然后新建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("Lyn","","326030434@qq.com"))
                .description("这是SwaggerUi生成的接口文档")
                .build();
    }
}

重启springboot,访问http://localhost:8080/swagger-ui.html,没有拿到所写的接口信息,原因是接口中没有配置swagger配置

3.给接口加上注解

首先要在整个类前面加

@Api(value = "/",description = "这是所有get方法")

然后在每个方法上加

@ApiOperation(value = "通过这个方法可以获取到cookies",httpMethod = "GET")
@RestController
@Api(value = "/", description = "这是我全部的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信息才能访问的get请求
     */
    @RequestMapping(value = "get/with/cookies", method = RequestMethod.GET)
    @ApiOperation(value = "这是一个需要携带cookies信息才能访问的get请求", httpMethod = "GET")
    public String getWithCookies(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return "no cookies with get method";
        }
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("login") && cookie.getValue().equals("true")) {
                return "这是一个需要携带cookies信息才能访问的get请求";
            }
        }
        return "cookies info wrong";
    } 
    /**
     * 开发一个需要携带参数才能访问的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> myLsit = new HashMap<String, Integer>();
        myLsit.put("鞋子", 400);
        myLsit.put("方便面", 5);
        myLsit.put("口红", 300);
 
        return myLsit;
    }
    /**
     * 开发一个需要携带参数才能访问的get请求
     * 第二种实现方式:url:url:port/get/with/param/10/20
     * 我们来模拟获取商品列表
     */
    @RequestMapping(value = "/get/with/param/{start}/{end}", method = RequestMethod.GET)
    @ApiOperation(value = "需要携带参数才能访问的get请求方法二", httpMethod = "GET")
    public Map<String, Integer> getMyList(@PathVariable Integer start,
                                          @PathVariable Integer end) {
        Map<String, Integer> myLsit = new HashMap<String, Integer>();
        myLsit.put("鞋子", 400);
        myLsit.put("方便面", 5);
        myLsit.put("口红", 300);
 
        return myLsit;
    } 
    /**
     * 开发一个需要携带参数和cookies才能访问的get请求
     * 我们来模拟用户使用用户名和密码登录
     */
    @RequestMapping(value = "/get/with/paramAndCookie", method = RequestMethod.GET)
    @ApiOperation(value = "需要携带参数和cookies才能访问的get请求", httpMethod = "GET")
    public String login(HttpServletRequest request,
                        @RequestParam String name,
                        @RequestParam String password) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return "请配置cookie信息";
        }
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("login") & cookie.getValue().equals("true")) {
                if (name.equals("zhangsan") && password.equals("123456")) {
                    return "zhangsan 登录成功";
                } else if (name.equals("zhangsan") && !password.equals("123456")) {
                    return "密码错误请重试";
                } else {
                    return "请注册再登录";
                }
            }
        }
        return "请注册再登录";
    } 
}

4.修改Application文件

之前是@ComponentScan(“com.course.server”)并没有包含到com.course.config文件,所以,这里修改为@ComponentScan(“com.course”)

@SpringBootApplication
@ComponentScan("com.course")
public class Application {
    public static void main(String[] args){
        SpringApplication.run(Application.class,args);
    }
}

5.查看接口文档

运行 Application文件,然后浏览器访问http://localhost:9000/swagger-ui.html
在这里插入图片描述

6.测试接口

可以在该网站中对接口做简单的测试工作:找到某个具体的测试接口输入入参后,点击“Cry it out”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值