SpringBoot集成SwaggerUI及其使用

撸了今年阿里、头条和美团的面试,我有一个重要发现.......>>> hot3.png

本文永久更新地址:https://my.oschina.net/bysu/blog/3092623

推荐一款免费的自购省钱,分享赚钱的平台——赚赚熊,感兴趣的,特别是家里有婆娘专职带娃的,请看文末。

1.在pom.xml文件中加入
 

<properties>
        <swagger.version>2.6.1</swagger.version>
    </properties>


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

2.创建一个cmd包,在cmd中创建Application类,如下:

package cmd;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

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

3.创建video.aigo.config包,在config中创建SwaggerConfig类,如下:

package video.aigo.config;

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.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@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("bysu","","by.su@qq.com"))
                .description("这是我的swaggerui生成的接口文档").version("1.0.0")
                .build();

    }
}

4.运行Application后,在浏览器地址栏输入http://localhost:8080/swagger-ui.html  如下图为集成成功

e7e4d8713e7f94644dcec4f4ed7b7db5c58.jpg
如何使用swaggerui?1.创建包video.aigo.server,在server中创建MyGetMethod类,代码如下:

package video.aigo.server;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@RestController
@Api(value = "/",description = "这是我全部的get方法")
public class MyGetMethod {
    @RequestMapping(value = "/getCookies",method = RequestMethod.GET)
    @ApiOperation(value = "通过这个get方法可以获取到cookies的值",httpMethod = "GET")
    public String getCookies(HttpServletResponse response){
        Cookie cookie = new Cookie("login","true");
        response.addCookie(cookie);
        return "恭喜获取cookies成功";
    }
}

2.把上面集成的步骤2中@ComponentScan("video.aigo.server")改成@ComponentScan("video.aigo");
3.运行Application后,在浏览器地址栏输入http://localhost:8080/swagger-ui.html 
e3485b4ed25f97b01ec8596aaee27e01548.jpg

4.在包video.aigo.server中新增MyPostMethod类,代码如下:

package video.aigo.server;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@RestController
@Api(value = "/",description = "这是我全部的post方法")
@RequestMapping(value = "v1")
public class MyPostMethod {

    private static Cookie cookie;
    @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 = "passwd",required = true) String passwd){
        if (userName.equals("bysu") && passwd.equals("666666")){
            cookie = new Cookie("login","true");
            response.addCookie(cookie);
            return "恭喜你登录成功";
        }
        return "用户名或密码错误";
    }
}

5.重启Application后,刷新页面,结果如下图,发现红框中与上图不一样。在userName和passwd中输入用户名和密码,点击try it out,会对账户名密码进行校验。

d2a4fafc5ee98b5f34d04f57df2317bb6e2.jpg

 

最后:整个project的目录如下

5fa374c04224317d1ead507070c1cb1e2d1.jpg

----------------------下方为免费的广告---------------------

1.识别下图中的二维码,跳到步骤2的界面;

145f7da2d987f8138c50cf5fee472cdadd7.jpg

2.填入手机,获取验证码,填入验证码注册,界面跳转至步骤3:

da90d28fc83439d9444f6a444614ba75aa2.jpg

3.点开右上角的“...”,界面跳转至步骤4:

31f38c15547175553247b4aed48f5c70f1b.jpg

4.选择在系统浏览器打开后,界面跳转至步骤5:

52b733f5d83ad4bf9c6c096762f19ffe8dc.jpg

 

5.根据你手机的系统,选择下载的版本。

70b64b0b5f3688b0e2e3b00f843c7f05a81.jpg

6.安装成功后,登录app,选择授权淘宝。授权后,在淘宝购买东西大多数都有返利,后续还会支持京东,唯品会...各大平台。

赚赚熊有问题可以咨询

6eeb2b61e839cb466502c970612c72dd120.jpg

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值