当前位置: 首页 > news >正文

信和财富网站开发电影院卖品套餐计划

信和财富网站开发,电影院卖品套餐计划,好用的外贸网站,聊城建网站springboot2集成knife4j springboot2集成knife4j 环境说明集成knife4j 第一步:引入依赖第二步:编写配置类第三步:测试一下 第一小步:编写controller第二小步:启动项目,访问api文档 相关资料 环境说明 …

springboot2集成knife4j

  • springboot2集成knife4j
    • 环境说明
    • 集成knife4j
      • 第一步:引入依赖
      • 第二步:编写配置类
      • 第三步:测试一下
        • 第一小步:编写controller
        • 第二小步:启动项目,访问api文档
    • 相关资料

环境说明

  • springboot:2.6.4
  • knife4j-openapi2-spring-boot-starter:4.0.0

集成knife4j

第一步:引入依赖

<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-openapi2-spring-boot-starter</artifactId><version>4.0.0</version>
</dependency>

第二步:编写配置类

提示:可以借助配置文件,进一步改造

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;/*** Knife4j配置** @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img* src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />* @since 1.0.0*/
@Slf4j
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {@Value("${spring.application.name:default}")private String applicationName;@Beanpublic Docket docket() {// 指定使用Swagger2规范return new Docket(DocumentationType.SWAGGER_2).apiInfo(new ApiInfoBuilder()// 简介(支持Markdown语法).description("# 我是API简介")// 服务地址.termsOfServiceUrl("http://local.idea-aedi.com/")// 作者及联系信息.contact(new Contact("JustryDeng", "https://gitee.com/JustryDeng", "13548417409@163.com"))// api版本.version("1.0.0").build())//分组名称(微服务项目可以用微服务名分组).groupName(applicationName).select()// 定位api.apis(RequestHandlerSelectors.basePackage(getProjectBasePackage()).and(RequestHandlerSelectors.withClassAnnotation(RestController.class).or(RequestHandlerSelectors.withClassAnnotation(Controller.class)))).paths(PathSelectors.any()).build();}/*** 获取项目包前缀*/private String getProjectBasePackage() {String projectBasePackage;String currPackageName = this.getClass().getPackage().getName();String[] packageItemArr = currPackageName.split("\\.");if (packageItemArr.length > 3) {projectBasePackage = String.join(".", packageItemArr[0], packageItemArr[1], packageItemArr[2]);} else {projectBasePackage = currPackageName;}log.info("Base package to scan api is -> {}", projectBasePackage);return projectBasePackage;}}

第三步:测试一下

第一小步:编写controller

import com.ideaaedi.demo.controller.model.UserAddReqVO;
import com.ideaaedi.demo.controller.model.UserDetailRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;/*** 用于测试knife4j的controller** @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img* src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />* @since 1.0.0*/
@RestController
@Api(tags = "我是DemoController")
public class TestController {@GetMapping("/hello")@ApiOperation(value = "哈喽")public String hello(@ApiParam(name = "name", value = "姓名", required = true)@RequestParam String name) {return "hello " + name;}@PostMapping("/user/add")@ApiOperation(value = "新增用户")public UserDetailRespVO addUser(@RequestBody UserAddReqVO req) {UserDetailRespVO resp = new UserDetailRespVO();resp.setId(9527L);resp.setName(req.getName());resp.setAge(req.getAge());return resp;}@DeleteMapping("/user/delete/{id}")@ApiOperation(value = "删除用户")public Boolean addUser(@ApiParam(name = "id", value = "数据id", required = true) @PathVariable Long id) {return true;}/*** 测试 @RequestBody、@RequestParam、@PathVariable并存*/@PostMapping("/multi-anno/{id}")@ApiOperation(value = "组合使用测试")@ApiParampublic UserDetailRespVO testMultiAnno(@RequestBody UserAddReqVO req,@ApiParam(name = "name", value = "姓名", required = true)@RequestParam String name,@ApiParam(name = "id", value = "数据id", required = true)@PathVariable Long id) {UserDetailRespVO resp = new UserDetailRespVO();resp.setId(9527L);resp.setName(req.getName());resp.setAge(req.getAge());return resp;}}

此controller中用到的相关模型

  • UserAddReqVO

    import io.swagger.annotations.ApiModelProperty;
    import lombok.Data;import javax.validation.constraints.NotBlank;/*** 用户新增req模型*/
    @Data
    public class UserAddReqVO {@ApiModelProperty(value = "姓名",required = true)@NotBlank(message = "姓名不能为空")private String name;@ApiModelProperty("年龄")private Integer age;
    }
    
  • UserDetailRespVO

    import io.swagger.annotations.ApiModelProperty;
    import lombok.Data;/*** 用户详情resp模型*/
    @Data
    public class UserDetailRespVO {@ApiModelProperty("id")private Long id;@ApiModelProperty("姓名")private String name;@ApiModelProperty("年龄")private Integer age;
    }
    

第二小步:启动项目,访问api文档

启动项目后,直接访问http://{ip}:{端口}/doc.html即可

content

在这里插入图片描述

说明:

  • 文档分组:可切换观察其余分组下的api
  • 主页:概览
  • Swagger Models:可以查看所有请求模型的信息
  • 文档管理:可以导出文档、进行高级设置(如设置后处理脚本等)、进行全局参数设置、查看api信息
  • 点击进入文档后,会展示api的详细信息,也可以进行调试,还可以打开api json数据等等

相关资料

  • demo代码下载
  • knife4j官网
  • 本文已被收录进《程序员成长笔记》 ,笔者JustryDeng
http://www.yayakq.cn/news/138860/

相关文章:

  • wordpress的wiki主题解答网站内容优化策略
  • 网站分为哪几种类型网站备案是什么
  • phpmysql网站开发入门与提高怎样用vs做简单网站
  • 国外网站建设视频教学泰州网站排名seo
  • 公司网站微信推广创意网站 案例 下载
  • 西安网站建设发布制作一个网站平台吗
  • 做网站公司南京网络运营公司经营范围
  • 专业 网站设计公司价格怎么给自己的网站更换域名
  • 网站图片用什么做烟台汽车租赁网站建设
  • 百度做个公司网站要多少钱上传网站的软件
  • 开源企业网站管理系统中天建设集团网站
  • 中国最近新闻消息肇庆seo外包服务
  • 广西城乡住房建设厅网站首页多个网站备案
  • 西安 网站建设 培训班代做网站关键词排名
  • 网站 功能呢ui设计学费多少 要学多久
  • asp代码如何修改asp网站网页域名名称wordpress如何新建模板页面
  • 站内优化怎么做网页设计html代码教程
  • 博爱网站建设深圳工程建设交易服务中心网站
  • 个人做二次元网站怎么赚钱做推送用的网站
  • 网站设计 趋势手机参数查询网站
  • 住房和城乡建设部网站主页杭州百度推广代理公司哪家好
  • 装修推广网站哪个好四川成都建设网
  • 沈阳做网站的公司推荐宿迁明远建设有限公司网站
  • 金融 网站 模板网站建设开源模板
  • 省市建设类网站链接站长网站统计
  • asp网站建设实录源码定制网站建设公司
  • 设计手机网站温州广告网页设计招聘网
  • 软装设计网站排名网站这么推广
  • 菏泽科技网站建设涉密网络建设
  • 网站空间价格在哪个网站做流动补胎的广告好