fix request-example

This commit is contained in:
songhaozhi 2020-01-17 13:32:22 +08:00
parent c1b97ad55e
commit 30fab5b2c1
4 changed files with 41 additions and 33 deletions

View File

@ -113,9 +113,13 @@ public class DocGlobalConstants {
public static final String SHORT_REQUEST_BODY = "RequestBody";
public static final String CURL_GET = "curl -X GET -i ";
public static final String CURL_POST = "curl -X POST -i ";
public static final String CURL_PUT = "curl -X PUT -i ";
public static final String CURL_DELETE = "curl -X DELETE -i ";
public static final String CURL_POST_JSON = "curl -X POST -H 'Content-Type: application/json; charset=utf-8' -i ";
public static final String CURL_PUT_JSON = "curl -X PUT -H 'Content-Type: application/json; charset=utf-8' -i ";
public static final String ENMPTY = "";
}

View File

@ -53,11 +53,10 @@ public class SpringMVCRequestMappingHandler {
*/
public RequestMapping handle(String serverUrl, String controllerBaseUrl, JavaMethod method) {
List<JavaAnnotation> annotations = method.getAnnotations();
String url = null;
String url;
String methodType = null;
String shortUrl = null;
String mediaType = null;
boolean isPostMethod = false;
int methodCounter = 0;
for (JavaAnnotation annotation : annotations) {
String annotationName = annotation.getType().getName();
@ -68,13 +67,7 @@ public class SpringMVCRequestMappingHandler {
if (SpringMvcAnnotations.REQUEST_MAPPING.equals(annotationName) || DocGlobalConstants.REQUEST_MAPPING_FULLY.equals(annotationName)) {
shortUrl = DocUtil.handleMappingValue(annotation);
Object nameParam = annotation.getNamedParameter("method");
if (null != nameParam) {
methodType = nameParam.toString();
methodType = DocUtil.handleHttpMethod(methodType);
if ("POST".equals(methodType) || "PUT".equals(methodType)) {
isPostMethod = true;
}
} else {
if ( null == nameParam ) {
methodType = Methods.GET.getValue();
}
methodCounter++;
@ -86,7 +79,6 @@ public class SpringMVCRequestMappingHandler {
shortUrl = DocUtil.handleMappingValue(annotation);
methodType = Methods.POST.getValue();
methodCounter++;
isPostMethod = true;
} else if (SpringMvcAnnotations.PUT_MAPPING.equals(annotationName) || DocGlobalConstants.PUT_MAPPING_FULLY.equals(annotationName)) {
shortUrl = DocUtil.handleMappingValue(annotation);
methodType = Methods.PUT.getValue();
@ -111,8 +103,7 @@ public class SpringMVCRequestMappingHandler {
shortUrl = UrlUtil.simplifyUrl("/" + controllerBaseUrl + "/" + shortUrl);
}
RequestMapping requestMapping = RequestMapping.builder().
setMediaType(mediaType).setMethodType(methodType).setUrl(url).setShortUrl(shortUrl)
.setPostMethod(isPostMethod);
setMediaType(mediaType).setMethodType(methodType).setUrl(url).setShortUrl(shortUrl);
return requestMapping;
}
return null;

View File

@ -37,7 +37,6 @@ public class RequestMapping {
private String shortUrl;
private String methodType;
private String mediaType;
private boolean postMethod;
public static RequestMapping builder() {
return new RequestMapping();
@ -78,13 +77,4 @@ public class RequestMapping {
this.mediaType = mediaType;
return this;
}
public boolean isPostMethod() {
return postMethod;
}
public RequestMapping setPostMethod(boolean postMethod) {
this.postMethod = postMethod;
return this;
}
}

View File

@ -154,7 +154,8 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate {
List<ApiParam> requestParams = requestParams(method, DocTags.PARAM, projectBuilder);
apiMethodDoc.setRequestParams(requestParams);
// build request json
ApiRequestExample requestExample = buildReqJson(method, apiMethodDoc, requestMapping.isPostMethod(), projectBuilder);
ApiRequestExample requestExample = buildReqJson(method, apiMethodDoc, requestMapping.getMethodType(),
projectBuilder);
String requestJson = requestExample.getExampleBody();
// set request example detail
apiMethodDoc.setRequestExample(requestExample);
@ -180,7 +181,8 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate {
return methodDocList;
}
private ApiRequestExample buildReqJson(JavaMethod method, ApiMethodDoc apiMethodDoc, Boolean isPostMethod, ProjectDocConfigBuilder configBuilder) {
private ApiRequestExample buildReqJson(JavaMethod method, ApiMethodDoc apiMethodDoc, String methodType,
ProjectDocConfigBuilder configBuilder) {
List<JavaParameter> parameterList = method.getParameters();
if (parameterList.size() < 1) {
return ApiRequestExample.builder().setUrl(apiMethodDoc.getUrl());
@ -320,9 +322,8 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate {
String body;
String exampleBody;
String url;
if (isPostMethod) {
//for post
if (Methods.POST.getValue().equals( methodType ) || Methods.PUT.getValue().equals( methodType )) {
//for post put
path = DocUtil.formatAndRemove(path, pathParamsMap);
body = UrlUtil.urlJoin(DocGlobalConstants.ENMPTY, DocUtil.formDataToMap(formDataList)).replace("?", DocGlobalConstants.ENMPTY);
body = StringUtil.removeQuotes(body);
@ -330,27 +331,49 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate {
url = UrlUtil.simplifyUrl(url);
if (requestExample.isJson()) {
if (StringUtil.isNotEmpty(requestExample.getJsonBody())) {
exampleBody = DocGlobalConstants.CURL_POST_JSON + url + " --data \'" + requestExample.getJsonBody() + "\n'";
if(Methods.POST.getValue().equals( methodType )){
exampleBody =
DocGlobalConstants.CURL_POST_JSON + url + " --data '" + requestExample.getJsonBody() + "'";
}else{
exampleBody =
DocGlobalConstants.CURL_PUT_JSON + url + " --data '" + requestExample.getJsonBody() + "'";
}
} else {
exampleBody = DocGlobalConstants.CURL_POST + url;
if(Methods.POST.getValue().equals( methodType )){
exampleBody = DocGlobalConstants.CURL_POST + url;
}else{
exampleBody = DocGlobalConstants.CURL_PUT + url;
}
}
} else {
if (StringUtil.isNotEmpty(body)) {
exampleBody = DocGlobalConstants.CURL_POST + url + " --data \'" + body + "'";
if(Methods.POST.getValue().equals( methodType )){
exampleBody = DocGlobalConstants.CURL_POST + url + " --data '" + body + "'";
}else{
exampleBody = DocGlobalConstants.CURL_PUT + url + " --data '" + body + "'";
}
} else {
exampleBody = DocGlobalConstants.CURL_POST + url;
if(Methods.POST.getValue().equals( methodType )){
exampleBody = DocGlobalConstants.CURL_POST + url;
}else{
exampleBody = DocGlobalConstants.CURL_PUT + url;
}
}
}
requestExample.setExampleBody(exampleBody).setUrl(url);
} else {
// for get
// for get delete
pathParamsMap.putAll(DocUtil.formDataToMap(formDataList));
path = DocUtil.formatAndRemove(path, pathParamsMap);
url = UrlUtil.urlJoin(path, pathParamsMap);
url = StringUtil.removeQuotes(url);
url = apiMethodDoc.getServerUrl() + "/" + url;
url = UrlUtil.simplifyUrl(url);
exampleBody = "curl -X GET -i \'" + url + "\'";
if(Methods.GET.getValue().equals( methodType )){
exampleBody = DocGlobalConstants.CURL_GET + url ;
}else{
exampleBody = DocGlobalConstants.CURL_DELETE + url ;
}
requestExample.setExampleBody(exampleBody).setJsonBody("").setUrl(url);
}
return requestExample;