diff --git a/src/main/java/com/power/doc/constants/DocGlobalConstants.java b/src/main/java/com/power/doc/constants/DocGlobalConstants.java index ab0d226..6ee4db0 100644 --- a/src/main/java/com/power/doc/constants/DocGlobalConstants.java +++ b/src/main/java/com/power/doc/constants/DocGlobalConstants.java @@ -95,8 +95,12 @@ public class DocGlobalConstants { public static final String FILE_CONTENT_TYPE = "multipart/form-data"; + public static final String MULTIPART_TYPE = "multipart/form-data"; + public static final String APPLICATION_JSON = "application/json"; + public static final String JSON_CONTENT_TYPE = "application/json; charset=utf-8"; + public static final String POSTMAN_MODE_FORMDATA ="formdata"; public static final String POSTMAN_MODE_RAW ="raw"; diff --git a/src/main/java/com/power/doc/constants/SpringMvcAnnotations.java b/src/main/java/com/power/doc/constants/SpringMvcAnnotations.java index a420c0a..9f962c9 100644 --- a/src/main/java/com/power/doc/constants/SpringMvcAnnotations.java +++ b/src/main/java/com/power/doc/constants/SpringMvcAnnotations.java @@ -20,4 +20,10 @@ public class SpringMvcAnnotations { public static final String REQUEST_PARAM = "RequestParam"; public static final String REQUEST_BODY = "RequestBody"; + + public static final String CONTROLLER = "Controller"; + + public static final String REST_CONTROLLER = "RestController"; + + public static final String PATH_VARIABLE = "PathVariable"; } diff --git a/src/main/java/com/power/doc/handler/SpringMVCRequestMappingHandler.java b/src/main/java/com/power/doc/handler/SpringMVCRequestMappingHandler.java index e5d17c4..621fe31 100644 --- a/src/main/java/com/power/doc/handler/SpringMVCRequestMappingHandler.java +++ b/src/main/java/com/power/doc/handler/SpringMVCRequestMappingHandler.java @@ -85,7 +85,7 @@ public class SpringMVCRequestMappingHandler { url = DocUrlUtil.getMvcUrls(serverUrl, controllerBaseUrl, Arrays.asList(urls)); shortUrl = DocUrlUtil.getMvcUrls("", controllerBaseUrl, Arrays.asList(urls)); } else { - url = UrlUtil.simplifyUrl(serverUrl + "/" + controllerBaseUrl + "/" + url); + url = UrlUtil.simplifyUrl(serverUrl + "/" + controllerBaseUrl + "/" + shortUrl); shortUrl = UrlUtil.simplifyUrl("/" + controllerBaseUrl + "/" + url); } RequestMapping requestMapping = RequestMapping.builder(). diff --git a/src/main/java/com/power/doc/helper/JsonBuildHelper.java b/src/main/java/com/power/doc/helper/JsonBuildHelper.java index 63bf44e..83b494a 100644 --- a/src/main/java/com/power/doc/helper/JsonBuildHelper.java +++ b/src/main/java/com/power/doc/helper/JsonBuildHelper.java @@ -49,8 +49,8 @@ public class JsonBuildHelper { * @param counter Recursive counter * @return String */ - private static String buildJson(String typeName, String genericCanonicalName, - boolean isResp, int counter, Map registryClasses, ProjectDocConfigBuilder builder) { + public static String buildJson(String typeName, String genericCanonicalName, + boolean isResp, int counter, Map registryClasses, ProjectDocConfigBuilder builder) { if (registryClasses.containsKey(typeName) && counter > registryClasses.size()) { return "{\"$ref\":\"...\"}"; } diff --git a/src/main/java/com/power/doc/model/ApiRequestExample.java b/src/main/java/com/power/doc/model/ApiRequestExample.java index d613b87..c27a627 100644 --- a/src/main/java/com/power/doc/model/ApiRequestExample.java +++ b/src/main/java/com/power/doc/model/ApiRequestExample.java @@ -2,6 +2,8 @@ package com.power.doc.model; import com.power.doc.model.postman.request.body.FormData; +import java.util.List; + /** * @author yu 2019/12/22. */ @@ -10,17 +12,25 @@ public class ApiRequestExample { private String jsonBody; + private String exampleBody; + private String url; - private FormData formData; + private List formDataList; + private boolean json; + + public static ApiRequestExample builder(){ + return new ApiRequestExample(); + } public String getJsonBody() { return jsonBody; } - public void setJsonBody(String jsonBody) { + public ApiRequestExample setJsonBody(String jsonBody) { this.jsonBody = jsonBody; + return this; } public String getUrl() { @@ -31,11 +41,30 @@ public class ApiRequestExample { this.url = url; } - public FormData getFormData() { - return formData; + public List getFormDataList() { + return formDataList; } - public void setFormData(FormData formData) { - this.formData = formData; + public ApiRequestExample setFormDataList(List formDataList) { + this.formDataList = formDataList; + return this; + } + + public boolean isJson() { + return json; + } + + public ApiRequestExample setJson(boolean json) { + this.json = json; + return this; + } + + public String getExampleBody() { + return exampleBody; + } + + public ApiRequestExample setExampleBody(String exampleBody) { + this.exampleBody = exampleBody; + return this; } } diff --git a/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java b/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java index 2f04749..531aeb2 100644 --- a/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java +++ b/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java @@ -328,12 +328,9 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate { private boolean checkController(JavaClass cls) { List classAnnotations = cls.getAnnotations(); for (JavaAnnotation annotation : classAnnotations) { - String annotationName = annotation.getType().getName(); - if (DocAnnotationConstants.SHORT_CONTROLLER.equals(annotationName) - || DocAnnotationConstants.SHORT_REST_CONTROLLER.equals(annotationName) - || DocGlobalConstants.REST_CONTROLLER_FULLY.equals(annotationName) - || DocGlobalConstants.CONTROLLER_FULLY.equals(annotationName) - ) { + String name = annotation.getType().getName(); + name = DocClassUtil.getAnnotationSimpleName(name); + if (SpringMvcAnnotations.CONTROLLER.equals(name) || SpringMvcAnnotations.REST_CONTROLLER.equals(name)) { return true; } } diff --git a/src/main/java/com/power/doc/utils/DocUtil.java b/src/main/java/com/power/doc/utils/DocUtil.java index 042391c..d978a2c 100644 --- a/src/main/java/com/power/doc/utils/DocUtil.java +++ b/src/main/java/com/power/doc/utils/DocUtil.java @@ -7,6 +7,7 @@ import com.power.common.util.RandomUtil; import com.power.common.util.StringUtil; import com.power.doc.constants.DocAnnotationConstants; import com.power.doc.constants.DocGlobalConstants; +import com.power.doc.model.postman.request.body.FormData; import com.thoughtworks.qdox.model.DocletTag; import com.thoughtworks.qdox.model.JavaAnnotation; import com.thoughtworks.qdox.model.JavaField; @@ -387,4 +388,8 @@ public class DocUtil { builder.append("\"").append(content).append("\""); return builder.toString(); } + + public static Map formDataToMap(List formDataList){ + return formDataList.stream().collect(Collectors.toMap(FormData::getKey,FormData::getValue)); + } } diff --git a/src/main/java/com/power/doc/utils/JavaFieldUtil.java b/src/main/java/com/power/doc/utils/JavaFieldUtil.java index 847ffaf..651c5d3 100644 --- a/src/main/java/com/power/doc/utils/JavaFieldUtil.java +++ b/src/main/java/com/power/doc/utils/JavaFieldUtil.java @@ -1,7 +1,12 @@ package com.power.doc.utils; +import com.power.common.util.StringUtil; +import com.power.doc.constants.DocTags; +import com.power.doc.model.postman.request.body.FormData; import com.thoughtworks.qdox.model.JavaField; + +import java.util.ArrayList; import java.util.List; /** @@ -23,4 +28,30 @@ public class JavaFieldUtil { } return false; } + + public static List getFormData(List fields){ + List formDataList = new ArrayList<>(); + for(JavaField field:fields){ + String fieldName = field.getName(); + String subTypeName = field.getType().getFullyQualifiedName(); + if ("this$0".equals(fieldName) || + "serialVersionUID".equals(fieldName) || + DocClassUtil.isIgnoreFieldTypes(subTypeName)) { + continue; + } + String typeSimpleName = field.getType().getSimpleName(); + if (DocClassUtil.isPrimitive(subTypeName)){ + String fieldValue = DocUtil.getValByTypeAndFieldName(typeSimpleName, field.getName()); + FormData formData = new FormData(); + formData.setKey(fieldName); + formData.setType("text"); + formData.setValue(fieldValue); + formDataList.add(formData); + } else { + continue; + } + } + return formDataList; + + } }