support get url example
This commit is contained in:
parent
6d9c524179
commit
b400fb00c7
|
@ -725,7 +725,7 @@ public class SourceBuilder {
|
|||
throw new RuntimeException("Map's key can only use String for json,but you use " + getKeyValType[0]);
|
||||
}
|
||||
String gicName = gNameTemp.substring(gNameTemp.indexOf(",") + 1, gNameTemp.lastIndexOf(">"));
|
||||
if ("java.lang.Object".equals(gicName)) {
|
||||
if (GlobalConstants.JAVA_OBJECT.equals(gicName)) {
|
||||
data.append("{").append("\"mapKey\":").append("{\"waring\":\"You may use java.util.Object for Map value; smart-doc can't be handle.\"}").append("}");
|
||||
} else if (DocClassUtil.isPrimitive(gicName)) {
|
||||
data.append("{").append("\"mapKey1\":").append(DocUtil.jsonValueByType(gicName)).append(",");
|
||||
|
@ -738,8 +738,8 @@ public class SourceBuilder {
|
|||
data.append("{").append("\"mapKey\":").append(buildJson(gicName, gNameTemp, responseFieldMap, isResp)).append("}");
|
||||
}
|
||||
return data.toString();
|
||||
} else if ("java.lang.Object".equals(typeName)) {
|
||||
if ("java.lang.Object".equals(typeName)) {
|
||||
} else if (GlobalConstants.JAVA_OBJECT.equals(typeName)) {
|
||||
if (GlobalConstants.JAVA_OBJECT.equals(typeName)) {
|
||||
data.append("{\"object\":\" any object\"},");
|
||||
// throw new RuntimeException("Please do not return java.lang.Object directly in api interface.");
|
||||
}
|
||||
|
@ -911,6 +911,11 @@ public class SourceBuilder {
|
|||
|
||||
private String buildReqJson(JavaMethod method, ApiMethodDoc apiMethodDoc) {
|
||||
List<JavaParameter> parameterList = method.getParameters();
|
||||
if (parameterList.size() < 1) {
|
||||
return "No request parameters are required.";
|
||||
}
|
||||
boolean containsBrace = apiMethodDoc.getUrl().contains("{");
|
||||
Map<String, String> paramsMap = new LinkedHashMap<>();
|
||||
for (JavaParameter parameter : parameterList) {
|
||||
JavaType javaType = parameter.getType();
|
||||
String simpleTypeName = javaType.getValue();
|
||||
|
@ -941,14 +946,20 @@ public class SourceBuilder {
|
|||
|
||||
}
|
||||
if (requestBodyCounter < 1) {
|
||||
//not json
|
||||
return "smart-doc currently cannot provide examples of parameters for the RequestParam request mode.";
|
||||
|
||||
paramsMap.put(paraName, DocUtil.getValByTypeAndFieldName(simpleTypeName, paraName,
|
||||
true));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return "No request parameters are required.";
|
||||
String url;
|
||||
if (containsBrace) {
|
||||
url = DocUtil.formatAndRemove(apiMethodDoc.getUrl(), paramsMap);
|
||||
url = DocUtil.urlJoin(url, paramsMap);
|
||||
} else {
|
||||
url = DocUtil.urlJoin(apiMethodDoc.getUrl(), paramsMap);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -14,4 +14,9 @@ public class GlobalConstants {
|
|||
* rest controller注解全名称
|
||||
*/
|
||||
public static final String REST_CONTROLLER_FULLY = "org.springframework.web.bind.annotation.RestController";
|
||||
|
||||
/**
|
||||
* java object类名
|
||||
*/
|
||||
public static final String JAVA_OBJECT = "java.lang.Object";
|
||||
}
|
||||
|
|
|
@ -29,6 +29,8 @@ public class DocUtil {
|
|||
fieldValue.put("name-string", faker.name().username());
|
||||
fieldValue.put("url-string", faker.internet().url());
|
||||
fieldValue.put("username-string", faker.name().username());
|
||||
fieldValue.put("page-int", "1");
|
||||
fieldValue.put("page-integer", "1");
|
||||
fieldValue.put("age-int", String.valueOf(RandomUtil.randomInt(0, 70)));
|
||||
fieldValue.put("age-integer", String.valueOf(RandomUtil.randomInt(0, 70)));
|
||||
fieldValue.put("email-string", faker.internet().emailAddress());
|
||||
|
@ -58,10 +60,19 @@ public class DocUtil {
|
|||
fieldValue.put("flag-boolean", "true");
|
||||
fieldValue.put("flag-Boolean", "false");
|
||||
fieldValue.put("idcard-string", IDCardUtil.getIdCard());
|
||||
fieldValue.put("sex-int", String.valueOf(RandomUtil.randomInt(0, 1)));
|
||||
fieldValue.put("sex-integer", String.valueOf(RandomUtil.randomInt(0, 1)));
|
||||
fieldValue.put("gender-int", String.valueOf(RandomUtil.randomInt(0, 1)));
|
||||
fieldValue.put("gender-integer", String.valueOf(RandomUtil.randomInt(0, 1)));
|
||||
fieldValue.put("sex-int", String.valueOf(RandomUtil.randomInt(0, 2)));
|
||||
fieldValue.put("sex-integer", String.valueOf(RandomUtil.randomInt(0, 2)));
|
||||
fieldValue.put("gender-int", String.valueOf(RandomUtil.randomInt(0, 2)));
|
||||
fieldValue.put("gender-integer", String.valueOf(RandomUtil.randomInt(0, 2)));
|
||||
fieldValue.put("limit-int", "10");
|
||||
fieldValue.put("limit-integer", "10");
|
||||
fieldValue.put("size-int", "10");
|
||||
fieldValue.put("size-integer", "10");
|
||||
|
||||
fieldValue.put("offset-int", "1");
|
||||
fieldValue.put("offset-integer", "1");
|
||||
fieldValue.put("offset-long", "1");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -116,6 +127,22 @@ public class DocUtil {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除字符串的双引号
|
||||
*
|
||||
* @param type0 类型
|
||||
* @param filedName 字段名称
|
||||
* @param removeDoubleQuotation 移除标志
|
||||
* @return
|
||||
*/
|
||||
public static String getValByTypeAndFieldName(String type0, String filedName, boolean removeDoubleQuotation) {
|
||||
if (removeDoubleQuotation) {
|
||||
return getValByTypeAndFieldName(type0, filedName).replace("\"", "");
|
||||
} else {
|
||||
return getValByTypeAndFieldName(type0, filedName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是合法的java类名称
|
||||
*
|
||||
|
@ -160,4 +187,54 @@ public class DocUtil {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* An interpreter for strings with named placeholders.
|
||||
*
|
||||
* @param str string to format
|
||||
* @param values to replace
|
||||
* @return formatted string
|
||||
*/
|
||||
public static String formatAndRemove(String str, Map<String, String> values) {
|
||||
StringBuilder builder = new StringBuilder(str);
|
||||
Set<Map.Entry<String, String>> entries = values.entrySet();
|
||||
Iterator<Map.Entry<String, String>> iteratorMap = entries.iterator();
|
||||
while (iteratorMap.hasNext()) {
|
||||
Map.Entry<String, String> next = iteratorMap.next();
|
||||
int start;
|
||||
String pattern = "{" + next.getKey() + "}";
|
||||
String value = next.getValue().toString();
|
||||
// values.remove(next.getKey());
|
||||
// Replace every occurence of {key} with value
|
||||
while ((start = builder.indexOf(pattern)) != -1) {
|
||||
builder.replace(start, start + pattern.length(), value);
|
||||
iteratorMap.remove();
|
||||
values.remove(next.getKey());
|
||||
}
|
||||
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static String urlJoin(String url, Map<String, String> params) {
|
||||
StringBuilder endUrl = new StringBuilder(url);
|
||||
if (null == params) {
|
||||
return url;
|
||||
}
|
||||
boolean isFirst = true;
|
||||
Set<Map.Entry<String, String>> entrySet = params.entrySet();
|
||||
for (Map.Entry<String, String> entry : entrySet) {
|
||||
if (isFirst && !url.contains("?")) {
|
||||
isFirst = false;
|
||||
endUrl.append("?");
|
||||
} else {
|
||||
endUrl.append("&");
|
||||
}
|
||||
endUrl.append(entry.getKey());
|
||||
endUrl.append("=");
|
||||
endUrl.append(entry.getValue());
|
||||
}
|
||||
return endUrl.toString();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue