名数的英文译语怎么说-cardboard
2023年3月31日发(作者:博大考神)
利⽤Swagger2⾃动⽣成对外接⼝的⽂档
⼀直以来做对外的接⼝⽂档都⽐较原始,基本上都是⼿写的⽂档传来传去,最近发现了⼀个新玩具,可以在接⼝上省去不少⿇烦。
swagger是⼀款⽅便展⽰的API⽂档框架。它可以将接⼝的类型最全⾯的展⽰给对⽅开发⼈员,避免了⼿写⽂档的⽚⾯和误差⾏为。
swagger⽬前有两种swagger和swagger2两种,1⽐较⿇烦,所以不考虑使⽤。本⽂主要记录我⽤swagger2做对外接⼝的两种⽅式,⽅⾯后⾯查阅。
⼀、使⽤传统的springmvc整合swagger2
1、maven依赖
2、中添加映射静态的配置(其实我项⽬中把这个去掉也可以,不知道什么情况):
注意:基本的springmvc配置我就不贴了,需要注意的是,如果你看到界⾯出来,但却⼀⽚空⽩,请检查下你中拦截器的配
置,⼀定要springmvc先拦截到,然后界⾯才会显⽰的。
3、然后是swagger2的配置类:
@Configuration
@EnableSwagger2
publicclassSwaggerConfigextendsWebMvcConfigurationSupport{
@Bean
publicDocketcreateRestApi(){
returnnewDocket(R_2)
.apiInfo(apiInfo())
.select()
.apis(ckage(\"\"))
.paths(())
.build();
}
privateApiInfoapiInfo(){
returnnewApiInfoBuilder()
.title(\"yyblog项⽬RESTfulAPIs\")
.description(\"yyblog项⽬api接⼝⽂档\")
.version(\"1.0\")
.build();
}
}
注意:paths如果在⽣产情况下可以调整为(),即不显⽰所有接⼝信息;
4、接⼝信息配置
即在SpringMvc的Controller中配置相关的接⼝信息
@Controller
@RequestMapping(value=\"aitou\")
@Api(description=\"测试服务-账户信息查询\")
publicclassDailyOperationDataController{
Loggerlogger=ger();
@Autowired
privateDailyOp介绍端午节的来历 erationDataServiceDailyOperationDataService;
/*
*@ApiOperation(value=\"接⼝说明\",httpMethod=\"接⼝请求⽅式\",response=\"接⼝返回参数类型\",notes=\"接⼝发布说明\"
*@ApiParam(required=\"是否必须参数\",name=\"参数名称\",value=\"参数具体描述\"
*/
@ApiOperation(value=\"账户信息查询接⼝\")
@RequestMapping(method={,},value=\"/query/dailydata/{dataDate}\")
@ResponseBody
publicDailyOperationDataDtogetDailyReportByDataDate(@PathVariable(\"dataDate\")StringdataDate){
try{
lyReportByData二年级上册语文书电子版 Date(dataDate);
}catch(Exceptione){
(sage(),e);
}
returnnull;
}
}
注:通常情况下swagger2会将扫描包下所有的接⼝展⽰出来,这⾥我是对外的接⼝是单独⼀个包,避免展⽰过多的接⼝,当然接⼝⽅法也可以让它不
展⽰。可以在下⾯看到相关的注解中有写。
常⽤的⼀些注解
@Api:⽤在类上,说明该类的作⽤
@ApiOperation:⽤在⽅法上,说明⽅法的作⽤
@ApiImplicitParams:⽤在⽅法上包含⼀组参数说明
@ApiImplicitParam:⽤在@ApiImplicitParams注解中,指定⼀个请求参数的各个⽅⾯
paramType:参数放在哪个地⽅
header-->请求参数的获取:@RequestHeader
query-->请求参数的获取:@RequestParam
path(⽤于restful接⼝)-->请求参数的获取:@PathVariable
body(不常⽤)
form(不常⽤)
name:参数名
dataType:参数类型
required:参数是否必须传
value:参数的意思
卧组词 defaultValue:参数的默认值
@ApiResponses:⽤于表⽰⼀组响应唯一的近义词
@ApiResponse:⽤在@ApiResponses中,⼀般⽤于表达⼀个错误的响应信息
code:数字,例如400
message:信息,例如\"请求参数没填好\"
response:抛出异常的类
@ApiParam:单个参数描述
@ApiModel:描述⼀个Model的信息,⽤对象来接收参数(这种⼀般⽤在post创建的时候,使⽤@RequestBody这样的场景,请求参数⽆
法使⽤@ApiImplicitParam注解进⾏描述的时候)
@ApiModelProperty:描述⼀个model的属性
@ApiProperty:⽤对象接收参数时,描述对象的⼀个字段
@ApiIgnore:使⽤该注解忽略这个API
基本上就是上⾯这些了,是不是很easy,下⾯看下效果图
⼆、使⽤springboot整合swagger2
上⾯说了使⽤传统的springmvc整合swagger2,在说说最近⽐较流⾏的springboot的⽅式,其实原理都是⼀样的。
1、maven依赖
这个是我最近⽤springboot写的个⼈项⽬中的内⽤,版本⽤的2.7.0
2、添加静态资源配置
@Configuration
publicclassWebMvcConfigextendsWebMvcConfigurerAdapter{/**
*配置静态资源路径以及上传⽂件的路径
*
*@paramregistry
*/
@Override
publicvoidaddResourceHandlers(ResourceHandlerRegistryregistry){
ourceHandler(\"/static/**\").addResourceLocations(\"classpath:/static/\");
ourceHandler(\"/upload/**\").addResourceLocations(perty(\"-locations\"));
/*swagger-ui*/
ourceHandler(\"\").addResourceLocations(\"classpath:/META-INF/resources/\");
ourceHandler(\"/webjars/**\").addResourceLocations(\"classpath:/META-INF/resources/webjars/\");
}
}
其实就是最后两句,如果你不配置这个的话,访问会出现500,还是404的错误来着,记不清了,应该是404.
3、swagger2的配置类
和上⾯⼀样,基本上没区别
@Configuration
@EnableSwagger2
@EnableWebMvc
publicclassSwaggerConfigextendsWebMvcConfigurationSupport{
@Bean
publicDocketcreateRestApi(){
returnnewDocket(R_2)
.apiInfo(apiInfo())
.select()
.apis(ckage(\"nd\"))
.paths(())
.build();
}
privateApiInfoapiInfo(){
returnnewApiInfoBuilder()
.title(\"yyblog项⽬RESTfulAPIs\")
.description(\"yyblog项⽬api接⼝⽂档\")
.version(\"1.0\")
.build();
}
}
注意,我上⾯有说path的问题哦,直接拷贝不显⽰api的,(#^.^#)
4、接⼝的配置
/**
*前台⽂章Controller
*@author⼩卖铺的⽼爷爷
*@date2018年5⽉5⽇
*@
*/
@Api(description=\"⽂章查询\")
@Controller
@RequestMapping(\"/article\")
publicclassArticleController{
@Autowired
privateArticleServicearticleService;
@Autowired
privateSettingServicesettingService;
@Autowired
privateCateServicecateService;
@Autowired
privateTagReferServicetagReferService;
@Autowired
privateUserServiceuserService;
@Autowired
privateArticleMapperarticleMapper;
@Autowired
privateCommentServicecommentService;
@ApiOperation(value=\"⽂章查询接⼝\")
@ApiImplicitParam(name=\"id\",忆的拼音 value=\"⽂章ID\",required=true,dataType=\"Long\")
@Get计日程功 Mapping(\"/{id}\")
publicStringindex(Modelmodel,@PathVariable(\"id\")Longid){
try{
ViewsById(id);
}catch(Exceptionignore){
}
List
Map
for(Settingsetting:settings){
(e(),ue());
}
Articlearticle=icleById(id);
ribute(\"settings\",map);
ribute(\"cateList\",lCate());
ribute(\"article\",article);
ribute(\"tags\",meByArticleId(()));
ribute(\"author\",knameById(horId()));
//回头改
ribute(\"articles\",ticleByTitle(null));
ribute(\"similars\",ticleByTitle(null));
CommentQueryquery=newCommentQuery();
it(10);
e(1);
icleId(id);
ribute(\"comments\",mmentByArticleId(query));
return\"frontend/article\";
}
@ApiOperation(value=\"⽂章评论查询接⼝\")
@PostMapping(\"/comments\")
@ResponseBody
publicDataGridResultcomments(CommentQueryquery){
//设置默认10
it(10);
mmentByArticleId(query);
}
@ApiOperation(value=\"⽂章点赞接⼝\")
@ApiImplicitParam(name=\"articleId\",value=\"⽂章ID\",required=true,dataType=\"Long\")
@PostMapping(\"/approve\")
@ResponseBody
publicYYBlogResultapprove(@RequestParamLongarticleId){
ApproveCntById(articleId);
}
}
最后同样来个效果图,和上⾯⼀样。
()的时候
()的时候
看到效果图是不是接⼝内容⼀⽬了然,很简洁明了了。
最后,好像忘记说swagger⽂档的路径了
更多推荐
swager是什么意思ger在线翻译读音例句
发布评论