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

做党政板报的网站给你一个网站怎么优化

做党政板报的网站,给你一个网站怎么优化,做网站的会淘宝美工么,做摄影的网站知乎Swagger3 使用详解 一、简介1 引入依赖2 开启注解3 增加一个测试接口4 启动服务报错1.5 重新启动6 打开地址:http://localhost:8093/swagger-ui/index.html 二、Swagger的注解1.注解Api和ApiOperation2.注解ApiModel和ApiModelProperty3.注解ApiImplicitParams和Api…

Swagger3 使用详解

  • 一、简介
    • 1 引入依赖
    • 2 开启注解
    • 3 增加一个测试接口
    • 4 启动服务报错
    • 1.5 重新启动
    • 6 打开地址:http://localhost:8093/swagger-ui/index.html
  • 二、Swagger的注解
    • 1.注解@Api和@ApiOperation
    • 2.注解@ApiModel和@ApiModelProperty
    • 3.注解@ApiImplicitParams和@ApiImplicitParam
    • 4.注解@ApiResponses和ApiResponse
    • 5.注解@ApiIgnore
  • 三、Swagger的相关配置

一、简介

官方网站:https://swagger.io/
Swagger 是一个开源的 API 开发和文档框架,Swagger 旨在简化 RESTful API 的设计、开发、测试、文档化和消费过程。Swagger的出现节约了大量的后端人员对接接口的时间,通过Swagger可以快速定义接口文档,方便了前端后端的接口对接工作,废话不多说直接上用例。
这里使用的SpringBoot 是2.6.11 版本

1 引入依赖

<dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version>
</dependency>

2 开启注解

启动类增加注解

@EnableSwagger2
// 或者使用下面的注解,也是可以的
@EnableOpenApi

3 增加一个测试接口

增加一个接口,方便看到Swagger帮我们生成的接口文档中的接口信息,如下:

package com.cheng.ebbing.message.controller;import com.cheng.ebbing.message.vo.UserVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author pcc* @version 1.0.0* @description 测试接口*/
@RestController
public class TestController {@RequestMapping(value ="/test")public UserVO test(){return new UserVO("zhangsan","1234",1);}
}

4 启动服务报错

报错:Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
原因可以参考这篇文章 https://blog.csdn.net/weixin_45131680/article/details/131580270,总结一句话就是SpringBoot2.6以上的版本整合Swagger2:3.0.0会碰到这个问题,根本原因是路径匹配模式不适配。解决方案有两个,他们的目的是为了更改路径匹配规则以适配Swagger2 的3.0 的版本。

  • 解决方案一:
    // 启动类增加了开启swagger注解的基础上增加以下注解
    @EnableWebMvc
    
  • 解决方案二:
    配置文件增加以下配置:
    spring:mvc:pathmatch:matching-strategy: ant_path_matcher
    

1.5 重新启动

选择上面其中一个方案进行解决即可,然后重新启动就会正常了,如下:
在这里插入图片描述

6 打开地址:http://localhost:8093/swagger-ui/index.html

到这里swagger的入门集成就成功了,如果你没有设置项目的根路径,那么Swagger的地址就是这个了,如果设置了自行端口后增加根路径就行,打开后如下图:
在这里插入图片描述
界面里有四块信息是需要关注的。
左一:展示的信息支持自定义,可以用来标识文档的和服务的信息(第四部分进行详细介绍)
左二:接口展示,这里展示接口的详细信息,最常用的地方,可以看到上面定义的测试接口已经在这里了(第二部分详细介绍)
左三:这里展示我们再请求和响应中定义的实体信息,比如上面接口的UserVO就在这里(第二部分详细介绍)
右一:分组信息,一般用于多个微服务时对不同微服务进行分组,常用与和Gateway集成时对服务进行分组展示(第三部分详细介绍)

二、Swagger的注解

这里详细说下Swagger常用的注解,根据使用频率来进行先后说明。这里分为了五个部分来说,前四个部分都是分组来说的,每组注解基本都是一起使用的所以就放一起来说,也方便理解记忆。下面的前四组例子都将使用使用注解和不使用注解的方式来进行说明比对,以此来方便了解注解的具体的作用。

1.注解@Api和@ApiOperation

这是使用频率最高的一组注解了。@Api常用与接口类上,用以对当前的接口类做整体声明。@ApiOperation则用于接口方法上,用以描述具体的方法真。

  • @Api
    正使用时其实只需要为@Api声明tags标签即可(其他的基本用不到,如果需要再阅读下注解的源码即可),value的值在源码描述中说是当tags不使用时会将其作为资源的描述,但是笔者试了没啥用,而且大家都是使用tags,直接使用tags标签即可。假如存在下面两个方法,他们的信息基本都是相同的,一个使用了@Api注解,一个没有使用:

    // 使用了 @Api注解的类
    @Api(tags = "测试接口类1")
    @RestController
    public class TestController1 {@PostMapping("/test1Fun")public String test() {return "test1";}
    }// 未使用@Api注解的类
    @RestController
    public class TestController2 {@PostMapping("/test2Fun")public String test() {return "test2";}
    }
    

    下面一起看下他们在Swagger中的展示区别吧(注意重启生效):
    在这里插入图片描述
    可以看到使用后多了汉字的描述,这样对于文档使用者来说也就更为有好了,这也就是tags属性的作用了,其他属性基本不咋使用。

  • @ApiOperation
    这个注解则是使用在接口方法上的,用以描述一个具体的接口,它支持的所有属性都是和RequestMapping对应的,因为他本来就是用来描述接口的和RequestMapping对应则是很好理解的。不过最常用的还是他的value属性,用以简介当前的接口。其他用以描述接口的信息一般不设置也是可以看到的,我们一般使用value属性简单描述接口的作用。
    下面是一组使用@ApiOperation和不使用的代码:

    
    // 使用注解@ApiOperation
    @ApiOperation("测试接口1-方法1")
    @PostMapping("/test1Fun")
    public String test() {return "test1";
    }// 不使用注解@ApiOperation
    @PostMapping("/test2Fun")
    public String test() {return "test2";
    }
    

    下面一起看下使用注解和不使用注解在Swagger中的区别:
    在这里插入图片描述

2.注解@ApiModel和@ApiModelProperty

这组注解是使用在实体上的,一般前后端参数交互时都是使用Json数据进行交互,Json交互时后端使用实体和注解@RequestBody来接收前端传递的参数,而这组注解则是用来描述实体的。实体既可以作为接收参数当然也可以作为返回参数。下面一起来看下。

  • @ApiModel
    被用于修饰实体类,实体类可用于接口入参和返参,一般只使用value属性即可。假设有如下两个实体信息,一个加了ApiModel一个没有加:

    // 使用了注解@ApiModel的注解
    @Data
    @ApiModel("用户实体1")
    @AllArgsConstructor
    @NoArgsConstructor
    public class UserVO1 implements Serializable {private static final long serialVersionUID = 1L;private String username;private String password;Integer id;
    }// 不使用注解@ApiModel
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class UserVO2 implements Serializable {private static final long serialVersionUID = 1L;private String username;private String password;Integer id;
    }
    

    先看下使用了注解时的样子(不使用则显示类名):
    在这里插入图片描述
    下面是不使用的样子:
    在这里插入图片描述
    此外在下面的位置也是可以看出区别的:
    在这里插入图片描述

  • @ApiModelProperty
    这个属性被用在实体的属性上,用来标识实体属性的含义,这个还是很有用的,因为不用他来标识的话有些字段是人无法知道具体代表的含义的。这个也是基本使用value即可,下面也是使用使用和不使用注解来比对下区别,这里就只贴使用注解的代码了

    @Data
    @ApiModel("用户实体1")
    @AllArgsConstructor
    @NoArgsConstructor
    public class UserVO1 implements Serializable {private static final long serialVersionUID = 1L;@ApiModelProperty("用户名")private String username;@ApiModelProperty("用户密码")private String password;@ApiModelProperty("用户ID")Integer id;
    }
    

    下面是在swagger文章中看到的区别:
    在这里插入图片描述

3.注解@ApiImplicitParams和@ApiImplicitParam

上面一组注解被用于标识使用实体接收和返回的数据对象,那么如果接受的数据不是json呢,则需要别的注解来处理了,这一组注解就可以实现这个功能。

  • 参数传递大致有以下几种
    • 1.使用body传递json,后端使用注解RequestBody来接收
    • 2.使用body传递form-data,后端使用注解RequestParam接收(文件除外)
    • 3.使用body传递x-www-form-urlencoded,后端使用注解RequestParam接收
    • 4.使用path传参(restful),后端使用注解PathVariable来接收
    • 5.使用query传参,即url传参,后端使用注解RequestParam接收
    • 6.使用Cookie传参,后端使用注解CookieValue接收

上面列举了集中数据的传递方式,可以看到除了Json进行传递,还有一些其他的数据传递,此时@ApiModel这组注解就无法满足我们的需要了,那就只能使用@ApiImplicitParams和@ApiImplicitParam。上面的这些数据传递(除了json)都会在接口方法有参数进行映射。所以使用这组注解就都可以进行声明。这组注解和上面还有一点区别就是必须同时使用。

这里使用query传参进行说明吧。假设有如下接口代码:

@ApiOperation("测试接口1-方法1")
@PostMapping("/test1Fun")
public UserVO1 test(@RequestParam("name")String name) {return new UserVO1("用户1","1234",10);
}

不使用注解时,swagger展示如下:
在这里插入图片描述
当添加了如下注解以后:

@ApiOperation("测试接口1-方法1")
@PostMapping("/test1Fun")
@ApiImplicitParams({@ApiImplicitParam(name = "name",value = "用户名",required = true,paramType = "query",dataType = "String",dataTypeClass = String.class)
})
public UserVO1 test(@RequestParam("name")String name) {return new UserVO1("用户1","1234",10);
}

swagger中展示如下:
在这里插入图片描述
注意:可以看到只是多了在注解ApiImplicitParam中描述的value其他都是相同的,所以说其实使用这组注解时只需要关注ApiImplicitParam他的value就行了。不过需要注意的是这里还是用了注解@RequestParam,swagger文档中的required和query是这个注解赋予的,如果没有这个注解swagger文档是不会有对应提示的。还有一点,若是使用ApiImplicitParam的dataType则一定要使用dataTypeClass不然会一直提示warn日志
在这里插入图片描述

4.注解@ApiResponses和ApiResponse

上一组注解时用来描述除了实体以外的入参的,这个注解从字面看就知道是跟返回信息有关的,先看下不加这个注解时的接口的返回在swagger中的展示:
在这里插入图片描述
然后我们为接口增加这组注解,如下:

    @ApiOperation("测试接口1-方法1")@PostMapping("/test1Fun")@ApiResponses({@ApiResponse(code = 200, message = "成功", response = UserVO1.class,reference = "UserVO1"),@ApiResponse(code = 400, message = "失败",response = UserVO1.class,reference = "UserVO1")})public UserVO1 test(@RequestParam("name")String name) {return new UserVO1("用户1","1234",10);}

下面是使用注解后的swagger文档展示:
在这里插入图片描述
可以看到真正有作用的是code和message,所以如果需要使用就使用这两个即可,不过一把不使用这组注解,因为后端服务一般不会抛出http的异常码,而是通过实体的错误码来标识响应信息。

5.注解@ApiIgnore

这个注解也比较简单,可以使用在类或者方法上都是可以的,用以将其排除,排除后swagger不会将其加入文档。
假如有如下代码:

    @ApiIgnore@ApiOperation("测试接口1-方法1")@PostMapping("/test1Fun")@ApiResponses({@ApiResponse(code = 200, message = "成功", response = UserVO1.class,reference = "UserVO1"),@ApiResponse(code = 400, message = "失败",response = UserVO1.class,reference = "UserVO1")})public UserVO1 test(@RequestParam("name")String name) {return new UserVO1("用户1","1234",10);}

因为加了该注解,我们再swagger中是看不到的:
在这里插入图片描述

三、Swagger的相关配置

其实不添加任何配置不影响使用,但知道配置方便我们更好地针对自己的特别需求进行定制化。所以配置还是需要进行了解和熟悉的。比如生产上是不推荐打开swagger的,因为有很大的安全隐患。下面一起说下,注意这些配置信息都需要放在配置文件,这里为了方便都直接写死了。

package com.cheng.ebbing.message;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;/*** @author pcc* @version 1.0.0* @description swagger3配置类*/
@EnableWebMvc // 适配springboot2.6和swagger3
@EnableOpenApi // 开启swagger3
@Configuration
public class config {@Beanpublic Docket  createRestApi(){return new Docket(DocumentationType.OAS_30).enable(true) // 开启swagger,生产建议关闭.apiInfo(apiInfo()) // 配置文档的基础信息.groupName("这是分组名")// 不设置默认都是default,单个服务可以不使用分组,如果想要多个分组再来个Docket设置组名即可.select() // 开始配置路径相关信息// 扫描固定包,其他包下的信息不回出现在swagger.apis(RequestHandlerSelectors.basePackage("com.cheng.ebbing"))// 扫描所有有注解的api,用这种方式更灵活,也可以扫描指定的接口,支持通配符.paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {// 配置文档的基础信息,例如标题、描述等return new ApiInfoBuilder().title("这是title").description("这是description").license("这是license").licenseUrl("这是licenseUrl").termsOfServiceUrl("这是termsOfServiceUrl").contact(new Contact("concat-name","concat-url","concat-email")).version("版本1.0.0").build();}
}

增加完配置信息以后,swagger文档展示如下:
在这里插入图片描述
这些配置信息,注意不要直接写死,而应该从配置文件获取,这里直接写死只是为了演示使用,swagger只是项目辅助的小框架就只说到这里了。


文章转载自:
http://dinncowady.tqpr.cn
http://dinncoretake.tqpr.cn
http://dinncotelangiectasis.tqpr.cn
http://dinncodeuterogenesis.tqpr.cn
http://dinncoseventhly.tqpr.cn
http://dinncogross.tqpr.cn
http://dinncounchoke.tqpr.cn
http://dinncosaleswoman.tqpr.cn
http://dinncopolycondensation.tqpr.cn
http://dinncoagranulocyte.tqpr.cn
http://dinncomollycoddle.tqpr.cn
http://dinncovengefully.tqpr.cn
http://dinncoballistician.tqpr.cn
http://dinncogrunt.tqpr.cn
http://dinncobatangas.tqpr.cn
http://dinncobert.tqpr.cn
http://dinncoribotide.tqpr.cn
http://dinncomidear.tqpr.cn
http://dinncorabbity.tqpr.cn
http://dinncofishing.tqpr.cn
http://dinncodnf.tqpr.cn
http://dinncoblatant.tqpr.cn
http://dinncolaparectomy.tqpr.cn
http://dinncorationally.tqpr.cn
http://dinncogeordie.tqpr.cn
http://dinncomonocarboxylic.tqpr.cn
http://dinncodisenroll.tqpr.cn
http://dinncoweaponless.tqpr.cn
http://dinncoparallelogram.tqpr.cn
http://dinncocouncillor.tqpr.cn
http://dinncobenignancy.tqpr.cn
http://dinncoyugoslavia.tqpr.cn
http://dinncomultiprocessor.tqpr.cn
http://dinncopsychiater.tqpr.cn
http://dinncoinnocence.tqpr.cn
http://dinncoundersoil.tqpr.cn
http://dinncoarterialize.tqpr.cn
http://dinncohexane.tqpr.cn
http://dinncoferrous.tqpr.cn
http://dinncofront.tqpr.cn
http://dinncocalamus.tqpr.cn
http://dinncophantasize.tqpr.cn
http://dinncocastellated.tqpr.cn
http://dinncobodyguard.tqpr.cn
http://dinncounsightly.tqpr.cn
http://dinncoflavine.tqpr.cn
http://dinncogrungy.tqpr.cn
http://dinncoreit.tqpr.cn
http://dinncocasaba.tqpr.cn
http://dinncostovepipe.tqpr.cn
http://dinncohypertherm.tqpr.cn
http://dinncounrent.tqpr.cn
http://dinncolop.tqpr.cn
http://dinncocrankpin.tqpr.cn
http://dinncoprotractile.tqpr.cn
http://dinncodecimalist.tqpr.cn
http://dinncometatherian.tqpr.cn
http://dinncotracheotomy.tqpr.cn
http://dinncomicrometeor.tqpr.cn
http://dinncovalvular.tqpr.cn
http://dinncoudf.tqpr.cn
http://dinncoatone.tqpr.cn
http://dinncoironsmith.tqpr.cn
http://dinncoradiantly.tqpr.cn
http://dinncoinsinuative.tqpr.cn
http://dinncoestimable.tqpr.cn
http://dinncopostponed.tqpr.cn
http://dinncolittermate.tqpr.cn
http://dinncoantigenicity.tqpr.cn
http://dinncobookplate.tqpr.cn
http://dinncooverparted.tqpr.cn
http://dinncounlid.tqpr.cn
http://dinncoseclusion.tqpr.cn
http://dinncodulcinea.tqpr.cn
http://dinncoheight.tqpr.cn
http://dinncokindless.tqpr.cn
http://dinncoarmamentarium.tqpr.cn
http://dinncomentally.tqpr.cn
http://dinncoperikaryon.tqpr.cn
http://dinncoblowfly.tqpr.cn
http://dinncoreremouse.tqpr.cn
http://dinnconorthwest.tqpr.cn
http://dinncodiacid.tqpr.cn
http://dinncohunk.tqpr.cn
http://dinncoregensburg.tqpr.cn
http://dinncoasphaltum.tqpr.cn
http://dinncodoven.tqpr.cn
http://dinncobuccal.tqpr.cn
http://dinncocaesium.tqpr.cn
http://dinncoperfecta.tqpr.cn
http://dinncostoss.tqpr.cn
http://dinncoganges.tqpr.cn
http://dinncotaxpaying.tqpr.cn
http://dinncodoss.tqpr.cn
http://dinncofilmgoer.tqpr.cn
http://dinncorexine.tqpr.cn
http://dinncoyaleman.tqpr.cn
http://dinncounarmoured.tqpr.cn
http://dinncoincipience.tqpr.cn
http://dinncoclitoris.tqpr.cn
http://www.dinnco.com/news/110966.html

相关文章:

  • 西安 网站开发 招聘成都官网seo费用
  • 廊坊高端模板建站嘉兴百度快照优化排名
  • 网站建设个人工作室重庆网站建设软件
  • 自己做的网站页面错误上海网站排名推广
  • 网站整体建设方案论文网络营销与直播电商专业学什么
  • 制作一个网站平台需要多少钱单页面seo搜索引擎优化
  • 做3d动画视频接私活的网站百度关键词排名突然下降很多
  • 卢湾品牌网站建设百度seo引流怎么做
  • 惠州疫情最新消息2021seo的优化技巧和方法
  • 做竞价可以让网站提高快速排名吗网站内部seo优化包括
  • 中国服装设计网站郑州网络推广团队
  • 10个免费的黑科技网站软文营销的技巧有哪些?
  • 郑州做网站的公司哪家社区建站网站系统
  • 有没有专门做外贸的网站最新全国疫情实时大数据
  • vs2010网站制作教程北京网站优化企业
  • 怎样用8uftp做网站媒体资源网
  • 中小企业网站开发google下载app
  • 中国建设银行网站首页网站推广培训
  • wordpress商品展示模块seo是什么专业
  • 邢台做网站优化哪儿好seo站长优化工具
  • 企业网站设计注意事项seo搜索引擎优化主要做什么
  • vs连接数据库做网站网络推广软文怎么写
  • 百度公司网站怎么建设汽车宣传软文
  • 网站建设的新闻动态百度云资源搜索
  • 个人微信号做网站行吗做一个简单网页
  • 做网站需要什么配置的电脑百度网站首页网址
  • 局网站建设管理整改情况手机如何做网站
  • 做网站需要用到哪些编程知识高明公司搜索seo
  • 郑州网站建设新闻电商网店
  • 青岛博海建设网站谷歌浏览器下载手机版