package com.keepsoft.devbase.iot.web.config; import cn.hutool.json.JSONArray; import cn.hutool.json.JSONObject; import com.alibaba.fastjson.JSON; import com.fasterxml.classmate.TypeResolver; import com.google.common.collect.Sets; import com.keepsoft.devbase.db.DB; import com.keepsoft.devbase.db.model.Action; import com.keepsoft.devbase.iot.web.model.ActionSelf; import com.keepsoft.devbase.iot.web.model.Actions; import com.keepsoft.devbase.iot.web.model.AuthModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import springfox.documentation.builders.OperationBuilder; import springfox.documentation.builders.ParameterBuilder; import springfox.documentation.schema.ModelRef; import springfox.documentation.service.ApiDescription; import springfox.documentation.service.Operation; import springfox.documentation.service.Parameter; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.service.ApiListingScannerPlugin; import springfox.documentation.spi.service.contexts.DocumentationContext; import springfox.documentation.spring.web.readers.operation.CachingOperationNameGenerator; import java.util.ArrayList; import java.util.List; import java.util.Map; @Component public class SwaggerAddtion implements ApiListingScannerPlugin { @Autowired private DB db; @Override public List<ApiDescription> apply(DocumentationContext documentationContext) { Action action = new Action("bus", "LOAD", "LOAD_SYS_SQLCONFIG", "result"); ArrayList<Map> m = (ArrayList) db.doAction(action).get("result"); System.out.println("LOAD_SYS_SQLCONFIG" + m.toString()); ArrayList<ApiDescription> apiDescriptions = new ArrayList<>(); for (Map m1 : m) { String remark = String.valueOf(m1.get("remark")); String code = String.valueOf(m1.get("code")); if (!(code.indexOf("LOAD") != -1)) { continue; } List<Operation> operationBuilders = new ArrayList<>(); List<Parameter> parameterBuilders = new ArrayList<>(); Parameter parameterBuilder = new ParameterBuilder() .description("通用ajax 参数")//参数描述 .type(new TypeResolver().resolve(Actions.class))//参数数据类型 .name("ACTION")//参数名称 // .defaultValue(param)//参数默认值 .parameterType("body")//参数类型 query .required(true)//是否必填 .modelRef(new ModelRef("Actions")) //参数数据类型 .build(); parameterBuilders.add(parameterBuilder); Operation operationBuilder = new OperationBuilder( new CachingOperationNameGenerator()) .method(HttpMethod.POST)//http请求类型 .produces(Sets.newHashSet(MediaType.ALL_VALUE)).consumes(Sets.newHashSet(MediaType.APPLICATION_JSON_VALUE)) .summary(remark) // 文档 方法描述 .notes(remark)//方法描述(文档接口描述) .tags(Sets.newHashSet("通用ajax")).codegenMethodNameStem(remark)//归类标签 .parameters(parameterBuilders).build(); operationBuilders.add(operationBuilder); ApiDescription apiDescription = new ApiDescription( "/api/system/action" +"/"+code, //url "通用ajax ApiDescription", //描述 operationBuilders, false); apiDescriptions.add(apiDescription); } return apiDescriptions; } @Override public boolean supports(DocumentationType documentationType) { // return DocumentationType.SWAGGER_2.equals(documentationType); return true; } }
swagger结合ACtion 展示api
最新推荐文章于 2024-07-19 08:42:46 发布