开发工具:STS

前言:

  对外提供一个Api,无论是对开发、测试、维护,都有很大的帮助。

  下面我们来实现swagger2。

参考实例:https://blog.csdn.net/weixin_39477597/article/details/79639239

实例:

1.添加依赖:

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

2.配置:

package com.xm.shiro.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.builders.RequestHandlerSelectors;
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 docket(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
// 当前包路径
.apis(RequestHandlerSelectors.basePackage("com.xm.shiro.controller"))
.paths(PathSelectors.any()).build(); }
//构建api文档的详细信息函数
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
//页面标题
.title("springBoot测试使用Swagger2构建RESTful API")
//创建人
.contact("郭小明")
//版本号
.version("1.0")
//描述
.description("API 描述")
.build();
} }

3.在controller中引用api:

注解说明:

@Api():作用于类上,表示这个类是swagger的资源。
    tags = ”说明该类的作用“
@ApiOperation():用在请求的方法上,说明的方法的用户和作用
    value=“说明方法的用途、作用”

notes="方法的备注说明“

@ApiImplicitParams():用在请求的方法上,表示一组参数说明,可以包含多个@ApiImplicitParam()
@ApiImplicitParam():指定一个请求参数的各个方面
       name:参数名
       value:参数的汉字说明
       required:参数是否必须传
       dataType:参数类型

defaultValue:参数的默认值

@ApiResponses():用在请求的方法上,表示一组响应。可以包含多个@ApiResponse()
@ApiResponse():用于表示一个错误的响应信息
    code:数字
    message:信息
    response:抛出异常的类      
 
@ApiModel():用在响应类上,表示一个返回响应数据的信息。
@ApiModelProperty():用在属性上,描述响应类的属性
package com.xm.shiro.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; @Api(value="UserController",tags="用户 登录、登出、注册")
@RestController
public class UserController { @RequiresPermissions("hello")
@GetMapping("/hello")
public String hello() {
return "Hello Shiro!";
} @GetMapping("/login")
public String login() {
return "权限管理";
} @ApiOperation(value="根据用户名密码登录",notes="用户登录")
@GetMapping("/doLogin/{username}/{password}")
public String doLogin(@PathVariable("username") String username,@PathVariable("password") String password) {
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
Subject currentUser = SecurityUtils.getSubject();
try {
currentUser.login(token);
//此步将 调用realm的认证方法
} catch(IncorrectCredentialsException e){
//这最好把 所有的 异常类型都背会
return "密码错误";
} catch (AuthenticationException e) {
return "登录失败";
} currentUser.hasRole("sun");
currentUser.hasRole("sun");
currentUser.hasRole("sun");
currentUser.hasRole("sun");
return token.getPrincipal()+":登录成功";
} @ApiOperation(value="用户退出",notes="注销用户")
@GetMapping("/logout")
public String logout() {
Subject currentUser = SecurityUtils.getSubject();
currentUser.logout();
return "退出登录";
} @GetMapping("/noUnauthorized")
public String error() {
return "无权限";
} }

4.登录http://localhost:8080/swagger-ui.html,查看api

15、SpringBoot------整合swagger2的更多相关文章

  1. SpringBoot(七):SpringBoot整合Swagger2

    原文地址:https://blog.csdn.net/saytime/article/details/74937664 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文 ...

  2. SpringBoot整合Swagger2

    相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还是为了以后交接方便,都有要求写API文档. 手写Api文档的几个痛点: 文档需 ...

  3. SpringBoot整合Swagger2(Demo示例)

    写在前面 由于公司项目采用前后端分离,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理 ...

  4. springboot 整合Swagger2的使用

    Swagger2相较于传统Api文档的优点 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时. 接口返回结果不明确 不能直接在线测试接口,通常需要使用工 ...

  5. SpringBoot整合Swagger2案例,以及报错:java.lang.NumberFormatException: For input string: ""原因和解决办法

    原文链接:https://blog.csdn.net/weixin_43724369/article/details/89341949 SpringBoot整合Swagger2案例 先说SpringB ...

  6. SpringBoot整合Swagger2详细教程

    1. 简介   随着前后端分离开发模式越来越流行,编写接口文档变成了开发人员非常头疼的事.而Swagger是一个规范且完整的web框架,用于生成.描述.调用可视化的RESTful风格的在线接口文档,并 ...

  7. SpringBoot整合Swagger2及使用

    简介 swagger是一个流行的API开发框架,这个框架以"开放API声明"(OpenAPI Specification,OAS)为基础, 对整个API的开发周期都提供了相应的解决 ...

  8. SpringBoot整合Swagger2,再也不用维护接口文档了!

    前后端分离后,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理想的状态,实际开发中却很 ...

  9. SpringBoot学习笔记(16)----SpringBoot整合Swagger2

    Swagger 是一个规范和完整的框架,用于生成,描述,调用和可视化RESTful风格的web服务 http://swagger.io Springfox的前身是swagger-springmvc,是 ...

  10. Spring Boot2 系列教程(十七)SpringBoot 整合 Swagger2

    前后端分离后,维护接口文档基本上是必不可少的工作. 一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理想的状态,实际开发中却 ...

随机推荐

  1. javasript数据类型以及如何判断数据类型

    在javascript里面一共有5种基本的数据类型,分别是:Number,String,Boolean,Null,Undefined7种引用类型,分别是:Object类型,Array类型,Date类型 ...

  2. PHP文件上传error的错误类型 - $_FILES['file']['error']

    假设文件上传字段的名称img,则: $_FILES['img']['error']有以下几种类型 1.UPLOAD_ERR_OK 其值为 0,没有错误发生,文件上传成功. 2.UPLOAD_ERR_I ...

  3. python3+Appium自动化13-H5元素定位实践案例

    测试场景 启动钉钉app进入工作H5页面,点击考勤签到 查看webview上元素 1.电脑上打开chrome浏览器输入:chrome://inspect/#devices 2.Discover USB ...

  4. Tomcat安装和配置过程

    tomcat的下载.配置.启动方法.网上教程乱七八糟,还是自己mark一下备用吧. 一.官网下载Tomcat的压缩包 Tomcat是Apache软件基金会的一个开源项目.登录Apache官网即可找到下 ...

  5. pat1038. Recover the Smallest Number (30)

    1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  6. Unicode汉字编码表以及参考源码分享

    1 Unicode编码表  Unicode只有一个字符集,中.日.韩的三种文字占用了Unicode中0x3000到0x9FFF的部分  Unicode目前普遍采用的是UCS-2,它用两个字节来编码一个 ...

  7. 使用js调用摄像头拍照

    在一些浏览器里已经可以使用web api调用摄像头功能了. 基于此可以经行拍照摄像功能,网上找了些资料,然后实现了简单的拍照功能 演示地址 bingxl.cn/webrtc.html 代码 <! ...

  8. c# 字符串大小写混合转换

    我是个.net萌新,在大学是计算机应用专业 学的比较杂 出来准备走net方向  培训了两个月了 今天被出了一道上机题  题本来是挺简单的  输入一个字符 如果是大写则转换为小写  如果是小写则转换为大 ...

  9. 一、简单gridview列表展示

    1.HomeController public ActionResult Index() { //返回绑定models的index.cshtml return View(NorthwindDataPr ...

  10. html学习笔记(一)div的透明设置

    要使得div的透明度设置,有两种方法. 第一种:使用opacity属性,单词的意思是不透明性,你可以设置它的值,范围是0到1,1为不透明,0为全透明.具体使用如下: css代码: #div01{ ba ...