生成postman json格式完成 待测试
This commit is contained in:
parent
bc3a927ed2
commit
2333418992
|
@ -1,8 +1,147 @@
|
|||
package com.power.doc.builder;
|
||||
|
||||
|
||||
import com.power.doc.constants.DocGlobalConstants;
|
||||
import com.power.doc.model.ApiConfig;
|
||||
import com.power.doc.model.ApiDoc;
|
||||
import com.power.doc.model.ApiMethodDoc;
|
||||
import com.power.doc.model.ApiReqHeader;
|
||||
import com.power.doc.model.postman.InfoBean;
|
||||
import com.power.doc.model.postman.ItemBean;
|
||||
import com.power.doc.model.postman.RequestItem;
|
||||
import com.power.doc.model.postman.request.RequestBean;
|
||||
import com.power.doc.model.postman.request.body.BodyBean;
|
||||
import com.power.doc.model.postman.request.header.HeaderBean;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* @author yu 2019/11/21.
|
||||
*/
|
||||
public class PostMainJsonBuilder {
|
||||
|
||||
/**
|
||||
* 构建postman json
|
||||
*
|
||||
* @param config
|
||||
* @return
|
||||
*/
|
||||
public static RequestItem BuildPostmanApi(ApiConfig config) {
|
||||
DocBuilderTemplate builderTemplate = new DocBuilderTemplate();
|
||||
builderTemplate.checkAndInit(config);
|
||||
SourceBuilder sourceBuilder = new SourceBuilder(config);
|
||||
List<ApiDoc> apiDocList = sourceBuilder.getControllerApiData();
|
||||
|
||||
RequestItem requestItem = new RequestItem();
|
||||
requestItem.setInfo(new InfoBean());
|
||||
List<ItemBean> itemBeans = new ArrayList<>();
|
||||
apiDocList.forEach(
|
||||
apiDoc -> {
|
||||
ItemBean itemBean = buildItemBean(apiDoc);
|
||||
itemBeans.add(itemBean);
|
||||
}
|
||||
);
|
||||
requestItem.setItem(itemBeans);
|
||||
// requestItem 为最终的数据 转json ok
|
||||
// System.out.println(new Gson().toJson(requestItem));
|
||||
return requestItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* 第一层的Item
|
||||
*
|
||||
* @param apiDoc
|
||||
* @return
|
||||
*/
|
||||
private static ItemBean buildItemBean(ApiDoc apiDoc) {
|
||||
ItemBean itemBean = new ItemBean();
|
||||
itemBean.setName(apiDoc.getDesc());
|
||||
List<ItemBean> itemBeans = new ArrayList<>();
|
||||
List<ApiMethodDoc> apiMethodDocs = apiDoc.getList();
|
||||
apiMethodDocs.forEach(
|
||||
apiMethodDoc -> {
|
||||
ItemBean itemBean1 = buildItem(apiMethodDoc);
|
||||
itemBeans.add(itemBean1);
|
||||
}
|
||||
);
|
||||
itemBean.setItem(itemBeans);
|
||||
return itemBean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建第二层的item
|
||||
* @param apiMethodDoc
|
||||
* @return
|
||||
*/
|
||||
private static ItemBean buildItem(ApiMethodDoc apiMethodDoc) {
|
||||
ItemBean item = new ItemBean();
|
||||
RequestBean requestBean = new RequestBean();
|
||||
|
||||
item.setName(apiMethodDoc.getDesc());
|
||||
item.setDescription(apiMethodDoc.getDetail());
|
||||
|
||||
requestBean.setDescription(apiMethodDoc.getDesc());
|
||||
requestBean.setMethod(apiMethodDoc.getType());
|
||||
requestBean.setUrl(apiMethodDoc.getUrl());
|
||||
requestBean.setHeader(buildHeaderBeanList(apiMethodDoc));
|
||||
if (apiMethodDoc.getType().equals(DocGlobalConstants.HTTP_POST)) {
|
||||
requestBean.setBody(buildBodyBean(apiMethodDoc));
|
||||
} else {
|
||||
requestBean.setUrl(apiMethodDoc.getRequestUsage());
|
||||
}
|
||||
item.setRequest(requestBean);
|
||||
return item;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造请求体
|
||||
*
|
||||
* @param apiMethodDoc
|
||||
* @return
|
||||
*/
|
||||
private static BodyBean buildBodyBean(ApiMethodDoc apiMethodDoc) {
|
||||
|
||||
if (apiMethodDoc.getContentType().equals(DocGlobalConstants.FILE_CONTENT_TYPE)) {
|
||||
BodyBean bodyBean = new BodyBean(true);
|
||||
bodyBean.setMode(DocGlobalConstants.POSTMAN_MODE_FORMDATA);
|
||||
return bodyBean;
|
||||
} else if (apiMethodDoc.getContentType().contains(DocGlobalConstants.APPLICATION_JSON)) {
|
||||
BodyBean bodyBean = new BodyBean(false);
|
||||
bodyBean.setMode(DocGlobalConstants.POSTMAN_MODE_RAW);
|
||||
bodyBean.setRaw(apiMethodDoc.getRequestUsage());
|
||||
return bodyBean;
|
||||
} else {
|
||||
return new BodyBean(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造请求头
|
||||
*
|
||||
* @param apiMethodDoc
|
||||
* @return
|
||||
*/
|
||||
private static List<HeaderBean> buildHeaderBeanList(ApiMethodDoc apiMethodDoc) {
|
||||
List<HeaderBean> headerBeans = new ArrayList<>();
|
||||
|
||||
List<ApiReqHeader> headers = apiMethodDoc.getRequestHeaders();
|
||||
headers.forEach(
|
||||
apiReqHeader -> {
|
||||
HeaderBean headerBean = new HeaderBean();
|
||||
headerBean.setKey(apiReqHeader.getName());
|
||||
headerBean.setName(apiReqHeader.getName());
|
||||
headerBean.setDisabled(apiReqHeader.isRequired());
|
||||
headerBean.setDescription(apiReqHeader.getDesc());
|
||||
headerBeans.add(headerBean);
|
||||
}
|
||||
);
|
||||
|
||||
return headerBeans;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -90,4 +90,14 @@ public class DocGlobalConstants {
|
|||
public static final String NO_COMMENTS_FOUND = "No comments found.";
|
||||
|
||||
public static final String SPRING_WEB_ANNOTATION_PACKAGE = "org.springframework.web.bind.annotation";
|
||||
|
||||
public static final String FILE_CONTENT_TYPE = "multipart/form-data";
|
||||
|
||||
public static final String APPLICATION_JSON = "application/json";
|
||||
|
||||
public static final String POSTMAN_MODE_FORMDATA ="formdata";
|
||||
|
||||
public static final String POSTMAN_MODE_RAW ="raw";
|
||||
|
||||
public static final String HTTP_POST = "POST";
|
||||
}
|
||||
|
|
|
@ -7,31 +7,20 @@ import java.util.UUID;
|
|||
*/
|
||||
public class InfoBean {
|
||||
|
||||
private final String _postman_id =UUID.randomUUID().toString();
|
||||
private String _postman_id;
|
||||
private String name;
|
||||
|
||||
String schema ;
|
||||
public InfoBean(String name) {
|
||||
this.name = name;
|
||||
this._postman_id =UUID.randomUUID().toString();
|
||||
this.schema = "https://schema.getpostman.com/json/collection/v2.0.0/collection.json";
|
||||
}
|
||||
public InfoBean() {
|
||||
this.name = "smart-doc";
|
||||
}
|
||||
|
||||
public String get_postman_id() {
|
||||
return _postman_id;
|
||||
this._postman_id =UUID.randomUUID().toString();
|
||||
this.schema = "https://schema.getpostman.com/json/collection/v2.0.0/collection.json";
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getSchema() {
|
||||
String schema = "https://schema.getpostman.com/json/collection/v2.0.0/collection.json";
|
||||
return schema;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,16 @@ import java.util.List;
|
|||
public class ItemBean {
|
||||
private String name;
|
||||
private RequestBean request;
|
||||
private List<ItemBean> item;
|
||||
private String description;
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
|
@ -28,5 +37,11 @@ public class ItemBean {
|
|||
this.request = request;
|
||||
}
|
||||
|
||||
public List<ItemBean> getItem() {
|
||||
return item;
|
||||
}
|
||||
|
||||
public void setItem(List<ItemBean> item) {
|
||||
this.item = item;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,24 @@
|
|||
package com.power.doc.model.postman.request.body;
|
||||
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
/**
|
||||
* @author xingzi
|
||||
*/
|
||||
public class BodyBean {
|
||||
private String mode;
|
||||
private String raw;
|
||||
private FormData formdata;
|
||||
private BodyOptions options;
|
||||
|
||||
public BodyBean(boolean isFile) {
|
||||
if(isFile){
|
||||
this.formdata = new FormData();
|
||||
}else {
|
||||
this.options = new BodyOptions();
|
||||
}
|
||||
}
|
||||
|
||||
public String getMode() {
|
||||
return mode;
|
||||
|
@ -24,4 +36,30 @@ public class BodyBean {
|
|||
this.raw = raw;
|
||||
}
|
||||
|
||||
private class FormData{
|
||||
private String key;
|
||||
private String type;
|
||||
private String src;
|
||||
|
||||
FormData() {
|
||||
this.key = "file";
|
||||
this.type = "file";
|
||||
this.src = "";
|
||||
}
|
||||
}
|
||||
private class BodyOptions{
|
||||
private Raw raw;
|
||||
public BodyOptions() {
|
||||
this.raw = new Raw();
|
||||
}
|
||||
|
||||
private class Raw{
|
||||
private String language;
|
||||
Raw() {
|
||||
this.language = "json";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -10,6 +10,19 @@ public class HeaderBean {
|
|||
private String type;
|
||||
private boolean disabled;
|
||||
private String name;
|
||||
private String description;
|
||||
|
||||
public HeaderBean() {
|
||||
this.type = "text";
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
|
|
Loading…
Reference in New Issue