Title
-
功能描述
$funcDescribe
-
接口信息
1,请求方式:$requestMethod
2,服务地址:$requestUrl
3,请求参数类型:Json
4,返回参数类型:Json
-
接口参数
$inInfo
$inTable
-
返回值
$returnInfo
$returnTable
public class DocGeneratorGA {
public static void main(String[] args) throws IOException, TemplateException {
DocGeneratorGA docGeneratorGA = new DocGeneratorGA();
docGeneratorGA.generate(new LetterCheckController(), "delete");
}
private void generate(T t, String methodName) throws IOException, TemplateException {
// first step:instance freemarker configuration
Configuration configuration = new Configuration();
// second step:set configuration directory
String dir = DocGeneratorGA.class.getResource("/phalaenopsis/common/doc").getPath();
configuration.setDirectoryForTemplateLoading(new File(dir));
Template template = configuration.getTemplate("ApiDocTemplate.html");
String exportDir = JOptionPane.showInputDialog("请输入文档导出目录");
// export html
Writer out = new FileWriter(new File(exportDir + File.separator + methodName + ".html"));
Map map = new DocGeneratorGA().defineData(t, methodName);
template.process(map, out);
out.flush();
out.close();
}
private Map defineData(T t, String methodName) {
DocGeneratorTools docGeneratorTools = new DocGeneratorTools();
Map map = docGeneratorTools.returnMethodParamMap(t, methodName);
return map;
}
public class DocGeneratorTools {
/**
* 添加slf4j写日志对象
*/
private static final Logger logger = LoggerFactory.getLogger(DocGeneratorTools.
class);
/**
* 设置Map大小常量
*/
private static final int MAP_SIZE = 6;
/**
* 名称
*/
private static final String NAME = "name";
/**
* 说明
*/
private static final String DESCRIBE = "describe";
/**
* 类型
*/
private static final String TYPE = "type";
/**
* 是否必要参数
*/
private static final String REQUIRE = "require";
/**
* 例子
*/
private static final String EXAMPLE = "example";
/**
* Generic type
*/
private static final String SUB_CLASS = "subClass";
/**
* 反射获取实体字段中,需要排除的字段。如
*/
private static final String[] EXCLUDE_FIELDS = {"serialVersionUID"};
/**
* 包装类型类全名
*/
private static String[] classes = {
"java.lang.Byte",
"java.lang.Short",
"java.lang.Integer",
"java.lang.Long",
"java.lang.Double",
"java.lang.Float",
"java.lang.Boolean",
"java.util.Date"
};
public Map returnMethodParamMap(T t, String methodName) {
Map map = new HashMap();
Method[] methods = t.getClass().getMethods();
RequestMapping clazzRequestMapper = t.getClass().getAnnotation(RequestMapping.class);
List listMethod = Arrays.asList(methods);
Method method = Linq.extEquals(listMethod, "name", methodName);
RequestMapping methodRequestMapper = method.getAnnotation(RequestMapping.class);
//0,得到方法描述
ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
map.put("funcDescribe", apiOperation.value());
//1,得到完整的URL路径
String mappingUrl = clazzRequestMapper.value()[0] + methodRequestMapper.value()[0];
map.put("requestUrl", mappingUrl);
//2,得到请求方法:GET、POST、PUT、Delete
String requestMethod = methodRequestMapper.method()[0].name();
map.put("requestMethod", requestMethod);
// 2.1得到返回的GenericReturnType
Type genericReturnType = null;
Type returnType = method.getGenericReturnType();
if (returnType instanceof ParameterizedType) {
Type[] genericTypes = ((ParameterizedType) returnType).getActualTypeArguments();
genericReturnType = genericTypes[0];
}
// 2.2得到传入参数泛型
Type genericParameterType = null;
Type[] parameterTypes = method.getGenericParameterTypes();
for (Type parameterType : parameterTypes) {
if (parameterType instanceof ParameterizedType) {
Type[] genericTypes = ((ParameterizedType) parameterType).getActualTypeArguments();
genericParameterType = genericTypes[0];
}
}
//3,传入参数
Parameter[] inParameters = method.getParameters();
List