diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/easyopen/EasyopenResultExecutor.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/easyopen/EasyopenResultExecutor.java index 2f1a2eac..d0ab701c 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/easyopen/EasyopenResultExecutor.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/easyopen/EasyopenResultExecutor.java @@ -1,19 +1,34 @@ package com.gitee.sop.gatewaycommon.easyopen; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.gitee.sop.gatewaycommon.message.Error; import com.gitee.sop.gatewaycommon.result.ApiResult; import com.gitee.sop.gatewaycommon.result.ResultExecutor; import com.gitee.sop.gatewaycommon.zuul.result.ZuulResultExecutor; import com.netflix.zuul.context.RequestContext; +import java.util.Optional; + /** * @author tanghc */ public class EasyopenResultExecutor implements ResultExecutor { + + boolean onlyReturnData; + + public EasyopenResultExecutor(boolean onlyReturnData) { + this.onlyReturnData = onlyReturnData; + } + @Override public String mergeResult(RequestContext request, String serviceResult) { - return serviceResult; + if (onlyReturnData) { + JSONObject jsonObject = JSON.parseObject(serviceResult); + return Optional.ofNullable(jsonObject.getString("data")).orElse("{}"); + } else { + return serviceResult; + } } @Override diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/easyopen/EasyopenZuulConfiguration.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/easyopen/EasyopenZuulConfiguration.java index 6475368c..5e531fe4 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/easyopen/EasyopenZuulConfiguration.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/easyopen/EasyopenZuulConfiguration.java @@ -10,15 +10,29 @@ import com.gitee.sop.gatewaycommon.zuul.configuration.BaseZuulConfiguration; */ public class EasyopenZuulConfiguration extends BaseZuulConfiguration { - static { - ParamNames.APP_KEY_NAME = "app_key"; - ParamNames.API_NAME = "name"; - ParamNames.SIGN_TYPE_NAME = "sign_type"; - ParamNames.APP_AUTH_TOKEN_NAME = "access_token"; + public EasyopenZuulConfiguration() { ApiConfig apiConfig = ApiContext.getApiConfig(); - apiConfig.setSigner(new EasyopenSigner()); - apiConfig.setZuulResultExecutor(new EasyopenResultExecutor()); - apiConfig.setMergeResult(false); + if (compatibilityModel()) { + ParamNames.APP_KEY_NAME = "app_key"; + ParamNames.API_NAME = "name"; + ParamNames.SIGN_TYPE_NAME = "sign_type"; + ParamNames.APP_AUTH_TOKEN_NAME = "access_token"; + apiConfig.setSigner(new EasyopenSigner()); + apiConfig.setZuulResultExecutor(new EasyopenResultExecutor(false)); + apiConfig.setMergeResult(false); + } else { + apiConfig.setZuulResultExecutor(new EasyopenResultExecutor(true)); + } } + /** + * 是否是兼容模式 + * @return 返回true,返回true可兼容之前的easyopen接口。 + */ + public boolean compatibilityModel() { + return true; + } + + + } diff --git a/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/api/Goods2Api.java b/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/api/Goods2Api.java new file mode 100644 index 00000000..b41676cf --- /dev/null +++ b/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/api/Goods2Api.java @@ -0,0 +1,31 @@ +package com.gitee.easyopen.server.api; + +import com.gitee.easyopen.annotation.Api; +import com.gitee.easyopen.annotation.ApiService; +import com.gitee.easyopen.doc.annotation.ApiDoc; +import com.gitee.easyopen.doc.annotation.ApiDocMethod; +import com.gitee.easyopen.server.api.param.GoodsParam; +import com.gitee.easyopen.server.api.result.Goods; + +import java.math.BigDecimal; + +/** + * 业务类 + * + * @author tanghc + */ +@ApiService +@ApiDoc("库存接口") +public class Goods2Api { + + @Api(name = "store.get") + @ApiDocMethod(description = "获取库存") + Goods getGoods(GoodsParam param) { + Goods goods = new Goods(); + goods.setId(1L); + goods.setGoods_name("苹果iPhoneX"); + goods.setPrice(new BigDecimal(8000)); + return goods; + } + +} diff --git a/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/api/GoodsApi.java b/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/api/GoodsApi.java index fab74c0c..f26168ed 100644 --- a/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/api/GoodsApi.java +++ b/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/api/GoodsApi.java @@ -15,7 +15,7 @@ import java.math.BigDecimal; * @author tanghc */ @ApiService -@ApiDoc("商品模块") +@ApiDoc("商品接口") public class GoodsApi { @Api(name = "goods.get") diff --git a/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/config/BaseSopDocController.java b/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/config/BaseSopDocController.java new file mode 100644 index 00000000..11a4a32e --- /dev/null +++ b/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/config/BaseSopDocController.java @@ -0,0 +1,28 @@ +package com.gitee.easyopen.server.config; + +import com.gitee.easyopen.doc.ApiDocHolder; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import java.util.HashMap; +import java.util.Map; + +public abstract class BaseSopDocController { + + public abstract String getDocTitle(); + + @RequestMapping("/v2/api-docs") + @ResponseBody + public Map getDocInfo() { + Map context = this.getContext(); + context.put("easyopen", "1.16.3"); + context.put("apiModules", ApiDocHolder.getApiDocBuilder().getApiModules()); + context.put("title", getDocTitle()); + return context; + } + + public Map getContext() { + return new HashMap<>(8); + } + +} \ No newline at end of file diff --git a/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/config/SopConfig.java b/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/config/SopConfig.java index 490e16f2..32213655 100644 --- a/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/config/SopConfig.java +++ b/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/config/SopConfig.java @@ -2,10 +2,19 @@ package com.gitee.easyopen.server.config; import com.gitee.sop.servercommon.configuration.EasyopenServiceConfiguration; import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Controller; /** * @author tanghc */ @Configuration public class SopConfig extends EasyopenServiceConfiguration { + + @Controller + public static class SopDocController extends BaseSopDocController { + @Override + public String getDocTitle() { + return "商品API"; + } + } } diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/controller/AlipayController.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/controller/AlipayController.java index 92b8a13e..2ff337d4 100644 --- a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/controller/AlipayController.java +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/controller/AlipayController.java @@ -3,6 +3,7 @@ package com.gitee.sop.bookweb.controller; import com.gitee.sop.bookweb.controller.param.StoryParam; import com.gitee.sop.servercommon.annotation.ApiMapping; import com.gitee.sop.story.api.domain.Story; +import io.swagger.annotations.Api; import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiOperation; import lombok.Data; @@ -16,6 +17,7 @@ import java.util.Date; * @author tanghc */ @RestController +@Api(tags = "故事接口") public class AlipayController { @ApiMapping(value = "alipay.story.get") diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/controller/Story2Controller.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/controller/Story2Controller.java index 0c058582..360c3447 100644 --- a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/controller/Story2Controller.java +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/controller/Story2Controller.java @@ -3,6 +3,7 @@ package com.gitee.sop.bookweb.controller; import com.gitee.sop.servercommon.annotation.ApiAbility; import com.gitee.sop.servercommon.annotation.ApiMapping; import com.gitee.sop.story.api.domain.Story; +import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -13,6 +14,7 @@ import org.springframework.web.bind.annotation.RestController; @ApiAbility // 放在这里,下面所有的接口都具备接口提供能力 @RestController @RequestMapping("story2") +@Api(tags = "故事接口2") public class Story2Controller{ @RequestMapping("getStory4") diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/config/ZuulConfig.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/config/ZuulConfig.java index 5c481ef6..e03909c8 100644 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/config/ZuulConfig.java +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/config/ZuulConfig.java @@ -1,28 +1,20 @@ package com.gitee.sop.gateway.config; -import com.gitee.sop.gateway.entity.IsvInfo; +/** + * 使用Spring Cloud Zuul,推荐使用 + * + * 注意:下面两个只能使用一个 + */ + import com.gitee.sop.gateway.manager.ManagerInitializer; -import com.gitee.sop.gatewaycommon.bean.ApiConfig; import com.gitee.sop.gatewaycommon.bean.ApiContext; -import com.gitee.sop.gatewaycommon.easyopen.EasyopenZuulConfiguration; -import com.gitee.sop.gatewaycommon.manager.IsvRoutePermissionManager; -import com.gitee.sop.gatewaycommon.secret.IsvManager; import com.gitee.sop.gatewaycommon.zuul.configuration.AlipayZuulConfiguration; -import com.gitee.sop.gatewaycommon.zuul.configuration.TaobaoZuulConfiguration; -import com.gitee.sop.gatewaycommon.zuul.filter.PreLimitFilter; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.HashMap; import java.util.Map; -/** - * 使用Spring Cloud Zuul,推荐使用 - * - * 注意:下面两个只能使用一个 - */ - /** * 开通支付宝开放平台能力 * @author tanghc diff --git a/sop-website/website-front/pages/doc/doc.js b/sop-website/website-front/pages/doc/doc.js index 50990652..17c85fd3 100644 --- a/sop-website/website-front/pages/doc/doc.js +++ b/sop-website/website-front/pages/doc/doc.js @@ -7,43 +7,53 @@ layui.use(['element', 'form'], function(){ //加载code模块 function initDocModules() { $.getJSON(SopConfig.url + '/doc/getDocBaseInfo', function (baseInfo) { var html = []; - var modules = baseInfo.docModuleVOList; - for (var i = 0; i < modules.length; i++) { - var docDefinition = modules[i]; - var module = docDefinition.module; + var docInfoList = baseInfo.docInfoList; + for (var i = 0; i < docInfoList.length; i++) { + var docInfo = docInfoList[i]; var selected = i === 0 ? 'selected="selected"' : ''; - html.push(''); + var title = docInfo.title; + html.push(''); } $('#moduleList').html(html.join('')); + $('.url-prod').text(baseInfo.urlProd); form.render('select'); - if (modules && modules.length > 0) { - selectModule(modules[0].module); + + if (docInfoList && docInfoList.length > 0) { + selectDocInfo(docInfoList[0].title); } - $('.url-prod').text(baseInfo.urlProd); }) } - function selectModule(docModule) { - $.getJSON(SopConfig.url + '/doc/module/' + docModule, function (module) { - var docItems = module.docItems; - var html = ['
  • ' + docModule + '

  • ']; - for (var i = 0; i < docItems.length; i++) { - var docItem = docItems[i]; - docItemStore[docItem.nameVersion] = docItem; - /* -
  • - - 统一收单交易退款查询 - -
  • - */ - var selectedClass = i === 0 ? 'layui-this' : ''; - html.push('
  • '); - html.push(''+docItem.summary+'') + function selectDocInfo(title) { + $.getJSON(SopConfig.url + '/doc/docinfo/' + title, function (docInfo) { + var moduleList = docInfo.docModuleList; + var html = []; + var firstItem; + for (var j = 0; j < moduleList.length; j++) { + var module = moduleList[j]; + var docItems = module.docItems; + html.push('
  • ' + module.module + '

  • '); + for (var i = 0; i < docItems.length; i++) { + var docItem = docItems[i]; + var first = j == 0 && j == 0; + if (first) { + firstItem = docItem; + } + docItemStore[docItem.nameVersion] = docItem; + /* +
  • + + 统一收单交易退款查询 + +
  • + */ + html.push('
  • '); + html.push(''+docItem.summary+'') + } } + $('#docItemTree').html(html.join('')); - if (docItems && docItems.length > 0) { - var firstItem = docItems[0]; + if (firstItem) { selectDocItem(firstItem.nameVersion); } }) @@ -51,12 +61,11 @@ layui.use(['element', 'form'], function(){ //加载code模块 function initEvent() { form.on('select(moduleListFilter)', function (data) { - selectModule(data.value); + selectDocInfo(data.value); }) - $('#docItemTree').on('click', 'a', function () { - var $tagA = $(this); - selectDocItem($tagA.attr('nameversion')); - $tagA.parent().addClass('layui-this').siblings().removeClass('layui-this'); + $('#docItemTree').on('click', 'li', function () { + var $li = $(this); + selectDocItem($li.attr('nameversion')); }) } @@ -70,6 +79,9 @@ layui.use(['element', 'form'], function(){ //加载code模块 createRequestParameter(docItem); createResponseParameter(docItem); createResponseCode(docItem); + + var $li = $('#docItemTree').find('li[nameversion="'+nameVersion+'"]'); + $li.addClass('layui-this').siblings().removeClass('layui-this'); } function createRequestParameter(docItem) { diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/DocInfo.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/DocInfo.java new file mode 100644 index 00000000..b48b93e5 --- /dev/null +++ b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/DocInfo.java @@ -0,0 +1,14 @@ +package com.gitee.sop.websiteserver.bean; + +import lombok.Data; + +import java.util.List; + +/** + * @author tanghc + */ +@Data +public class DocInfo { + private String title; + private List docModuleList; +} diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/DocItem.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/DocItem.java index 076fb2f0..431c9482 100644 --- a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/DocItem.java +++ b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/DocItem.java @@ -9,6 +9,7 @@ import java.util.List; */ @Data public class DocItem { + private String module; private String name; private String version; private String summary; diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/controller/DocController.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/controller/DocController.java index e12b6826..712d723c 100644 --- a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/controller/DocController.java +++ b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/controller/DocController.java @@ -1,10 +1,10 @@ package com.gitee.sop.websiteserver.controller; +import com.gitee.sop.websiteserver.bean.DocInfo; import com.gitee.sop.websiteserver.bean.DocItem; -import com.gitee.sop.websiteserver.bean.DocModule; import com.gitee.sop.websiteserver.manager.DocManager; import com.gitee.sop.websiteserver.vo.DocBaseInfoVO; -import com.gitee.sop.websiteserver.vo.DocModuleVO; +import com.gitee.sop.websiteserver.vo.DocInfoVO; import org.apache.commons.lang.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -38,11 +38,11 @@ public class DocController { @GetMapping("/getDocBaseInfo") public DocBaseInfoVO getDocBaseInfo() { - List docModuleVOList = docManager.listAll() + List docInfoList = docManager.listAll() .stream() - .map(docModule -> { - DocModuleVO vo = new DocModuleVO(); - BeanUtils.copyProperties(docModule, vo); + .map(docInfo -> { + DocInfoVO vo = new DocInfoVO(); + BeanUtils.copyProperties(docInfo, vo); return vo; }) .collect(Collectors.toList()); @@ -50,13 +50,13 @@ public class DocController { DocBaseInfoVO baseInfoVO = new DocBaseInfoVO(); baseInfoVO.setUrlTest(urlTest); baseInfoVO.setUrlProd(urlProd); - baseInfoVO.setDocModuleVOList(docModuleVOList); + baseInfoVO.setDocInfoList(docInfoList); return baseInfoVO; } - @GetMapping("/module/{module}") - public DocModule getDocModule(@PathVariable("module") String module) { - return docManager.getByTitle(module); + @GetMapping("/docinfo/{title}") + public DocInfo getDocModule(@PathVariable("title") String title) { + return docManager.getByTitle(title); } @GetMapping("/item/{method}/{version}/") diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocManager.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocManager.java index 08de87b2..b60eb46f 100644 --- a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocManager.java +++ b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocManager.java @@ -1,6 +1,6 @@ package com.gitee.sop.websiteserver.manager; -import com.gitee.sop.websiteserver.bean.DocModule; +import com.gitee.sop.websiteserver.bean.DocInfo; import com.gitee.sop.websiteserver.bean.DocItem; import java.util.Collection; @@ -14,7 +14,7 @@ public interface DocManager { DocItem get(String method, String version); - DocModule getByTitle(String title); + DocInfo getByTitle(String title); - Collection listAll(); + Collection listAll(); } diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocManagerImpl.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocManagerImpl.java index 57e83152..d829ca9e 100644 --- a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocManagerImpl.java +++ b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocManagerImpl.java @@ -1,11 +1,9 @@ package com.gitee.sop.websiteserver.manager; import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.gitee.sop.websiteserver.bean.DocInfo; import com.gitee.sop.websiteserver.bean.DocItem; -import com.gitee.sop.websiteserver.bean.DocModule; -import com.gitee.sop.websiteserver.bean.DocParameter; import com.gitee.sop.websiteserver.bean.EurekaApplication; import com.gitee.sop.websiteserver.bean.EurekaApps; import com.gitee.sop.websiteserver.bean.EurekaInstance; @@ -30,9 +28,6 @@ import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.function.Function; import java.util.stream.Collectors; /** @@ -42,8 +37,8 @@ import java.util.stream.Collectors; @Slf4j public class DocManagerImpl implements DocManager { - // key:module - Map docDefinitionMap = new HashMap<>(); + // key:title + Map docDefinitionMap = new HashMap<>(); // key: name+version Map docItemMap = new HashMap<>(); @@ -53,6 +48,9 @@ public class DocManagerImpl implements DocManager { RestTemplate restTemplate = new RestTemplate(); + DocParser swaggerDocParser = new SwaggerDocParser(); + DocParser easyopenDocParser = new EasyopenDocParser(); + @Autowired private Environment environment; @@ -68,97 +66,46 @@ public class DocManagerImpl implements DocManager { ServiceInfoVO serviceInfoVo = entry.getValue().get(0); loadDocInfo(serviceInfoVo); } - Map itemMap = docDefinitionMap.values() - .stream() - .map(DocModule::getDocItems) - .flatMap(docItems -> docItems.stream()) - .collect(Collectors.toMap(DocItem::getNameVersion, Function.identity())); - this.docItemMap.putAll(itemMap); +// Map itemMap = docDefinitionMap.values() +// .stream() +// .map(DocInfo::getDocModuleList) +// .map(list->{ +// for (DocModule docModule : list) { +// +// } +// }) +// .map(DocModule::getDocItems) +// .flatMap(docItems -> docItems.stream()) +// .collect(Collectors.toMap(DocItem::getNameVersion, Function.identity())); +// this.docItemMap.putAll(itemMap); } catch (IOException e) { log.error("加载失败", e); } } - private void loadDocInfo(ServiceInfoVO serviceInfoVo) { + protected void loadDocInfo(ServiceInfoVO serviceInfoVo) { String url = "http://" + serviceInfoVo.getIpAddr() + ":" + serviceInfoVo.getServerPort() + "/v2/api-docs"; try { ResponseEntity entity = restTemplate.getForEntity(url, String.class); String docInfoJson = entity.getBody(); - DocModule docDefinition = this.parseDocJson(docInfoJson); - docDefinitionMap.put(docDefinition.getModule(), docDefinition); + JSONObject docRoot = JSON.parseObject(docInfoJson); + DocParser docParser = this.buildDocParser(docRoot); + DocInfo docInfo = docParser.parseJson(docRoot); + docDefinitionMap.put(docInfo.getTitle(), docInfo); } catch (RestClientException e) { // 这里报错可能是因为有些微服务没有配置swagger文档,导致404访问不到 // 这里catch跳过即可 - log.warn("读取文档失败, url:{}", url, e); - } - } - - private DocModule parseDocJson(String docInfoJson) { - JSONObject docRoot = JSON.parseObject(docInfoJson); - String title = docRoot.getJSONObject("info").getString("title"); - List docItems = new ArrayList<>(); - - JSONObject paths = docRoot.getJSONObject("paths"); - Set pathNameSet = paths.keySet(); - for (String pathName : pathNameSet) { - JSONObject pathInfo = paths.getJSONObject(pathName); - Set pathSet = pathInfo.keySet(); - Optional first = pathSet.stream().findFirst(); - if (first.isPresent()) { - String path = first.get(); - JSONObject docInfo = pathInfo.getJSONObject(path); - DocItem docItem = buildDocItem(docInfo, docRoot); - docItems.add(docItem); - } + log.warn("读取文档失败, url:{}, msg:{}", url, e.getMessage()); } - - DocModule docDefinition = new DocModule(); - docDefinition.setModule(title); - docDefinition.setDocItems(docItems); - return docDefinition; } - private DocItem buildDocItem(JSONObject docInfo, JSONObject docRoot) { - DocItem docItem = new DocItem(); - docItem.setName(docInfo.getString("sop_name")); - docItem.setVersion(docInfo.getString("sop_version")); - docItem.setSummary(docInfo.getString("summary")); - docItem.setDescription(docInfo.getString("description")); - Optional parametersOptional = Optional.ofNullable(docInfo.getJSONArray("parameters")); - JSONArray parameters = parametersOptional.orElse(new JSONArray()); - List docParameterList = parameters.toJavaList(DocParameter.class); - docItem.setRequestParameters(docParameterList); - - List responseParameterList = this.buildResponseParameterList(docInfo, docRoot); - docItem.setResponseParameters(responseParameterList); - - return docItem; - } - - private List buildResponseParameterList(JSONObject docInfo, JSONObject docRoot) { - String responseRef = getResponseRef(docInfo); - List respParameterList = new ArrayList<>(); - if (StringUtils.isNotBlank(responseRef)) { - JSONObject responseObject = docRoot.getJSONObject("definitions").getJSONObject(responseRef); - JSONObject properties = responseObject.getJSONObject("properties"); - Set fieldNames = properties.keySet(); - for (String fieldName : fieldNames) { - JSONObject fieldInfo = properties.getJSONObject(fieldName); - DocParameter respParam = fieldInfo.toJavaObject(DocParameter.class); - respParam.setName(fieldName); - respParameterList.add(respParam); - } + protected DocParser buildDocParser(JSONObject rootDoc) { + Object easyopen = rootDoc.get("easyopen"); + if (easyopen != null) { + return easyopenDocParser; + } else { + return swaggerDocParser; } - return respParameterList; - } - - private String getResponseRef(JSONObject docInfo) { - String ref = Optional.ofNullable(docInfo.getJSONObject("responses")) - .flatMap(jsonObject -> Optional.of(jsonObject.getJSONObject("200"))) - .flatMap(jsonObject -> Optional.of(jsonObject.getJSONObject("schema"))) - .flatMap(jsonObject -> Optional.of(jsonObject.getString("originalRef"))) - .orElse(""); - return ref; } @Override @@ -167,12 +114,12 @@ public class DocManagerImpl implements DocManager { } @Override - public DocModule getByTitle(String title) { + public DocInfo getByTitle(String title) { return docDefinitionMap.get(title); } @Override - public Collection listAll() { + public Collection listAll() { return docDefinitionMap.values(); } @@ -200,7 +147,7 @@ public class DocManagerImpl implements DocManager { return listMap; } - private String requestEurekaServer(EurekaUri eurekaUri, String... args) throws IOException { + protected String requestEurekaServer(EurekaUri eurekaUri, String... args) throws IOException { Request request = eurekaUri.getRequest(this.eurekaUrl, args); Response response = client.newCall(request).execute(); if (response.isSuccessful()) { diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocParser.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocParser.java new file mode 100644 index 00000000..70a4c3f7 --- /dev/null +++ b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocParser.java @@ -0,0 +1,11 @@ +package com.gitee.sop.websiteserver.manager; + +import com.alibaba.fastjson.JSONObject; +import com.gitee.sop.websiteserver.bean.DocInfo; + +/** + * @author tanghc + */ +public interface DocParser { + DocInfo parseJson(JSONObject docRoot); +} diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/EasyopenDocParser.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/EasyopenDocParser.java new file mode 100644 index 00000000..7745a603 --- /dev/null +++ b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/EasyopenDocParser.java @@ -0,0 +1,83 @@ +package com.gitee.sop.websiteserver.manager; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.gitee.sop.websiteserver.bean.DocInfo; +import com.gitee.sop.websiteserver.bean.DocItem; +import com.gitee.sop.websiteserver.bean.DocModule; +import com.gitee.sop.websiteserver.bean.DocParameter; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +/** + * @author tanghc + */ +public class EasyopenDocParser implements DocParser { + @Override + public DocInfo parseJson(JSONObject docRoot) { + String title = docRoot.getString("title"); + List docItems = new ArrayList<>(); + JSONArray apiModules = docRoot.getJSONArray("apiModules"); + for (int i = 0; i < apiModules.size(); i++) { + JSONObject module = apiModules.getJSONObject(i); + JSONArray moduleItems = module.getJSONArray("moduleItems"); + for (int k = 0; k < moduleItems.size(); k++) { + JSONObject docInfo = moduleItems.getJSONObject(k); + DocItem docItem = buildDocItem(docInfo); + docItem.setModule(module.getString("name")); + docItems.add(docItem); + } + } + + List docModuleList = docItems.stream() + .collect(Collectors.groupingBy(DocItem::getModule)) + .entrySet() + .stream() + .map(entry -> { + DocModule docModule = new DocModule(); + docModule.setModule(entry.getKey()); + docModule.setDocItems(entry.getValue()); + return docModule; + }) + .collect(Collectors.toList()); + + DocInfo docInfo = new DocInfo(); + docInfo.setTitle(title); + docInfo.setDocModuleList(docModuleList); + return docInfo; + } + + protected DocItem buildDocItem(JSONObject docInfo) { + DocItem docItem = new DocItem(); + docItem.setName(docInfo.getString("name")); + docItem.setVersion(docInfo.getString("version")); + docItem.setSummary(docInfo.getString("description")); + docItem.setDescription(docInfo.getString("description")); + List docParameterList = this.buildParameterList(docInfo, "paramDefinitions"); + docItem.setRequestParameters(docParameterList); + + List responseParameterList = this.buildParameterList(docInfo, "resultDefinitions"); + docItem.setResponseParameters(responseParameterList); + + return docItem; + } + + protected List buildParameterList(JSONObject docInfo, String key) { + JSONArray params = docInfo.getJSONArray(key); + if (params == null) { + return Collections.emptyList(); + } + List docParameterList = new ArrayList<>(); + for (int i = 0; i < params.size(); i++) { + JSONObject jsonObject = params.getJSONObject(i); + DocParameter docParameter = jsonObject.toJavaObject(DocParameter.class); + docParameter.setType(jsonObject.getString("dataType")); + docParameterList.add(docParameter); + } + return docParameterList; + } + +} diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/SwaggerDocParser.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/SwaggerDocParser.java new file mode 100644 index 00000000..0bba3cab --- /dev/null +++ b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/SwaggerDocParser.java @@ -0,0 +1,112 @@ +package com.gitee.sop.websiteserver.manager; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.gitee.sop.websiteserver.bean.DocInfo; +import com.gitee.sop.websiteserver.bean.DocItem; +import com.gitee.sop.websiteserver.bean.DocModule; +import com.gitee.sop.websiteserver.bean.DocParameter; +import org.apache.commons.lang.StringUtils; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * @author tanghc + */ +public class SwaggerDocParser implements DocParser { + @Override + public DocInfo parseJson(JSONObject docRoot) { + String title = docRoot.getJSONObject("info").getString("title"); + List docItems = new ArrayList<>(); + + JSONObject paths = docRoot.getJSONObject("paths"); + Set pathNameSet = paths.keySet(); + for (String pathName : pathNameSet) { + JSONObject pathInfo = paths.getJSONObject(pathName); + Set pathSet = pathInfo.keySet(); + Optional first = pathSet.stream().findFirst(); + if (first.isPresent()) { + String path = first.get(); + JSONObject docInfo = pathInfo.getJSONObject(path); + DocItem docItem = buildDocItem(docInfo, docRoot); + docItems.add(docItem); + } + } + + List docModuleList = docItems.stream() + .collect(Collectors.groupingBy(DocItem::getModule)) + .entrySet() + .stream() + .map(entry -> { + DocModule docModule = new DocModule(); + docModule.setModule(entry.getKey()); + docModule.setDocItems(entry.getValue()); + return docModule; + }) + .collect(Collectors.toList()); + + + DocInfo docInfo = new DocInfo(); + docInfo.setTitle(title); + docInfo.setDocModuleList(docModuleList); + return docInfo; + } + + protected DocItem buildDocItem(JSONObject docInfo, JSONObject docRoot) { + DocItem docItem = new DocItem(); + docItem.setName(docInfo.getString("sop_name")); + docItem.setVersion(docInfo.getString("sop_version")); + docItem.setSummary(docInfo.getString("summary")); + docItem.setDescription(docInfo.getString("description")); + String moduleName = this.buildModuleName(docInfo, docRoot); + docItem.setModule(moduleName); + Optional parametersOptional = Optional.ofNullable(docInfo.getJSONArray("parameters")); + JSONArray parameters = parametersOptional.orElse(new JSONArray()); + List docParameterList = parameters.toJavaList(DocParameter.class); + docItem.setRequestParameters(docParameterList); + + List responseParameterList = this.buildResponseParameterList(docInfo, docRoot); + docItem.setResponseParameters(responseParameterList); + + return docItem; + } + + protected String buildModuleName(JSONObject docInfo, JSONObject docRoot) { + String title = docRoot.getJSONObject("info").getString("title"); + JSONArray tags = docInfo.getJSONArray("tags"); + if (tags != null && tags.size() > 0) { + return tags.getString(0); + } + return title; + } + + protected List buildResponseParameterList(JSONObject docInfo, JSONObject docRoot) { + String responseRef = getResponseRef(docInfo); + List respParameterList = new ArrayList<>(); + if (StringUtils.isNotBlank(responseRef)) { + JSONObject responseObject = docRoot.getJSONObject("definitions").getJSONObject(responseRef); + JSONObject properties = responseObject.getJSONObject("properties"); + Set fieldNames = properties.keySet(); + for (String fieldName : fieldNames) { + JSONObject fieldInfo = properties.getJSONObject(fieldName); + DocParameter respParam = fieldInfo.toJavaObject(DocParameter.class); + respParam.setName(fieldName); + respParameterList.add(respParam); + } + } + return respParameterList; + } + + protected String getResponseRef(JSONObject docInfo) { + String ref = Optional.ofNullable(docInfo.getJSONObject("responses")) + .flatMap(jsonObject -> Optional.of(jsonObject.getJSONObject("200"))) + .flatMap(jsonObject -> Optional.of(jsonObject.getJSONObject("schema"))) + .flatMap(jsonObject -> Optional.of(jsonObject.getString("originalRef"))) + .orElse(""); + return ref; + } +} diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/vo/DocBaseInfoVO.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/vo/DocBaseInfoVO.java index 299d35de..ac893f7d 100644 --- a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/vo/DocBaseInfoVO.java +++ b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/vo/DocBaseInfoVO.java @@ -11,5 +11,5 @@ import java.util.List; public class DocBaseInfoVO { private String urlTest; private String urlProd; - private List docModuleVOList; + private List docInfoList; } diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/vo/DocModuleVO.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/vo/DocInfoVO.java similarity index 64% rename from sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/vo/DocModuleVO.java rename to sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/vo/DocInfoVO.java index 2cdc6271..307a02b4 100644 --- a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/vo/DocModuleVO.java +++ b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/vo/DocInfoVO.java @@ -6,6 +6,6 @@ import lombok.Data; * @author tanghc */ @Data -public class DocModuleVO { - private String module; +public class DocInfoVO { + private String title; }