开发工具: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. cmake中文帮助文档

    CMake的 在这个页面 了解CMake的生成命令 在摇篮使用cmake变量 报告问题 使用过Android Studio 2.2及更高版本,可以使用NDK和CMake的 编译C和C ++代码到本机库 ...

  2. derby

    /** * @Title: T.java * @Package test * @Description: TODO please write your description <BR> * ...

  3. [转]jQuery插件写法总结以及面向对象方式写法

    本文转自:http://www.xuanfengge.com/jquery-plug-in-written-summary-and-summary-of-writing-object-oriented ...

  4. [转]dataTables-使用详细说明整理

    本文转自:http://blog.csdn.net/mickey_miki/article/details/8240477 本文共四部分:官网 | 基本使用|遇到的问题|属性表 一:官方网站:[htt ...

  5. ORACLE SQL 实现IRR的计算

    一.IRR计算的原理: 内部收益率(Internal Rate of Return (IRR)),就是资金流入现值总额与资金流出现值总额相等.净现值等于零时的折现率. 用公式 标识:-200+[30/ ...

  6. Murano Weekly Meeting 2016.05.10

    Meeting time: 2016.May.10 1:00~2:00 Chairperson:  Serg Melikyan, from Mirantis Meeting summary: 1. m ...

  7. P2P原理和NAT打洞

    1. P2P协议--点对点通信 1.1 常用的P2P协议 1.1.1 电驴(eMule) 一个电驴网络由服务器端和客户端两部分组成. 服务器端是客户端连接的.为了搜索和查找可以下载用户的桥梁.客户通过 ...

  8. GitKraken使用教程-基础部分(7)

    8.  本地分支和标签 1) 在提交记录区中查看分支状态 提交记录区中每一个分支都位于一个提交记录所在的行中. 从图 2‑1中可以看到,服务器上的master分支停留在整理格式(把这个提交记录记为or ...

  9. GPU学习随笔

    NVML   NVAPI   GDK GDK包含NVML NVAPI库不能提供获取GPU使用率的接口 NVML能提供但不支持geforce系列 NVAPI.dll NVAPI64.dll动态加载可以查 ...

  10. gulp 无损压缩图片

    在做项目中,美工有时候会给一些比较大的图片,在做网站的时候,图片太大会影响加载速度.因此,我们需要无损压缩图片. 在尝试过几个压缩图片的方法,发现gulp中的gulp-tinypng-nokey插件是 ...