开发工具: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. Mybatis学习笔记16 - bind标签

    1.${}拼串进行模糊查询,不安全 示例代码: 接口定义: package com.mybatis.dao; import com.mybatis.bean.Employee; import java ...

  2. vue2.X+elementUI表单自定义验证

    <template> <div class="taxi-appointment-dairen"> <el-form :model="rule ...

  3. 386. Lexicographical Numbers 把1--n按字典序排序

    https://leetcode.com/problems/lexicographical-numbers/description/ 前20个是 1, 10, 11, 12, 13, 14, .... ...

  4. 软件使用---Eclipse

    代码提示快捷操作.这个叫做,内容分析(content assist) 1.设置自动提示: 2.设置快捷键:

  5. Barty's Computer 字典树

    https://nanti.jisuanke.com/t/17122 Barty have a computer, it can do these two things. Add a new stri ...

  6. LeetCode 441.排列硬币(C++)

    你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整型的范围内. 示例 ...

  7. JS获取元素属性、样式getComputedStyle()和currentStyle方法兼容性问题

    1. getComputedStyle()  方法获取到的是经过计算机/浏览器计算后的样式 getComputedStyle($("#div")).width; 兼容性:IE6 7 ...

  8. 初步学习XML的基本代码

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. maven配置好了之后再次开机找不到命令

    问题: 昨天还运行的好好的,今天突然又报错了, mvn不是内部或外部命令,也不是可运行程序或批处理文件 原因: 环境配置问题,windows7和windows10稍微有一点不一样,对照下面配置看哪里不 ...

  10. php支付走过的坑(支付宝篇 注册 秘钥 环境等等配置)

    支付这东西,说容易也容易,说难也难 代码这玩意还比较好说 但是 如果没有demo 直接去看官方文档 十有八九一脸懵逼 今天就整理一下 支付这块走过的坑 涉及 微信h5支付 支付宝h5支付 (api文档 ...