日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Bladex生成Swagger的方法

發布時間:2023/12/3 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Bladex生成Swagger的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、在啟動類中添加如下代碼:(目的是為了打印輸出swagger的地址等)
注解:@Slf4j
實現接口:CommandLineRunner
依賴注入:

@Autowiredprivate Environment environment; @Overridepublic void run(String... strings) throws Exception {try {String port = Optional.ofNullable(environment.getProperty("server.port")).orElse("8080");log.info("\n------------------------環境信息---------------------------\n\t" +"Application '{}' is running! Access URLs:\n\t" +"Local : \thttp://{}:{}\n\t" +"Swagger: \thttp://{}:{}/doc.html\n\t" +"Profile(s): \t{}\n----------------------------------------------------------",environment.getProperty("spring.application.name"),InetAddress.getLocalHost().getHostAddress(),port,InetAddress.getLocalHost().getHostAddress(),port,Arrays.toString(environment.getActiveProfiles()));} catch (UnknownHostException e) {e.printStackTrace();}}

啟動類全部代碼如下:

/** Copyright (c) 2018-2028, Chill Zhuang All rights reserved.** Redistribution and use in source and binary forms, with or without* modification, are permitted provided that the following conditions are met:** Redistributions of source code must retain the above copyright notice,* this list of conditions and the following disclaimer.* Redistributions in binary form must reproduce the above copyright* notice, this list of conditions and the following disclaimer in the* documentation and/or other materials provided with the distribution.* Neither the name of the dreamlu.net developer nor the names of its* contributors may be used to endorse or promote products derived from* this software without specific prior written permission.* Author: Chill 莊騫 (smallchill@163.com)*/ package org.springblade;import lombok.extern.slf4j.Slf4j; import org.springblade.common.constant.CommonConstant; import org.springblade.core.launch.BladeApplication; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.core.env.Environment; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.web.client.RestTemplate;import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; import java.util.Optional;/*** 啟動器** @author Chill*/ @Slf4j @EnableScheduling @SpringBootApplication public class Application implements CommandLineRunner {@Autowiredprivate Environment environment;@Beanpublic RestTemplate restTemplate(RestTemplateBuilder builder){return builder.build();}public static void main(String[] args) {BladeApplication.run(CommonConstant.APPLICATION_NAME, Application.class, args);}@Overridepublic void run(String... strings) throws Exception {try {String port = Optional.ofNullable(environment.getProperty("server.port")).orElse("8080");log.info("\n------------------------環境信息---------------------------\n\t" +"Application '{}' is running! Access URLs:\n\t" +"Local : \thttp://{}:{}\n\t" +"Swagger: \thttp://{}:{}/doc.html\n\t" +"Profile(s): \t{}\n----------------------------------------------------------",environment.getProperty("spring.application.name"),InetAddress.getLocalHost().getHostAddress(),port,InetAddress.getLocalHost().getHostAddress(),port,Arrays.toString(environment.getActiveProfiles()));} catch (UnknownHostException e) {e.printStackTrace();}}}

二、找到需要生成Swagger接口文檔的控制器類(eg:ActiveCodeController):
1.在控制器類的上面添加注解:
@Api(value = "卡信息", tags = "卡信息接口")

value里面寫的是:這個控制器的描述,或者功能

如圖所示:

2.在需要生成文檔的方法上寫注解:
@ApiOperation(value = "詳情", notes = "傳入cardInfo")
value:接口的功能或者描述
notes:傳入參數的描述
全部代碼:

/*** 詳情*/@GetMapping("/detail")@ApiOperationSupport(order = 1)@ApiOperation(value = "詳情", notes = "傳入cardInfo")public R<CardInfo> detail(CardInfo cardInfo) {CardInfo detail = cardInfoService.getOne(Condition.getQueryWrapper(cardInfo));return R.data(detail);}

或者:

/*** 分頁 卡信息*/@GetMapping("/list")@ApiOperationSupport(order = 2)@ApiOperation(value = "分頁", notes = "傳入cardInfo")public R<IPage<CardInfo>> list(CardInfo cardInfo, Query query) {IPage<CardInfo> pages = cardInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(cardInfo));return R.data(pages);}

其他的可以自己補充。
三、配置Swagger的配置類,類路徑:BladeX-Boot/src/main/java/org/springblade/common/config/SwaggerConfiguration.java

加入如下代碼:

@Beanpublic Docket developerDocket() {return docket("開發者中心接口", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.developer"));}

【開發者接口】:就是Swagger的接口功能的名稱
【.modules.developer】:讓Swagger掃描哪個包,指向包就可以。

然后啟動服務,啟動完成之后,查看控制臺打印的Swagger地址。

點擊地址就可以查看

總結

以上是生活随笔為你收集整理的Bladex生成Swagger的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。