开发工具: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. 单片机的图形UI

    https://www.st.com/content/st_com/en/stm32-graphic-user-interface.html TouchGFX Designer:如今免费,资源占用10 ...

  2. 宋宝华: 关于Linux进程优先级数字混乱的彻底澄清

    宋宝华: 关于Linux进程优先级数字混乱的彻底澄清 原创: 宋宝华 Linux阅码场 9月20日 https://mp.weixin.qq.com/s/44Gamu17Vkl77OGV2KkRmQ ...

  3. Kure讲HTML_HTML界面结构

    1.HTML界面结构: 通常通过html开发的网页,它有一个自己固定的书写格式(类似于写信的时候也有固定的格式) <!-- DOCTYPE用来告诉浏览器用当前html文档是用html的哪个版本编 ...

  4. SecureCRT中文乱码解决方案

    SecureCRT是一个商业终端连接工具.SecureCRT可以自定义界面颜色方案,可以连接SSH1与SSH2.Telnet等服务.默认设置下,通过SecureCRT连接SSH服务器可能出现中文乱码的 ...

  5. DedeCms中Channel用typeid无效

    DedeCms中channel 用typeid调用无法达目的吗?请换成type试试! {dede:channel type='son' typeid='19' row='1'} <a href= ...

  6. vue中添加echarts

    方法一:全局引入echarts 步骤: 1.全局安装 echarts依赖.        cnpm install echarts -- save 2.引入echarts模块,在Vue项目的main. ...

  7. Day6上 括号匹配专项

    滑稽的题 T1 #include<iostream> #include<cstring> #include<queue> #include<algorithm ...

  8. nopcommerce 3.6网银在线支付插件(源码)

    网银在线支付插件,下载后通过后台插件管理安装.配置即可使用. 下载:网银在线支付插件3.1.3.6版.rar (106.3KB) 源代码放在\Plugins目录下,用vs打开重新生成. 源地址:htt ...

  9. apache管理命令

    常用的 httpd.exe -k [install(安装).uninstall(卸载).start(启动).stop(停止).restart(重启)] 说明:要执行命令,需进入到apache安装目录/ ...

  10. spring笔记2-注解

    一.属性与成员变量的区别: 属性:对外暴露的,getxxx/setxxx称为属性; 成员变量:private String name称为成员变量或字段 二.applicationContext.xml ...