springboot 中加入swagger 打包 运行打开网页
本人试验过程 没有遇到什么障碍,非常简单,好用,值得玩耍一下下。
swagger 超简单入门教程 使用(真实自测) - xmind果果 - CSDN博客--这篇文章非常好,收集了很多文章
https://blog.csdn.net/mengxiangxingdong/article/details/82743687
swagger常用注解说明 - 简书
https://www.jianshu.com/p/12f4394462d5
SpringBoot 使用Swagger2打造在线接口文档(附汉化教程) - 简书
https://www.jianshu.com/p/7e543f0f0bd8
全局参数_w3cschool
https://www.w3cschool.cn/swaggerbootstrapui/swaggerbootstrapui-i8nc32i7.html
Swagger常用注解使用详解
http://c.biancheng.net/view/5533.html
//==============
pom.xml 中加入
<swagger.version>2.7.0</swagger.version>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
/** 配置文件 */ package com.eos.wool.config; import io.swagger.annotations.ApiOperation; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.ApiKey; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.List; import static com.google.common.collect.Lists.newArrayList; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //加了ApiOperation注解的类,才生成接口文档 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //包下的类,才生成接口文档 .apis(RequestHandlerSelectors.basePackage("com.eos.wool.Controller")) .paths(PathSelectors.any()) .build() .securitySchemes(security()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("eos简易工具") .description("-api文档") .termsOfServiceUrl(" ") .version("0.0.2") .build(); } private List<ApiKey> security() { return newArrayList( new ApiKey("token", "token", "header") ); } }
写个Controller ,标注好,打包,运行,打开地址 就可以看到了。什么地址忘记了,
/** * */ package com.eos.wool.Controller;
import com.eos.wool.service.EosService; import io.bigbearbro.eos4j.Eos4j; import io.bigbearbro.eos4j.api.result.GetAccountResults; import io.bigbearbro.eos4j.api.result.PushTransactionResults; import io.bigbearbro.eos4j.crypto.EccTool; import io.bigbearbro.eos4j.entity.EosAccount; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang.RandomStringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.*; /** * @author rogge * * 2018年8月1日 上午9:54:57 */ @RestController @RequestMapping("eos") @Api(value = "相关业务的接口",tags="批量接口") public class eosControl { @Resource private EosService eosService; @Value("${eos.mainAccount.name}") private String mainAccountname; @Value("${eos.mainAccount.pk}") private String mainAccountPk; @Value("${eos.baseurl}") private String baseurl; public static List<String> inviteCodelist = new ArrayList<String>(); //public static Set<String> idlist = new HashSet<String>(); public static Map<String, String> VidKeyMmap = new HashMap<String, String>(); private SimpleDateFormat dFmt= new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); @ResponseBody @RequestMapping(value ="invite/{key}",method = RequestMethod.POST) @ApiOperation("获得邀请码") public String getinviteCode(@PathVariable("key") String mykey) { String vid="",key="",pk=""; if (mykey.equals("1234567")) { vid= RandomStringUtils.randomAlphanumeric(10); if (inviteCodelist.contains(vid)){ vid=""; }else inviteCodelist.add(vid); } return vid; } @ResponseBody @RequestMapping(value ="initVid",method = RequestMethod.POST) @ApiOperation(value ="获得授权码和公钥",notes="获得授权码和公钥") @ApiImplicitParams({ @ApiImplicitParam(name = "inviteCode", value = "邀请码", required = true, dataType = "String", paramType = "query")}) public Object getLastTrack(@RequestParam(value = "inviteCode", required = true) String inviteCode) { String vid="",key="",pk=""; //System.out.println(mykey); if (inviteCodelist.contains(inviteCode.trim())) { //System.out.println(mykey+"12345"); vid= RandomStringUtils.randomAlphanumeric(15); pk = EccTool.privateKeyFromSeed("!@#$%^&*(lajdlkjaksjdlkjaskldM<>?87126162kajsdjlaksd kajdlkaslkd heiuheijpe f[a- si0ausd9asd ahsdvcyasdcasdc ajhsdg8ca" + RandomStringUtils.randomAlphanumeric(13) + "we asds JHDKAHDKKASDKJALSKDKA ooidjajsdua09sid0asdo[paksdajsdlklasdmlk FJKLIKNLK;B/;LP[P'NC;PO'; OOPO;L0[" + RandomStringUtils.randomAlphanumeric(40) + "XP'C'[FG[" + RandomStringUtils.randomAlphanumeric(56) + "19218728909107328972309289832098012"); key= EccTool.privateToPublic2(pk); VidKeyMmap.put(vid,pk); inviteCodelist.remove(inviteCode.trim());//清除 可以设定为3天内有效 Map<String, String> map = new HashMap<String, String>(); map.put("授权码",vid); map.put("授权Active到此公钥",key); return map; } else { return inviteCode+"邀请码无效"; } } @ResponseBody @ApiOperation(value ="批量给账号买内存", notes="addFlag为true时直接增量购买buyRAMBytes的量,为false时是内存可使用值补足到buyRAMBytes的量") @ApiImplicitParams({ @ApiImplicitParam(name = "vid", value = "授权码", required = true, dataType = "String", paramType = "query"), @ApiImplicitParam(name = "accountName", value = "已经绑定的用户名", required = true, dataType = "String", paramType = "query"), @ApiImplicitParam(name = "buyRAMBytes", value = "增量或补足的量 (byte)", required = true, dataType = "Integer", paramType = "query"), @ApiImplicitParam(name = "addFlag", value = "增量标志", required = true, dataType = "boolean", paramType = "query"), @ApiImplicitParam(name = "accountList", value = "账号列表,中间用英文逗号隔开", required = true, dataType = "String", paramType = "query") }) @RequestMapping(value = "/buyRAMBytes", method = RequestMethod.POST) public Object batchBuyRAMBytes(@RequestParam(value = "vid", required = true) String vid, @RequestParam(value = "accountName", required = true) String accountName, @RequestParam(value = "buyRAMBytes", required = true) Integer buyRAMBytes, @RequestParam(value = "addFlag", required = true) boolean addFlag, @RequestParam(value = "accountList", required = true) String accountList) throws IOException{ List<String> list = new ArrayList<String>(); String PrivateKEY=""; accountName=accountName.trim(); System.out.println(vid+" "+accountName+" "+buyRAMBytes+" "+accountList); if (!VidKeyMmap.keySet().contains(vid.trim())) { list.add("此授权码不存在"); return list; } else{ PrivateKEY=VidKeyMmap.get(vid); } String[] eosAccounts=accountList.split(","); //购买字节 Long buyRam = 400l; Eos4j eos4j = new Eos4j(baseurl); int i = 0; int bucong=0; for (String eosAccount : eosAccounts) { if (eosAccount.trim().isEmpty()) { continue; } i++; if (addFlag == true) { GetAccountResults info=eos4j.getAccount(eosAccount); int ramshenyu= (int) (info.getRamQuota()-info.getRamUsage()); if (ramshenyu >=buyRAMBytes.intValue()) { //System.out.println("超出了"); list.add("成功 "+i+":"+eosAccount+":超值不用买"); continue; } bucong=buyRAMBytes.intValue()-ramshenyu+5; } else { bucong=buyRAMBytes.intValue()+5; } buyRam=(long)bucong; PushTransactionResults Results3 = null; try { Results3 = eos4j.buyrambytes(PrivateKEY, accountName, eosAccount, buyRam); if (Results3 != null && Results3.getTransactionId() != "") { list.add("成功 "+i+":"+eosAccount+":购买了"+bucong); System.out.println( "表 买内存成功,账号:" + eosAccount + "\t" + i+ "\t"+bucong); } } catch (Exception e) { e.printStackTrace(); list.add("失败 "+i+":"+eosAccount); } } System.out.println("执行完毕"); list.add("执行完成!"); return list; } }
2022年11月13日 又想起来了,看了蛮久不知道怎么打开,运行日志看不出来,看到这个静态文件 就想起来了。
http://localhost:8887/swagger-ui.html
![](https://img2022.cnblogs.com/blog/652963/202211/652963-20221113182001354-1106604579.png)