pull/1/head
tanghc 6 years ago
parent 0966784976
commit 6408f0e7d2
  1. 19
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java
  2. 4
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/vo/FileUploadVO.java
  3. 138
      sop-website/src/main/java/com/gitee/sop/websiteserver/manager/SwaggerDocParser.java

@ -17,7 +17,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List;
/** /**
* 支付宝服务端假设签名验证通过后到达这里进行具体的业务处理 * 支付宝服务端假设签名验证通过后到达这里进行具体的业务处理
@ -180,6 +182,23 @@ public class AlipayController {
return storyVO; return storyVO;
} }
@ApiOperation(value = "返回数组结果", notes = "返回数组结果")
@ApiMapping(value = "alipay.story.find2")
public List<StoryVO> getStory3(StoryParam story) {
int index = 0;
StoryVO storyVO = new StoryVO();
storyVO.id = 1L;
storyVO.name = "白雪公主, index:" + index++;
storyVO.gmt_create = new Date();
StoryVO storyVO2 = new StoryVO();
storyVO2.id = 1L;
storyVO2.name = "白雪公主, index:" + index++;
storyVO2.gmt_create = new Date();
return Arrays.asList(storyVO, storyVO2);
}
/** /**
* 演示文档表格树 * 演示文档表格树
* @param story * @param story

@ -1,5 +1,6 @@
package com.gitee.sop.storyweb.vo; package com.gitee.sop.storyweb.vo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import java.util.ArrayList; import java.util.ArrayList;
@ -25,8 +26,11 @@ public class FileUploadVO {
public FileMeta() { public FileMeta() {
} }
@ApiModelProperty(value = "文件名称", example = "1.txt")
private String filename; private String filename;
@ApiModelProperty(value = "文件大小", example = "109")
private long size; private long size;
@ApiModelProperty(value = "文件内容", example = "啊啊啊")
private String content; private String content;
} }
} }

@ -8,7 +8,6 @@ import com.gitee.sop.websiteserver.bean.DocModule;
import com.gitee.sop.websiteserver.bean.DocParameter; import com.gitee.sop.websiteserver.bean.DocParameter;
import com.gitee.sop.websiteserver.bean.DocParserContext; import com.gitee.sop.websiteserver.bean.DocParserContext;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import java.util.ArrayList; import java.util.ArrayList;
@ -138,7 +137,7 @@ public class SwaggerDocParser implements DocParser {
}) })
.collect(Collectors.groupingBy(DocParameter::getModule)); .collect(Collectors.groupingBy(DocParameter::getModule));
collect.entrySet().stream() collect.entrySet()
.forEach(entry -> { .forEach(entry -> {
DocParameter moduleDoc = new DocParameter(); DocParameter moduleDoc = new DocParameter();
moduleDoc.setName(entry.getKey()); moduleDoc.setName(entry.getKey());
@ -147,18 +146,25 @@ public class SwaggerDocParser implements DocParser {
docParameterList.add(moduleDoc); docParameterList.add(moduleDoc);
}); });
List<DocParameter> ret = docParameterList.stream() return docParameterList.stream()
.filter(docParameter -> !docParameter.getName().contains(".")) .filter(docParameter -> !docParameter.getName().contains("."))
.collect(Collectors.toList()); .collect(Collectors.toList());
return ret;
} }
protected List<DocParameter> buildResponseParameterList(JSONObject docInfo, JSONObject docRoot) { protected List<DocParameter> buildResponseParameterList(JSONObject docInfo, JSONObject docRoot) {
String responseRef = getResponseRef(docInfo); RefInfo refInfo = getResponseRefInfo(docInfo);
List<DocParameter> respParameterList = Collections.emptyList(); List<DocParameter> respParameterList = Collections.emptyList();
if (StringUtils.isNotBlank(responseRef)) { if (refInfo != null) {
String responseRef = refInfo.ref;
respParameterList = this.buildDocParameters(responseRef, docRoot); respParameterList = this.buildDocParameters(responseRef, docRoot);
// 如果返回数组
if (refInfo.isArray) {
DocParameter docParameter = new DocParameter();
docParameter.setName("items");
docParameter.setType("array");
docParameter.setRefs(respParameterList);
respParameterList = Collections.singletonList(docParameter);
}
} }
return respParameterList; return respParameterList;
} }
@ -170,52 +176,104 @@ public class SwaggerDocParser implements DocParser {
List<DocParameter> docParameterList = new ArrayList<>(); List<DocParameter> docParameterList = new ArrayList<>();
for (String fieldName : fieldNames) { for (String fieldName : fieldNames) {
/* /*
{ {
"description": "分类故事", "description": "分类故事",
"$ref": "#/definitions/StoryVO", "$ref": "#/definitions/StoryVO"
"originalRef": "StoryVO"
} }
*/ */
JSONObject fieldInfo = properties.getJSONObject(fieldName); JSONObject fieldInfo = properties.getJSONObject(fieldName);
DocParameter respParam = fieldInfo.toJavaObject(DocParameter.class); DocParameter docParameter = fieldInfo.toJavaObject(DocParameter.class);
respParam.setName(fieldName); docParameter.setName(fieldName);
docParameterList.add(respParam); docParameterList.add(docParameter);
String originalRef = isArray(fieldInfo) ? getRef(fieldInfo.getJSONObject(fieldName)) : getRef(fieldInfo); RefInfo refInfo = this.getRefInfo(fieldInfo);
if (StringUtils.isNotBlank(originalRef)) { if (refInfo != null) {
List<DocParameter> refs = buildDocParameters(originalRef, docRoot); List<DocParameter> refs = buildDocParameters(refInfo.ref, docRoot);
respParam.setRefs(refs); docParameter.setRefs(refs);
} }
} }
return docParameterList; return docParameterList;
} }
protected boolean isArray(JSONObject fieldInfo) {
return "array".equalsIgnoreCase(fieldInfo.getString("type"));
}
private String getRef(JSONObject fieldInfo) {
return Optional.ofNullable(fieldInfo)
.map(jsonObject -> jsonObject.getString("originalRef"))
.orElse(null);
}
protected String getResponseRef(JSONObject docInfo) { /**
* 简单对象返回
* "responses": {
* "200": {
* "description": "OK",
* "schema": {
* "$ref": "#/definitions/FileUploadVO"
* }
* },
* "401": {
* "description": "Unauthorized"
* },
* "403": {
* "description": "Forbidden"
* },
* "404": {
* "description": "Not Found"
* }
* }
* 纯数组返回
* "responses": {
* "200": {
* "description": "OK",
* "schema": {
* "type": "array",
* "items": {
* "$ref": "#/definitions/StoryVO"
* }
* }
* },
* "401": {
* "description": "Unauthorized"
* },
* "403": {
* "description": "Forbidden"
* },
* "404": {
* "description": "Not Found"
* }
* }
* @param docInfo
* @return
*/
protected RefInfo getResponseRefInfo(JSONObject docInfo) {
return Optional.ofNullable(docInfo.getJSONObject("responses")) return Optional.ofNullable(docInfo.getJSONObject("responses"))
.flatMap(jsonObject -> Optional.ofNullable(jsonObject.getJSONObject("200"))) .flatMap(jsonObject -> Optional.ofNullable(jsonObject.getJSONObject("200")))
.flatMap(jsonObject -> Optional.ofNullable(jsonObject.getJSONObject("schema"))) .flatMap(jsonObject -> Optional.ofNullable(jsonObject.getJSONObject("schema")))
.flatMap(jsonObject -> { .flatMap(schema -> {
// #/definitions/Category RefInfo refInfo = getRefInfo(schema);
String $ref = jsonObject.getString("$ref"); return Optional.ofNullable(refInfo);
if ($ref == null) {
return Optional.empty();
}
int index = $ref.lastIndexOf("/");
if (index > -1) {
$ref = $ref.substring(index + 1);
}
return Optional.of($ref);
}) })
.orElse(""); .orElse(null);
}
private RefInfo getRefInfo(JSONObject jsonObject) {
String $ref;
boolean isArray = "array".equals(jsonObject.getString("type"));
if (isArray) {
$ref = jsonObject.getJSONObject("items").getString("$ref");
} else {
// #/definitions/Category
$ref = jsonObject.getString("$ref");
}
if ($ref == null) {
return null;
}
int index = $ref.lastIndexOf("/");
if (index > -1) {
$ref = $ref.substring(index + 1);
}
RefInfo refInfo = new RefInfo();
refInfo.isArray = isArray;
refInfo.ref = $ref;
return refInfo;
}
private static class RefInfo {
private boolean isArray;
private String ref;
} }
} }

Loading…
Cancel
Save