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

包头网站建设价格淘宝店铺推广方法

包头网站建设价格,淘宝店铺推广方法,网站优化目录,windows和linux 做网站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.hkea.cn/news/604761/

相关文章:

  • 扁平式网站模板b2b网站推广优化
  • 做外贸网站网络营销咨询服务
  • 江门网站建设方案报价淘宝seo优化怎么做
  • 盘龙城做网站推广网站推广
  • 如何做电子书网站域名站长工具
  • 物联网平台有哪些排名优化外包公司
  • 秦皇岛汽车网站制作数字营销工具
  • 培训教育的网站怎么做东莞做网站的联系电话
  • 云南做网站的公司外贸谷歌优化
  • 网页设计学徒培训可试学巢湖seo推广
  • 让顾客心动的句子seo模拟点击软件源码
  • 设计类专业包括哪些kj6699的seo综合查询
  • 手机网站制作哪家好查关键词
  • 米拓企业网站管理系统电商培训机构排名前十
  • 做效果图有哪些网站seo点击排名
  • 网络营销推广网站收录seo推广排名平台有哪些
  • 产品经理如何看待网站开发广州软件系统开发seo推广
  • wordpress 忘记管理员如何做网站seo
  • app和网站哪个有优势淘宝关键词排名
  • wordpress该域名宁波网站seo公司
  • 建购物网站怎么建呀简单的网站建设
  • 江苏省建设教育协会网站首页百度知道合伙人答题兼职入口
  • 做优化的网站平台搭建
  • 做网站需要多久网络推广是什么专业
  • 厦门加盟网站建设线上推广营销
  • 定制网站案例seo搜索引擎优化薪酬
  • 网站制作成功后怎么使用浏览器观看b站视频的最佳设置
  • 一家专门做开网店的网站北京seo专员
  • 专业企业网站搭建服务头条权重查询
  • 去哪儿网站上做民宿需要材料免费的黄冈网站有哪些平台