From ccbd3c8e7a8a1e7904e28bb72a764158a51e089d Mon Sep 17 00:00:00 2001 From: wangbing Date: Thu, 13 Jun 2019 23:56:00 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81Api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../javafx/manger/callable/ApiCallable.java | 567 +++++------------- .../resources/modules/Java_api/ApiClient.ftl | 75 +-- .../resources/modules/Java_api/ApiTest.ftl | 98 +++ .../Java_api/module/request/request.ftl | 4 +- .../Java_api/module/response/response.ftl | 2 +- src/main/resources/modules/Java_api/pom.ftl | 6 + 6 files changed, 253 insertions(+), 499 deletions(-) create mode 100644 src/main/resources/modules/Java_api/ApiTest.ftl diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/ApiCallable.java b/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/ApiCallable.java index 6856b6ab..f8d90b45 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/ApiCallable.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/ApiCallable.java @@ -47,6 +47,46 @@ public class ApiCallable implements Callable { dBmapper = ProjectManager.dBmapper; + File domain; + File testdomain; + File request; + File response; + File entity; + File enumeration; + + StringBuffer sbmain = new StringBuffer(""); + sbmain.append(this.api.getPath() + File.separator); + sbmain.append("src" + File.separator); + sbmain.append("main" + File.separator); + sbmain.append("java" + File.separator); + + StringBuffer sbtest = new StringBuffer(""); + sbtest.append(this.api.getPath() + File.separator); + sbtest.append("src" + File.separator); + sbtest.append("test" + File.separator); + sbtest.append("java" + File.separator); + + JavaClassReader javaClass = new JavaClassReader(apis.get(0).getReq()); + String[] split = javaClass.getDomainName().split("\\."); + + for (String s1 : split) { + sbmain.append(s1 + File.separator); + sbtest.append(s1 + File.separator); + } + + domain = new File(sbmain.toString()); + domain.mkdirs(); + testdomain = new File(sbtest.toString()); + testdomain.mkdirs(); + request = new File(domain, "request"); + request.mkdirs(); + response = new File(domain, "response"); + response.mkdirs(); + entity = new File(domain, "entity"); + entity.mkdirs(); + enumeration = new File(domain, "enums"); + enumeration.mkdirs(); + { System.out.println("生成模块:Pom"); HashMap ctx = new HashMap(); @@ -55,9 +95,7 @@ public class ApiCallable implements Callable { freeMarkerManager.outputTemp(file, "Java_api/pom.ftl", ctx); } - File frameWork = null; - String domain = null; - String module = null; + Set managerList = new HashSet<>(); List methodList = new ArrayList<>(); @@ -75,35 +113,9 @@ public class ApiCallable implements Callable { method.setMethod(Tool.getRequestAction(javaClassReader.getClassName())); method.setManager(Tool.getRequestTarget(javaClassReader.getClassName()) + "Manager"); methodList.add(method); - StringBuffer stringBuffer = new StringBuffer(""); - stringBuffer.append(this.api.getPath() + File.separator); - stringBuffer.append("src" + File.separator); - stringBuffer.append("main" + File.separator); - stringBuffer.append("java" + File.separator); - - String[] split = javaClassReader.getDomainName().split("\\."); managerList.add(method.getManager()); - for (String s1 : split) { - stringBuffer.append(s1 + File.separator); - } - if (frameWork == null) { - frameWork = new File(stringBuffer.toString()); - frameWork.mkdirs(); - } - if (domain == null) { - domain = javaClassReader.getDomainName(); - } - if (module == null) { - module = javaClassReader.getModuleName(); - } - - stringBuffer.append(javaClassReader.getModuleName()); - - File request = new File(stringBuffer.toString(), "request"); - request.mkdirs(); - { HashMap ctx = new HashMap(); ctx.put("package", "package " + javaClassReader.getDomainName() + "." + javaClassReader.getModuleName() + "." + "request;"); @@ -131,24 +143,6 @@ public class ApiCallable implements Callable { //region 生成response JavaClassReader javaClassReader = new JavaClassReader(api.getRsp()); - StringBuffer stringBuffer = new StringBuffer(""); - stringBuffer.append(this.api.getPath() + File.separator); - stringBuffer.append("src" + File.separator); - stringBuffer.append("main" + File.separator); - stringBuffer.append("java" + File.separator); - - String[] split = javaClassReader.getDomainName().split("\\."); - - - for (String s1 : split) { - stringBuffer.append(s1 + File.separator); - } - - stringBuffer.append(javaClassReader.getModuleName()); - - File response = new File(stringBuffer.toString(), "response"); - response.mkdirs(); - { HashMap ctx = new HashMap(); ctx.put("package", "package " + javaClassReader.getDomainName() + "." + javaClassReader.getModuleName() + "." + "request;"); @@ -187,33 +181,6 @@ public class ApiCallable implements Callable { method.setMethod(Tool.getRequestAction(javaClassReader.getClassName())); method.setManager(Tool.getRequestTarget(javaClassReader.getClassName()) + "Manager"); - StringBuffer stringBuffer = new StringBuffer(""); - stringBuffer.append(this.api.getPath() + File.separator); - stringBuffer.append("src" + File.separator); - stringBuffer.append("main" + File.separator); - stringBuffer.append("java" + File.separator); - - String[] split = javaClassReader.getDomainName().split("\\."); - - for (String s1 : split) { - stringBuffer.append(s1 + File.separator); - } - if (frameWork == null) { - frameWork = new File(stringBuffer.toString()); - frameWork.mkdirs(); - } - if (domain == null) { - domain = javaClassReader.getDomainName(); - } - if (module == null) { - module = javaClassReader.getModuleName(); - } - - stringBuffer.append(javaClassReader.getModuleName()); - - File request = new File(stringBuffer.toString(), "request"); - request.mkdirs(); - { HashMap ctx = new HashMap(); ctx.put("package", "package " + javaClassReader.getDomainName() + "." + javaClassReader.getModuleName() + "." + "request;"); @@ -243,24 +210,6 @@ public class ApiCallable implements Callable { } JavaClassReader javaClassReader = new JavaClassReader(f); - StringBuffer stringBuffer = new StringBuffer(""); - stringBuffer.append(this.api.getPath() + File.separator); - stringBuffer.append("src" + File.separator); - stringBuffer.append("main" + File.separator); - stringBuffer.append("java" + File.separator); - - String[] split = javaClassReader.getDomainName().split("\\."); - - - for (String s1 : split) { - stringBuffer.append(s1 + File.separator); - } - - stringBuffer.append(javaClassReader.getModuleName()); - - File response = new File(stringBuffer.toString(), "response"); - response.mkdirs(); - { HashMap ctx = new HashMap(); ctx.put("package", "package " + javaClassReader.getDomainName() + "." + javaClassReader.getModuleName() + "." + "request;"); @@ -294,24 +243,6 @@ public class ApiCallable implements Callable { } JavaClassReader javaClassReader = new JavaClassReader(f); - StringBuffer stringBuffer = new StringBuffer(""); - stringBuffer.append(this.api.getPath() + File.separator); - stringBuffer.append("src" + File.separator); - stringBuffer.append("main" + File.separator); - stringBuffer.append("java" + File.separator); - - String[] split = javaClassReader.getDomainName().split("\\."); - - - for (String s1 : split) { - stringBuffer.append(s1 + File.separator); - } - - stringBuffer.append(javaClassReader.getModuleName()); - - File entity = new File(stringBuffer.toString(), "entity"); - entity.mkdirs(); - { HashMap ctx = new HashMap(); ctx.put("package", "package " + javaClassReader.getDomainName() + "." + javaClassReader.getModuleName() + "." + "request;"); @@ -341,24 +272,6 @@ public class ApiCallable implements Callable { } JavaEnumReader javaEnumReader = new JavaEnumReader(f); - StringBuffer stringBuffer = new StringBuffer(""); - stringBuffer.append(this.api.getPath() + File.separator); - stringBuffer.append("src" + File.separator); - stringBuffer.append("main" + File.separator); - stringBuffer.append("java" + File.separator); - - String[] split = javaEnumReader.getDomainName().split("\\."); - - - for (String s1 : split) { - stringBuffer.append(s1 + File.separator); - } - - stringBuffer.append(javaEnumReader.getModuleName()); - - File enums_ = new File(stringBuffer.toString(), "enums"); - enums_.mkdirs(); - { HashMap ctx = new HashMap(); ctx.put("package", "package " + javaEnumReader.getDomainName() + "." + javaEnumReader.getModuleName() + "." + "enums;"); @@ -369,7 +282,7 @@ public class ApiCallable implements Callable { ctx.put("body", javaEnumReader.getBody()); ctx.put("tool", tool); - File file = new File(enums_, f.getName()); + File file = new File(enumeration, f.getName()); freeMarkerManager.outputTemp(file, "Java_api/module/enums/type.ftl", ctx); System.out.println("生成文件" + file.getName() + "成功"); } @@ -382,315 +295,125 @@ public class ApiCallable implements Callable { } } - -// if (req.exists() && rsp.exists() && ent.exists()) { -// File[] files = req.listFiles(); -// System.out.println("生成模块:Request,Response,Entity"); -// for (File f : files) { -// try { -// //region 生成request -// JavaClassReader javaClassReader = new JavaClassReader(f); -// -// Method method = new Method(); -// method.setStringMethod("api." + javaClassReader.getModuleName() + "." + Tool.camelToPoint(javaClassReader.getClassName().replaceAll("Request", ""))); -// method.setRequest(javaClassReader.getClassName()); -// method.setTarget(Tool.getRequestTarget(javaClassReader.getClassName())); -// method.setMethod(Tool.getRequestAction(javaClassReader.getClassName())); -// method.setManager(Tool.getRequestTarget(javaClassReader.getClassName()) + "Manager"); -// methodList.add(method); -// StringBuffer stringBuffer = new StringBuffer(""); -// stringBuffer.append(api.getPath() + File.separator); -// stringBuffer.append("src" + File.separator); -// stringBuffer.append("main" + File.separator); -// stringBuffer.append("java" + File.separator); -// -// String[] split = javaClassReader.getDomainName().split("\\."); -// -// -// for (String s1 : split) { -// stringBuffer.append(s1 + File.separator); -// } -// if (frameWork == null) { -// frameWork = new File(stringBuffer.toString()); -// frameWork.mkdirs(); -// } -// if (domain == null) { -// domain = javaClassReader.getDomainName(); -// } -// if (module == null) { -// module = javaClassReader.getModuleName(); -// } -// -// stringBuffer.append(javaClassReader.getModuleName()); -// -// File request = new File(stringBuffer.toString() + File.separator + "request"); -// request.mkdirs(); -// -// { -// HashMap ctx = new HashMap(); -// ctx.put("package", "package " + javaClassReader.getDomainName() + "." + javaClassReader.getModuleName() + "." + "request;"); -// ctx.put("domain", javaClassReader.getDomainName()); -// ctx.put("module", javaClassReader.getModuleName()); -// ctx.put("importList", javaClassReader.getImportList()); -// ctx.put("annotation", javaClassReader.getAnnotationList()); -// ctx.put("className", javaClassReader.getClassName().replaceAll("Request", "")); -// ctx.put("body", javaClassReader.getBody()); -// ctx.put("tool", tool); -// ctx.put("hasList", javaClassReader.isHasList()); -// ctx.put("findOrSearchflag", javaClassReader.getFindOrSearchflag()); -// File file = new File(request.getAbsolutePath() + File.separator + f.getName()); -// -// freeMarkerManager.outputTemp(file, "Java_api/module/request/request.ftl", ctx); -// System.out.println("生成文件" + file.getName() + "成功"); -// } -// //endregion -// } catch (IOException e) { -// e.printStackTrace(); -// } -// -// try { -// //region 生成response -// File r = new File(f.getParent().replaceAll("req", "rsp") + File.separator + f.getName().replaceAll("Request", "Response")); -// JavaClassReader javaClassReader = new JavaClassReader(r); -// -// StringBuffer stringBuffer = new StringBuffer(""); -// stringBuffer.append(api.getPath() + File.separator); -// stringBuffer.append("src" + File.separator); -// stringBuffer.append("main" + File.separator); -// stringBuffer.append("java" + File.separator); -// -// String[] split = javaClassReader.getDomainName().split("\\."); -// -// -// for (String s1 : split) { -// stringBuffer.append(s1 + File.separator); -// } -// -// stringBuffer.append(javaClassReader.getModuleName()); -// -// File response = new File(stringBuffer.toString() + File.separator + "response"); -// response.mkdirs(); -// -// { -// HashMap ctx = new HashMap(); -// ctx.put("package", "package " + javaClassReader.getDomainName() + "." + javaClassReader.getModuleName() + "." + "request;"); -// ctx.put("domain", javaClassReader.getDomainName()); -// ctx.put("module", javaClassReader.getModuleName()); -// ctx.put("importList", javaClassReader.getImportList()); -// ctx.put("annotation", javaClassReader.getAnnotationList()); -// ctx.put("className", javaClassReader.getClassName().replaceAll("Response", "")); -// ctx.put("body", javaClassReader.getBody()); -// ctx.put("tool", tool); -// ctx.put("hasList", javaClassReader.isHasList()); -// ctx.put("Tclass", javaClassReader.getTclass()); -// File file = new File(response.getAbsolutePath() + File.separator + f.getName().replaceAll("Request", "Response")); -// -// freeMarkerManager.outputTemp(file, "Java_api/module/response/response.ftl", ctx); -// System.out.println("生成文件" + r.getName() + "成功"); -// } -// //endregion -// } catch (IOException e) { -// e.printStackTrace(); -// } -// } -// -// for (File f : ent.listFiles()) { -// try { -// //region 生成entity -// JavaClassReader javaClassReader = new JavaClassReader(f); -// managerList.add(javaClassReader.getClassName() + "Manager"); -// -// StringBuffer stringBuffer = new StringBuffer(""); -// stringBuffer.append(api.getPath() + File.separator); -// stringBuffer.append("src" + File.separator); -// stringBuffer.append("main" + File.separator); -// stringBuffer.append("java" + File.separator); -// -// String[] split = javaClassReader.getDomainName().split("\\."); -// -// -// for (String s1 : split) { -// stringBuffer.append(s1 + File.separator); -// } -// -// stringBuffer.append(javaClassReader.getModuleName()); -// -// File entity = new File(stringBuffer.toString() + File.separator + "entity"); -// entity.mkdirs(); -// -// { -// HashMap ctx = new HashMap(); -// ctx.put("package", "package " + javaClassReader.getDomainName() + "." + javaClassReader.getModuleName() + "." + "request;"); -// ctx.put("domain", javaClassReader.getDomainName()); -// ctx.put("module", javaClassReader.getModuleName()); -// ctx.put("importList", javaClassReader.getImportList()); -// ctx.put("annotation", javaClassReader.getAnnotationList()); -// ctx.put("className", javaClassReader.getClassName().replaceAll("Entity", "")); -// ctx.put("body", javaClassReader.getBody()); -// ctx.put("tool", tool); -// File file = new File(entity.getAbsolutePath() + File.separator + f.getName()); -// freeMarkerManager.outputTemp(file, "Java_api/module/entity/entity.ftl", ctx); -// System.out.println("生成文件" + file.getName() + "成功"); -// } -// //endregion -// } catch (IOException e) { -// e.printStackTrace(); -// } -// } -// } else { -// return false; -// } -// -// if (enums.exists()) { -// System.out.println("生成模块:Enums"); -// for (File f : enums.listFiles()) { -// try { -// //region 生成enums -// JavaEnumReader javaEnumReader = new JavaEnumReader(f); -// -// StringBuffer stringBuffer = new StringBuffer(""); -// stringBuffer.append(api.getPath() + File.separator); -// stringBuffer.append("src" + File.separator); -// stringBuffer.append("main" + File.separator); -// stringBuffer.append("java" + File.separator); -// -// String[] split = javaEnumReader.getDomainName().split("\\."); -// -// -// for (String s1 : split) { -// stringBuffer.append(s1 + File.separator); -// } -// -// stringBuffer.append(javaEnumReader.getModuleName()); -// -// File enums_ = new File(stringBuffer.toString() + File.separator + "enums"); -// enums_.mkdirs(); -// -// { -// HashMap ctx = new HashMap(); -// ctx.put("package", "package " + javaEnumReader.getDomainName() + "." + javaEnumReader.getModuleName() + "." + "enums;"); -// ctx.put("domain", javaEnumReader.getDomainName()); -// ctx.put("module", javaEnumReader.getModuleName()); -// ctx.put("annotation", javaEnumReader.getAnnotationList()); -// ctx.put("className", javaEnumReader.getClassName()); -// ctx.put("body", javaEnumReader.getBody()); -// ctx.put("tool", tool); -// -// File file = new File(enums_.getAbsolutePath() + File.separator + f.getName()); -// freeMarkerManager.outputTemp(file, "Java_api/module/enums/type.ftl", ctx); -// System.out.println("生成文件" + file.getName() + "成功"); -// } -// //endregion -// -// } catch (IOException e) { -// e.printStackTrace(); -// } -// } -// } - { System.out.println("生成模块:ApiController"); HashMap ctx = new HashMap(); ctx.put("tool", tool); - ctx.put("domain", domain); - ctx.put("module", module); + ctx.put("domain", javaClass.getDomainName()); + ctx.put("module", javaClass.getModuleName()); ctx.put("managerList", managerList); ctx.put("methodList", methodList); - File file = new File(frameWork.getAbsolutePath() + File.separator + module + File.separator + "ApiController.java"); + File file = new File(domain.getAbsolutePath() + File.separator + "ApiController.java"); freeMarkerManager.outputTemp(file, "Java_api/module/controller/ApiController.ftl", ctx); } - System.out.println("生成基础类"); - HashMap ctx = new HashMap(); - ctx.put("domain", domain); - ctx.put("module", module); - { - File file = new File(frameWork, "ApiEntity.java"); - freeMarkerManager.outputTemp(file, "/Java_api/ApiEntity.ftl", ctx); - } - { - File file = new File(frameWork, "AESUtil.java"); - freeMarkerManager.outputTemp(file, "/Java_api/AESUtil.ftl", ctx); - } - { - File file = new File(frameWork, "ApiClient.java"); - freeMarkerManager.outputTemp(file, "/Java_api/ApiClient.ftl", ctx); - } - { - File file = new File(frameWork, "ApiRequest.java"); - freeMarkerManager.outputTemp(file, "/Java_api/ApiRequest.ftl", ctx); - } - { - File file = new File(frameWork, "ApiFindRequest.java"); - freeMarkerManager.outputTemp(file, "/Java_api/ApiFindRequest.ftl", ctx); - } { - File file = new File(frameWork, "ApiSearchRequest.java"); - freeMarkerManager.outputTemp(file, "/Java_api/ApiSearchRequest.ftl", ctx); - } - { - File file = new File(frameWork, "ApiResponse.java"); - freeMarkerManager.outputTemp(file, "/Java_api/ApiResponse.ftl", ctx); - } - { - File file = new File(frameWork, "ApiFindResponse.java"); - freeMarkerManager.outputTemp(file, "/Java_api/ApiFindResponse.ftl", ctx); - } - { - File file = new File(frameWork, "Base64Util.java"); - freeMarkerManager.outputTemp(file, "/Java_api/Base64Util.ftl", ctx); - } - { - File file = new File(frameWork, "DownloadUtil.java"); - freeMarkerManager.outputTemp(file, "/Java_api/DownloadUtil.ftl", ctx); - } - { - File file = new File(frameWork, "Error.java"); - freeMarkerManager.outputTemp(file, "/Java_api/Error.ftl", ctx); - } - { - File file = new File(frameWork, "ErrorType.java"); - freeMarkerManager.outputTemp(file, "/Java_api/ErrorType.ftl", ctx); - } - { - File file = new File(frameWork, "FileUploadRequest.java"); - freeMarkerManager.outputTemp(file, "/Java_api/FileUploadRequest.ftl", ctx); - } - { - File file = new File(frameWork, "FileUploadResponse.java"); - freeMarkerManager.outputTemp(file, "/Java_api/FileUploadResponse.ftl", ctx); - } - { - File file = new File(frameWork, "MapperUtil.java"); - freeMarkerManager.outputTemp(file, "/Java_api/MapperUtil.ftl", ctx); - } - { - File file = new File(frameWork, "MD5Util.java"); - freeMarkerManager.outputTemp(file, "/Java_api/MD5Util.ftl", ctx); - } - { - File file = new File(frameWork, "RSAUtil.java"); - freeMarkerManager.outputTemp(file, "/Java_api/RSAUtil.ftl", ctx); - } - { - File file = new File(frameWork, "ProgressRequestBody.java"); - freeMarkerManager.outputTemp(file, "/Java_api/ProgressRequestBody.ftl", ctx); - } + System.out.println("生成基础类"); + HashMap ctx = new HashMap(); + ctx.put("domain", javaClass.getDomainName()); + ctx.put("module", javaClass.getModuleName()); + { + File file = new File(domain, "ApiEntity.java"); + freeMarkerManager.outputTemp(file, "/Java_api/ApiEntity.ftl", ctx); + } + { + File file = new File(domain, "AESUtil.java"); + freeMarkerManager.outputTemp(file, "/Java_api/AESUtil.ftl", ctx); + } + { + File file = new File(domain, "ApiClient.java"); + freeMarkerManager.outputTemp(file, "/Java_api/ApiClient.ftl", ctx); + } + { + File file = new File(domain, "ApiRequest.java"); + freeMarkerManager.outputTemp(file, "/Java_api/ApiRequest.ftl", ctx); + } + { + File file = new File(domain, "ApiFindRequest.java"); + freeMarkerManager.outputTemp(file, "/Java_api/ApiFindRequest.ftl", ctx); + } + { + File file = new File(domain, "ApiSearchRequest.java"); + freeMarkerManager.outputTemp(file, "/Java_api/ApiSearchRequest.ftl", ctx); + } + { + File file = new File(domain, "ApiResponse.java"); + freeMarkerManager.outputTemp(file, "/Java_api/ApiResponse.ftl", ctx); + } + { + File file = new File(domain, "ApiFindResponse.java"); + freeMarkerManager.outputTemp(file, "/Java_api/ApiFindResponse.ftl", ctx); + } + { + File file = new File(domain, "Base64Util.java"); + freeMarkerManager.outputTemp(file, "/Java_api/Base64Util.ftl", ctx); + } + { + File file = new File(domain, "DownloadUtil.java"); + freeMarkerManager.outputTemp(file, "/Java_api/DownloadUtil.ftl", ctx); + } + { + File file = new File(domain, "Error.java"); + freeMarkerManager.outputTemp(file, "/Java_api/Error.ftl", ctx); + } + { + File file = new File(domain, "ErrorType.java"); + freeMarkerManager.outputTemp(file, "/Java_api/ErrorType.ftl", ctx); + } + { + File file = new File(domain, "FileUploadRequest.java"); + freeMarkerManager.outputTemp(file, "/Java_api/FileUploadRequest.ftl", ctx); + } + { + File file = new File(domain, "FileUploadResponse.java"); + freeMarkerManager.outputTemp(file, "/Java_api/FileUploadResponse.ftl", ctx); + } + { + File file = new File(domain, "MapperUtil.java"); + freeMarkerManager.outputTemp(file, "/Java_api/MapperUtil.ftl", ctx); + } + { + File file = new File(domain, "MD5Util.java"); + freeMarkerManager.outputTemp(file, "/Java_api/MD5Util.ftl", ctx); + } + { + File file = new File(domain, "RSAUtil.java"); + freeMarkerManager.outputTemp(file, "/Java_api/RSAUtil.ftl", ctx); + } + { + File file = new File(domain, "ProgressRequestBody.java"); + freeMarkerManager.outputTemp(file, "/Java_api/ProgressRequestBody.ftl", ctx); + } - { - File file = new File(frameWork, "SortType.java"); - freeMarkerManager.outputTemp(file, "/Java_api/SortType.ftl", ctx); - } + { + File file = new File(domain, "SortType.java"); + freeMarkerManager.outputTemp(file, "/Java_api/SortType.ftl", ctx); + } - { - File file = new File(frameWork, "StringUtils.java"); - freeMarkerManager.outputTemp(file, "/Java_api/StringUtils.ftl", ctx); - } - //4.0.1 去掉验证 减少体积 + { + File file = new File(domain, "StringUtils.java"); + freeMarkerManager.outputTemp(file, "/Java_api/StringUtils.ftl", ctx); + } + + //4.0.1 去掉验证 减少体积 // { // File file = new File(frameWork , "ValidationUtil.java"); // freeMarkerManager.outputTemp(file, "/Java_api/ValidationUtil.ftl", ctx); // } + + } + + {//生成Test + System.out.println("生成模块:Test"); + HashMap ctx = new HashMap(); + ctx.put("tool", tool); + ctx.put("domain", javaClass.getDomainName()); + ctx.put("module", javaClass.getModuleName()); + ctx.put("managerList", managerList); + ctx.put("methodList", methodList); + freeMarkerManager.outputTemp(new File(testdomain.getAbsolutePath(), "ApiTest.java"), "Java_api/ApiTest.ftl", ctx); + } + System.out.println("finish"); return true; } diff --git a/src/main/resources/modules/Java_api/ApiClient.ftl b/src/main/resources/modules/Java_api/ApiClient.ftl index 4e4587c9..e968cdd2 100644 --- a/src/main/resources/modules/Java_api/ApiClient.ftl +++ b/src/main/resources/modules/Java_api/ApiClient.ftl @@ -1,7 +1,5 @@ package ${domain}; -import ${domain}.${module}.request.ApiExampleRequest; -import ${domain}.${module}.response.ApiExampleResponse; import okhttp3.*; import java.io.*; @@ -127,7 +125,7 @@ public class ApiClient { .add(P_APP_KEY, appKey) .add(P_METHOD, request.apiMethod()) .add(P_TYPE, TYPE_JSON) - .add(P_TARGET, MapperUtil.toJson(request)) + .add(P_TARGET, encode(request, isEnhanced)) .add(P_TIMESTAMP, currentTime) .add(P_SIGN, sign(request, currentTime)) .add(P_ENHANCED, String.valueOf(isEnhanced)) @@ -434,75 +432,4 @@ public class ApiClient { public void setDebug(boolean debug) { this.debug = debug; } - - public static void main(String[] args) { - - //实例化API请求客户端 - ApiClient.init("http://localhost:8080/api", "app_key", "1234567890123456"); - ApiClient client = ApiClient.getInstance(); - - // 以下为对称加密公钥,对重要关键请求加密,可不设 - String cryptPublicKeyBase64 = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCTrwfsrJjCF+pP4S3A/wrD4U1txg53EuBC1mPt" + - "3vGXvSK2U0YNRVR3Q65ooHnPKmk4LwI8v+7+ATTxUg3qkuRiDuzBa5zLkYKM50LOgEWSdOKzbnbx" + - "a5FnE7IXawNt1p8+MVN1TTI7J/fZy6g1x0WBy1odE5Osru4WfZNOqQtjHwIDAQAB"; - client.setPublicKey(RSAUtil.parsePublicKey(cryptPublicKeyBase64)); - - final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); - //设置发送网络请求前的统一操作 - client.setBefore(new Before() { - public void call(ApiRequest request) { - System.out.println("请求参数" + MapperUtil.toJson(request)); - System.out.println("请求方法" + request.apiMethod()); - } - }); - //设置网络请求完成后的统一操作 - client.setAfter(new After() { - public void call(ApiRequest request, ApiResponse response) { - System.out.println("响应参数" + MapperUtil.toJson(response)); - Date end = new Date(); - System.out.println(simpleDateFormat.format(end)); - } - }); - - //json请求 - ApiExampleRequest request = new ApiExampleRequest(); - client.execute(request, true, new ApiClient.Callback() { - public void call(ApiExampleResponse response) { - System.out.println(MapperUtil.toJson(response)); - } - }); - -// {//无进度显示 -// final Date start = new Date(); -// -// //文件上传请求 -// FileUploadRequest fileUploadRequest = new FileUploadRequest(); -// fileUploadRequest.setFile(new File("D:\\1.PNG")); -// client.fileUpload(fileUploadRequest, new Callback() { -// public void call(FileUploadResponse response) { -// Date end = new Date(); -// System.out.println("无进度显示" + (end.getTime() - start.getTime())); -// } -// }); -// } - -// {//有进度显示 -// final Date start = new Date(); -// -// //文件上传请求 -// FileUploadRequest fileUploadRequest = new FileUploadRequest(); -// fileUploadRequest.setFile(new File("D:\\1.PNG")); -// client.fileUpload(fileUploadRequest, new Callback() { -// public void call(FileUploadResponse response) { -// Date end = new Date(); -// System.out.println("有进度显示" + (end.getTime() - start.getTime())); -// } -// }, new ProgressRequestBody.ProgressListener() { -// public void onProgress(long totalBytes, long remainingBytes, boolean done) { -// System.out.println((totalBytes - remainingBytes) * 100 / totalBytes + "%"); -// } -// }); -// } - - } } diff --git a/src/main/resources/modules/Java_api/ApiTest.ftl b/src/main/resources/modules/Java_api/ApiTest.ftl new file mode 100644 index 00000000..ba99b167 --- /dev/null +++ b/src/main/resources/modules/Java_api/ApiTest.ftl @@ -0,0 +1,98 @@ +package ${domain}; + +import ${domain}.request.ApiExampleRequest; +import ${domain}.response.ApiExampleResponse; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * ApiTest - 测试用例 + * + * @author author + * @version 0.0.1 + * @since 2019-06-11 + */ +public class ApiTest { + + private ApiClient apiClient; + + @Before + public void before() { + //实例化API请求客户端 + ApiClient.init("http://localhost:8080/api", "app_key", "1234567890123456"); + apiClient = ApiClient.getInstance(); + ApiClient client = ApiClient.getInstance(); + + // 以下为对称加密公钥,对重要关键请求加密,可不设 + String cryptPublicKeyBase64 = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCTrwfsrJjCF+pP4S3A/wrD4U1txg53EuBC1mPt" + + "3vGXvSK2U0YNRVR3Q65ooHnPKmk4LwI8v+7+ATTxUg3qkuRiDuzBa5zLkYKM50LOgEWSdOKzbnbx" + + "a5FnE7IXawNt1p8+MVN1TTI7J/fZy6g1x0WBy1odE5Osru4WfZNOqQtjHwIDAQAB"; + client.setPublicKey(RSAUtil.parsePublicKey(cryptPublicKeyBase64)); + + final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); + //设置发送网络请求前的统一操作 + client.setBefore(new ApiClient.Before() { + public void call(ApiRequest request) { + System.out.println("请求参数" + MapperUtil.toJson(request)); + System.out.println("请求方法" + request.apiMethod()); + } + }); + //设置网络请求完成后的统一操作 + client.setAfter(new ApiClient.After() { + public void call(ApiRequest request, ApiResponse response) { + System.out.println("响应参数" + MapperUtil.toJson(response)); + Date end = new Date(); + System.out.println(simpleDateFormat.format(end)); + } + }); + + } + + @Test + public void testApiExample() { + ApiExampleRequest request = new ApiExampleRequest(); + ApiExampleResponse response = apiClient.execute(request); + Assert.assertTrue(!response.hasError()); + } + + @Test + public void testFileUpload() { + {//无进度显示 + final Date start = new Date(); + + //文件上传请求 + FileUploadRequest fileUploadRequest = new FileUploadRequest(); + fileUploadRequest.setFile(new File("D:\\1.PNG")); + apiClient.fileUpload(fileUploadRequest, new ApiClient.Callback() { + public void call(FileUploadResponse response) { + Date end = new Date(); + System.out.println("无进度显示" + (end.getTime() - start.getTime())); + } + }); + } + + {//有进度显示 + final Date start = new Date(); + + //文件上传请求 + FileUploadRequest fileUploadRequest = new FileUploadRequest(); + fileUploadRequest.setFile(new File("D:\\1.PNG")); + apiClient.fileUpload(fileUploadRequest, new ApiClient.Callback() { + public void call(FileUploadResponse response) { + Date end = new Date(); + System.out.println("有进度显示" + (end.getTime() - start.getTime())); + } + }, new ProgressRequestBody.ProgressListener() { + public void onProgress(long totalBytes, long remainingBytes, boolean done) { + System.out.println((totalBytes - remainingBytes) * 100 / totalBytes + "%"); + } + }); + } + } + +} diff --git a/src/main/resources/modules/Java_api/module/request/request.ftl b/src/main/resources/modules/Java_api/module/request/request.ftl index a6a58ddf..d974db51 100644 --- a/src/main/resources/modules/Java_api/module/request/request.ftl +++ b/src/main/resources/modules/Java_api/module/request/request.ftl @@ -1,11 +1,11 @@ -package ${domain}.${module}.request; +package ${domain}.request; <#list importList as i> <#if !i?contains("javax.validation") && !i?contains("org.hibernate.validator")> ${i} -import ${domain}.${module}.response.${className}Response; +import ${domain}.response.${className}Response; <#if findOrSearchflag == '0'> import ${domain}.ApiRequest; <#elseif findOrSearchflag=='1'> diff --git a/src/main/resources/modules/Java_api/module/response/response.ftl b/src/main/resources/modules/Java_api/module/response/response.ftl index 295c3c3c..d34fde97 100644 --- a/src/main/resources/modules/Java_api/module/response/response.ftl +++ b/src/main/resources/modules/Java_api/module/response/response.ftl @@ -1,4 +1,4 @@ -package ${domain}.${module}.response; +package ${domain}.response; <#list importList as i> ${i} diff --git a/src/main/resources/modules/Java_api/pom.ftl b/src/main/resources/modules/Java_api/pom.ftl index e6996883..e46ccc78 100644 --- a/src/main/resources/modules/Java_api/pom.ftl +++ b/src/main/resources/modules/Java_api/pom.ftl @@ -25,6 +25,12 @@ 3.4.1 + + junit + junit + 4.12 + + <#-- <#----> <#--org.hibernate.validator-->