├── .gitignore ├── README.md ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── jtool │ │ └── codegenbuilderplugin │ │ ├── BuilderMojo.java │ │ ├── classLoader │ │ ├── ClassLoaderInterface.java │ │ └── MavenPluginContextClassLoader.java │ │ ├── finder │ │ ├── ExceptionFinder.java │ │ ├── FileFinder.java │ │ └── MethodFinder.java │ │ ├── generator │ │ └── DocMdFormatGenerator.java │ │ ├── model │ │ ├── CodeGenModel.java │ │ ├── ExceptionModel.java │ │ └── ParamModel.java │ │ ├── parser │ │ └── MethodParser.java │ │ └── util │ │ ├── AnnotationHelper.java │ │ └── CurlExampleHelper.java └── resources │ └── template │ └── mdTemplate.md └── test └── java └── com └── jtool └── codegenbuilderplugin └── test ├── Tester.java ├── api ├── request │ ├── Add.java │ ├── AppFullAd.java │ ├── AppPopAd.java │ ├── BaseAdRequest.java │ ├── BaseCheckSdkPluginsRequest.java │ ├── BaseResponse.java │ ├── Channel.java │ ├── Check.java │ ├── CheckSdkPluginsItem.java │ ├── CheckSdkPluginsRequest.java │ ├── EnumTypeRequest.java │ ├── GetSdkConfigResponse.java │ ├── LoginRequest.java │ ├── PrePaymentRequest.java │ ├── QuerySingleRequest.java │ ├── SdkConfig.java │ ├── SdkPlugin.java │ ├── SearchUserApiRequest.java │ ├── Type.java │ └── UploadAvatarApiRequest.java └── response │ ├── Account.java │ ├── BaseResponse.java │ ├── BaseSdkPluginInfo.java │ ├── CheckSdkPluginsResponse.java │ ├── CommonResponse.java │ ├── GetSdkPluginsResponse.java │ ├── LoginResponse.java │ ├── OrderDetail.java │ ├── Pages.java │ ├── QuerySingleResponse.java │ ├── RoleCategoryEnum.java │ ├── RoleOperatorEnum.java │ ├── SdkPluginsResponseItem.java │ ├── SearchUserApiResponse.java │ ├── UploadAvatarApiResponse.java │ └── User.java ├── controller └── DemoController.java ├── exception ├── BackEndException.java ├── ExceptionTypeEnum.java └── ParamException.java ├── util └── TestContextClassLoader.java └── validate ├── Add.java ├── Get.java └── Modify.java /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.settings 3 | /.classpath 4 | /.project 5 | /.idea 6 | *.iml -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # codegen-builder-plugin 2 | 3 | 如果你正新开始一个服务器端API项目,使用的正是maven, Spring MVC技术,并使用JSON作为服务器返回的格式,那么codegen-builder-plugin将让你免费获得自动生成文档,自动生成Android客户端代码等功能。例子代码CodeGenDemo 4 | 5 | ##第一步 6 | 在项目pom.xml添加新的插件repository 7 | 8 | ```xml 9 | 10 | 11 | jtool-mvn-repository 12 | https://raw.github.com/JavaServerGroup/jtool-mvn-repository/master/releases 13 | 14 | 15 | jtool-mvn-snapshots 16 | https://raw.github.com/JavaServerGroup/jtool-mvn-snapshots/master/snapshots 17 | 18 | true 19 | 20 | 21 | 22 | ``` 23 | 24 | ##第二步 25 | 在项目pom.xml添加插件依赖。 26 | * scanBasePackage是需要扫描的包名; 27 | * projectName设置项目的名称,主要用户生成文档的标题和名字, 28 | ```xml 29 | 30 | 31 | 32 | com.jtool 33 | codegen-builder-plugin 34 | ${latest-releases} 35 | 36 | 自动代码生成demo 37 | com.jtool.codegendemo 38 | 39 | 40 | 41 | 42 | ``` 43 | 44 | ##第三步 45 | 在项目pom.xml添加生成代码注解的依赖: 46 | ```xml 47 | 48 | 49 | com.jtool 50 | codegen-annotation 51 | 0.0.1 52 | 53 | 54 | ``` 55 | 56 | ##第四步 57 | 为controller添加注解。 58 | ```java 59 | @CodeGenApi(name = "查找用户", description = "根据用户国家,年纪,身高,是否结婚等条件过滤查找用户") 60 | @CodeGenRequest(SearchUserApiRequest.class) 61 | @CodeGenResponse(SearchUserApiResponse.class) 62 | @RequestMapping(value = "/searchUser", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") 63 | @ResponseBody 64 | public String searchUser(SearchUserApiRequest searchUserApiRequest) throws ParamException, BackEndException { 65 | return JSON.toJSONString(searchUserApiResponse); 66 | } 67 | ``` 68 | 69 | ##第五步 70 | 为请求/返回pojo添加注解。 71 | ```java 72 | public class SearchUserApiRequest { 73 | @NotNull 74 | @Size(min = 1, max = 20) 75 | @CodeGenField("用户所在国家") 76 | private String country; 77 | 78 | @NotNull 79 | @Min(0) 80 | @Max(120) 81 | @Digits(integer = 3, fraction = 0) 82 | @CodeGenField("年龄") 83 | private Integer age; 84 | 85 | @NotNull 86 | @Min(0) 87 | @Max(250) 88 | @Digits(integer = 3, fraction = 2) 89 | @CodeGenField("身高") 90 | private Double height; 91 | 92 | @CodeGenField("是否已婚, 0代表没结婚,1代表结婚") 93 | @AvailableValues(values={"0", "1"}) 94 | private String isMarried; 95 | 96 | ...getter and setter and toString 97 | } 98 | ``` 99 | 100 | ##第六步 101 | 运行命令生成文档,文档输出在target下 102 | ```shell 103 | com.jtool:codegen-builder-plugin:build 104 | ``` 105 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.jtool 8 | codegen-builder-plugin 9 | 0.1.4 10 | maven-plugin 11 | 12 | 13 | 14 | 15 | org.apache.maven.plugins 16 | maven-compiler-plugin 17 | 3.7.0 18 | 19 | 1.8 20 | 1.8 21 | 1.8 22 | 23 | 24 | 25 | org.apache.maven.plugins 26 | maven-plugin-plugin 27 | 3.5.1 28 | 29 | 30 | 31 | 32 | 33 | 34 | org.apache.maven 35 | maven-plugin-api 36 | 3.5.2 37 | 38 | 39 | org.apache.maven 40 | maven-project 41 | 2.2.1 42 | 43 | 44 | org.apache.maven 45 | maven-core 46 | 3.5.2 47 | 48 | 49 | 50 | javax.servlet 51 | javax.servlet-api 52 | 3.1.0 53 | 54 | 55 | org.springframework 56 | spring-webmvc 57 | 5.1.0.RELEASE 58 | 59 | 60 | org.apache.commons 61 | commons-lang3 62 | 3.7 63 | 64 | 65 | commons-io 66 | commons-io 67 | 2.4 68 | 69 | 70 | com.alibaba 71 | fastjson 72 | 1.2.6 73 | 74 | 75 | 76 | com.jtool 77 | codegen-annotation 78 | 0.0.6 79 | 80 | 81 | 82 | org.freemarker 83 | freemarker 84 | 2.3.23 85 | 86 | 87 | com.squareup 88 | javapoet 89 | 1.4.0 90 | 91 | 92 | com.jtool 93 | jtool-validation 94 | 0.0.5 95 | 96 | 97 | 98 | junit 99 | junit 100 | 4.12 101 | test 102 | 103 | 104 | 105 | 106 | 107 | 108 | springsource-repo 109 | SpringSource Repository 110 | http://repo.springsource.org/release 111 | 112 | 113 | spring-snapshots 114 | Spring Snapshots 115 | http://repo.spring.io/libs-snapshot 116 | 117 | true 118 | 119 | 120 | 121 | jtool-mvn-repository 122 | https://raw.github.com/JavaServerGroup/jtool-mvn-repository/master/releases 123 | 124 | 125 | jtool-mvn-snapshots 126 | https://raw.github.com/JavaServerGroup/jtool-mvn-snapshots/master/snapshots 127 | 128 | true 129 | 130 | 131 | 132 | 133 | 134 | 135 | jtool-maven-repository 136 | Internal Repository 137 | file://${project.basedir}/../jtool-mvn-repository/releases 138 | 139 | 140 | 141 | -------------------------------------------------------------------------------- /src/main/java/com/jtool/codegenbuilderplugin/BuilderMojo.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.jtool.codegenbuilderplugin.classLoader.ClassLoaderInterface; 5 | import com.jtool.codegenbuilderplugin.classLoader.MavenPluginContextClassLoader; 6 | import com.jtool.codegenbuilderplugin.finder.ExceptionFinder; 7 | import com.jtool.codegenbuilderplugin.finder.FileFinder; 8 | import com.jtool.codegenbuilderplugin.finder.MethodFinder; 9 | import com.jtool.codegenbuilderplugin.generator.DocMdFormatGenerator; 10 | import com.jtool.codegenbuilderplugin.model.CodeGenModel; 11 | import com.jtool.codegenbuilderplugin.model.ExceptionModel; 12 | import com.jtool.codegenbuilderplugin.parser.MethodParser; 13 | import org.apache.maven.artifact.DependencyResolutionRequiredException; 14 | import org.apache.maven.plugin.AbstractMojo; 15 | import org.apache.maven.plugin.MojoExecutionException; 16 | import org.apache.maven.plugin.MojoFailureException; 17 | import org.apache.maven.project.MavenProject; 18 | 19 | import java.io.File; 20 | import java.io.IOException; 21 | import java.lang.reflect.Method; 22 | import java.util.Collections; 23 | import java.util.List; 24 | 25 | /** 26 | * @goal build 27 | * @requiresDependencyResolution runtime 28 | */ 29 | public class BuilderMojo extends AbstractMojo { 30 | 31 | /** 32 | * @parameter property = "project.basedir" 33 | * @required 34 | * @readonly 35 | */ 36 | private File basedir; 37 | 38 | /** 39 | * @parameter 40 | * @required 41 | */ 42 | private String scanBasePackage; 43 | 44 | /** 45 | * @parameter 46 | */ 47 | private String projectName = "CodeGenDoc"; 48 | 49 | /** 50 | * @parameter property = "project" 51 | * @required 52 | * @readonly 53 | */ 54 | private MavenProject project; 55 | 56 | /** 57 | * @parameter default-value = "${project.build.directory}/" 58 | */ 59 | private String outPath; 60 | 61 | private ClassLoaderInterface classLoaderInterface; 62 | 63 | private String scanSource = "/src/main/java/"; 64 | 65 | public void execute() throws MojoExecutionException, MojoFailureException { 66 | 67 | //初始化classesLoader 68 | initClassesLoader(); 69 | 70 | //递归找出需要扫描的file 71 | List files = FileFinder.findAllFileNeedToParse(this); 72 | 73 | //遍历带有@CodeGenApi注解的方法 74 | List methodLists = MethodFinder.findAllCodeGenApiMethod(this, files); 75 | 76 | //遍历所有定义的Exception 77 | List exceptionModels = ExceptionFinder.findAllCodeGenException(this, files); 78 | 79 | Collections.sort(exceptionModels); 80 | 81 | //检查是否有一样错误码的异常 82 | checkDuplicateExceptionCodeDefine(exceptionModels); 83 | 84 | //遍历method集合,解析出可用的apiModel对象集合 85 | List codeGenModelList; 86 | try { 87 | codeGenModelList = MethodParser.parseMethodToCodeGenModel(this, methodLists); 88 | } catch (ClassNotFoundException | IOException | InstantiationException | IllegalAccessException e) { 89 | e.printStackTrace(); 90 | return; 91 | } 92 | 93 | this.getLog().debug(JSON.toJSONString(codeGenModelList)); 94 | 95 | //生成md文件 96 | DocMdFormatGenerator.genMdDoc(this, codeGenModelList, exceptionModels); 97 | 98 | } 99 | 100 | private void checkDuplicateExceptionCodeDefine(List exceptionModels) { 101 | if(exceptionModels.size() == 1) { 102 | return; 103 | } 104 | for(int i = 0; i < exceptionModels.size(); i++) { 105 | for(int j = i + 1; j < exceptionModels.size(); j++) { 106 | if( exceptionModels.get(i).getCode().equals(exceptionModels.get(j).getCode())) { 107 | throw new RuntimeException("一个项目里面不应该定义两个异常是有相同错误码的: " + exceptionModels.get(i).getCode()); 108 | } 109 | } 110 | } 111 | } 112 | 113 | private void initClassesLoader() { 114 | if (classLoaderInterface == null) { 115 | try { 116 | classLoaderInterface = new MavenPluginContextClassLoader(project); 117 | } catch (DependencyResolutionRequiredException e) { 118 | e.printStackTrace(); 119 | } 120 | } 121 | } 122 | 123 | public String getOutPath() { 124 | return outPath; 125 | } 126 | 127 | public String getScanBasePackage() { 128 | return scanBasePackage; 129 | } 130 | 131 | public void setScanBasePackage(String scanBasePackage) { 132 | this.scanBasePackage = scanBasePackage; 133 | } 134 | 135 | public MavenProject getProject() { 136 | return project; 137 | } 138 | 139 | public void setProject(MavenProject project) { 140 | this.project = project; 141 | } 142 | 143 | public void setOutPath(String outPath) { 144 | this.outPath = outPath; 145 | } 146 | 147 | public ClassLoaderInterface getClassLoaderInterface() { 148 | return classLoaderInterface; 149 | } 150 | 151 | public void setClassLoaderInterface(ClassLoaderInterface classLoaderInterface) { 152 | this.classLoaderInterface = classLoaderInterface; 153 | } 154 | 155 | public File getBasedir() { 156 | return basedir; 157 | } 158 | 159 | public void setBasedir(File basedir) { 160 | this.basedir = basedir; 161 | } 162 | 163 | public String getScanSource() { 164 | return scanSource; 165 | } 166 | 167 | public void setScanSource(String scanSource) { 168 | this.scanSource = scanSource; 169 | } 170 | 171 | public String getProjectName() { 172 | return projectName; 173 | } 174 | 175 | public void setProjectName(String projectName) { 176 | this.projectName = projectName; 177 | } 178 | 179 | } 180 | -------------------------------------------------------------------------------- /src/main/java/com/jtool/codegenbuilderplugin/classLoader/ClassLoaderInterface.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.classLoader; 2 | 3 | public interface ClassLoaderInterface { 4 | public Class loadClass(String name) throws ClassNotFoundException; 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/jtool/codegenbuilderplugin/classLoader/MavenPluginContextClassLoader.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.classLoader; 2 | 3 | import org.apache.maven.artifact.DependencyResolutionRequiredException; 4 | import org.apache.maven.project.MavenProject; 5 | 6 | import java.io.File; 7 | import java.net.MalformedURLException; 8 | import java.net.URL; 9 | import java.net.URLClassLoader; 10 | import java.util.List; 11 | 12 | /** 13 | * Created by jialechan on 15/6/24. 14 | */ 15 | public class MavenPluginContextClassLoader implements ClassLoaderInterface { 16 | 17 | private URLClassLoader newLoader; 18 | 19 | public MavenPluginContextClassLoader(MavenProject project) throws DependencyResolutionRequiredException { 20 | List runtimeClasspathElements = project.getRuntimeClasspathElements(); 21 | URL[] runtimeUrls = new URL[runtimeClasspathElements.size()]; 22 | for (int i = 0; i < runtimeClasspathElements.size(); i++) { 23 | String element = (String) runtimeClasspathElements.get(i); 24 | try { 25 | runtimeUrls[i] = new File(element).toURI().toURL(); 26 | } catch (MalformedURLException e) { 27 | e.printStackTrace(); 28 | } 29 | } 30 | newLoader = new URLClassLoader(runtimeUrls, 31 | Thread.currentThread().getContextClassLoader()); 32 | } 33 | 34 | public Class loadClass(String name) throws ClassNotFoundException { 35 | return newLoader.loadClass(name); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/jtool/codegenbuilderplugin/finder/ExceptionFinder.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.finder; 2 | 3 | import com.jtool.codegenannotation.CodeGenExceptionDefine; 4 | import com.jtool.codegenannotation.CodeGenExceptionTypeEnum; 5 | import com.jtool.codegenbuilderplugin.BuilderMojo; 6 | import com.jtool.codegenbuilderplugin.model.ExceptionModel; 7 | 8 | import java.io.File; 9 | import java.lang.reflect.InvocationTargetException; 10 | import java.lang.reflect.Method; 11 | import java.util.ArrayList; 12 | import java.util.LinkedHashMap; 13 | import java.util.List; 14 | 15 | public class ExceptionFinder { 16 | 17 | public static List findAllCodeGenException(BuilderMojo builderMojo, List files) { 18 | 19 | List result = new ArrayList<>(); 20 | 21 | for (File file : files) { 22 | result.addAll(findCodeGenExceptionFromFile(builderMojo, file)); 23 | } 24 | 25 | return result; 26 | } 27 | 28 | private static List findCodeGenExceptionFromFile(BuilderMojo builderMojo, File file) { 29 | 30 | List result = new ArrayList<>(); 31 | 32 | if (!file.getAbsolutePath().endsWith(".java")) { 33 | return result; 34 | } 35 | 36 | builderMojo.getLog().debug("准备分析的文件:" + file.getAbsolutePath()); 37 | 38 | String className = file.getAbsolutePath().replace(builderMojo.getBasedir().getAbsolutePath() + builderMojo.getScanSource(), "").replace(".java", "").replaceAll(File.separator, "."); 39 | 40 | Class clazz; 41 | try { 42 | clazz = builderMojo.getClassLoaderInterface().loadClass(className); 43 | } catch (ClassNotFoundException e) { 44 | throw new RuntimeException("没有找到类:" + className); 45 | } 46 | 47 | CodeGenExceptionTypeEnum codeGenExceptionTypeEnum = clazz.getAnnotation(CodeGenExceptionTypeEnum.class); 48 | if(codeGenExceptionTypeEnum != null) { 49 | try { 50 | Method getCode = clazz.getMethod("getCode"); 51 | Method getDesc = clazz.getMethod("getDesc"); 52 | //得到enum的所有实例 53 | for (Object obj : clazz.getEnumConstants()) { 54 | 55 | ExceptionModel exceptionModel = new ExceptionModel(); 56 | exceptionModel.setCode(Integer.valueOf((String)getCode.invoke(obj))); 57 | exceptionModel.setDesc((String)getDesc.invoke(obj)); 58 | 59 | result.add(exceptionModel); 60 | 61 | } 62 | } catch (NoSuchMethodException e) { 63 | e.printStackTrace(); 64 | } catch (InvocationTargetException e) { 65 | e.printStackTrace(); 66 | } catch (IllegalAccessException e) { 67 | e.printStackTrace(); 68 | } 69 | 70 | 71 | 72 | } 73 | 74 | return result; 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/jtool/codegenbuilderplugin/finder/FileFinder.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.finder; 2 | 3 | import com.jtool.codegenbuilderplugin.BuilderMojo; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import java.io.File; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | public class FileFinder { 11 | 12 | public static List findAllFileNeedToParse(BuilderMojo builderMojo) { 13 | 14 | //检查scanBasePackageStr, 必须且不为空。 15 | if (StringUtils.isBlank(builderMojo.getScanBasePackage())) { 16 | throw new RuntimeException("设置扫描的路径"); 17 | } else { 18 | builderMojo.setScanBasePackage(builderMojo.getScanBasePackage().trim()); 19 | } 20 | 21 | return findAllFileNeedToParse(makeScanPackageFile(builderMojo)); 22 | } 23 | 24 | //递归找出所有需要分析的文件(.java结尾的文件) 25 | private static List findAllFileNeedToParse(File file) { 26 | 27 | List result = new ArrayList<>(); 28 | 29 | File[] files = file.listFiles(); 30 | 31 | if (files != null && files.length > 0) { 32 | for (File fileItem : files) { 33 | if (fileItem.isDirectory()) { 34 | result.addAll(findAllFileNeedToParse(fileItem)); 35 | } else if (fileItem.getAbsolutePath().endsWith(".java")) { 36 | result.add(fileItem); 37 | } 38 | } 39 | } 40 | 41 | return result; 42 | } 43 | 44 | //生成需要递归扫描的路径对应的File实例 45 | public static File makeScanPackageFile(BuilderMojo builderMojo) { 46 | String scanBasePackagePath = makePackagePath(builderMojo.getScanBasePackage()); 47 | File scanBasePackageFile = new File(builderMojo.getBasedir().getAbsolutePath() + builderMojo.getScanSource() + scanBasePackagePath); 48 | builderMojo.getLog().debug("准备扫描的路径:" + scanBasePackageFile.getAbsolutePath()); 49 | if (!scanBasePackageFile.exists()) { 50 | throw new RuntimeException("设置的扫描路径有误:" + scanBasePackageFile.getAbsolutePath()); 51 | } 52 | return scanBasePackageFile; 53 | } 54 | 55 | //将包名转化为路径 56 | private static String makePackagePath(String packageName) { 57 | return String.join("/", packageName.split("\\.")); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/jtool/codegenbuilderplugin/finder/MethodFinder.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.finder; 2 | 3 | import com.jtool.codegenannotation.CodeGenApi; 4 | import com.jtool.codegenbuilderplugin.BuilderMojo; 5 | 6 | import java.io.File; 7 | import java.lang.annotation.Annotation; 8 | import java.lang.reflect.Method; 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | public class MethodFinder { 13 | 14 | public static List findAllCodeGenApiMethod(BuilderMojo builderMojo, List files) { 15 | 16 | List methodList = new ArrayList<>(); 17 | 18 | for (File file : files) { 19 | methodList.addAll(findMethodWithCodeGenApiFromFile(builderMojo, file)); 20 | } 21 | 22 | return methodList; 23 | } 24 | 25 | private static List findMethodWithCodeGenApiFromFile(BuilderMojo builderMojo, File file) { 26 | 27 | List result = new ArrayList<>(); 28 | 29 | if (!file.getAbsolutePath().endsWith(".java")) { 30 | return result; 31 | } 32 | 33 | builderMojo.getLog().debug("准备分析的文件:" + file.getAbsolutePath()); 34 | 35 | String className = file.getAbsolutePath().replace(builderMojo.getBasedir().getAbsolutePath() + builderMojo.getScanSource(), "").replace(".java", "").replaceAll(File.separator, "."); 36 | 37 | Class clazz; 38 | try { 39 | clazz = builderMojo.getClassLoaderInterface().loadClass(className); 40 | } catch (ClassNotFoundException e) { 41 | throw new RuntimeException("没有找到类:" + className); 42 | } 43 | 44 | //遍历源文件的method 45 | for (Method method : clazz.getDeclaredMethods()) { 46 | for (Annotation annotation : method.getAnnotations()) { 47 | // 只保留有@CodeGenApi注解的方法 48 | if (annotation instanceof CodeGenApi) { 49 | result.add(method); 50 | } 51 | } 52 | } 53 | 54 | return result; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/jtool/codegenbuilderplugin/generator/DocMdFormatGenerator.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.generator; 2 | 3 | import com.jtool.codegenbuilderplugin.BuilderMojo; 4 | import com.jtool.codegenbuilderplugin.model.CodeGenModel; 5 | import com.jtool.codegenbuilderplugin.model.ExceptionModel; 6 | import com.jtool.codegenbuilderplugin.model.ParamModel; 7 | import freemarker.ext.beans.StringModel; 8 | import freemarker.template.*; 9 | 10 | import java.io.*; 11 | import java.util.*; 12 | 13 | public class DocMdFormatGenerator { 14 | 15 | public static void genMdDoc(BuilderMojo builderMojo, List codeGenModelList, List exceptionModels) { 16 | Map> codeGenModelMapByForWho = groupCodeGenModelListByFowWho(codeGenModelList); 17 | for(Map.Entry> entry : codeGenModelMapByForWho.entrySet()) { 18 | genEachMdDoc(builderMojo, entry.getKey(), entry.getValue(), exceptionModels); 19 | } 20 | } 21 | 22 | private static void genEachMdDoc(BuilderMojo builderMojo, String forWho, List codeGenModelList, List exceptionModels) { 23 | 24 | //组装数据 25 | Map root = new HashMap<>(); 26 | root.put("apiModelList", codeGenModelList); 27 | root.put("exceptionModels", exceptionModels); 28 | root.put("projectName", builderMojo.getProjectName()); 29 | root.put("hasRequestParams", true);//是否有请求参数 30 | 31 | Configuration cfg = new Configuration(Configuration.VERSION_2_3_23); 32 | cfg.setClassForTemplateLoading(builderMojo.getClass(), "/template"); 33 | cfg.setDefaultEncoding("UTF-8"); 34 | cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); 35 | cfg.setSharedVariable("successReturnJson", new MdSuccessReturnJsonGenerator()); 36 | cfg.setSharedVariable("returnParamDetail", new MdReturnParamDetailGenerator()); 37 | cfg.setSharedVariable("isMaxDouble", new IsMaxDouble()); 38 | 39 | try { 40 | Template docFile = cfg.getTemplate("mdTemplate.md"); 41 | String fileName; 42 | if("default".equals(forWho)) { 43 | fileName = builderMojo.getOutPath() + "/" + builderMojo.getProjectName() + "_md_.md"; 44 | } else { 45 | fileName = builderMojo.getOutPath() + "/" + builderMojo.getProjectName() + "_" + forWho + "_md_.md"; 46 | } 47 | Writer out = new FileWriter(new File(fileName)); 48 | docFile.process(root, out); 49 | } catch (TemplateException | IOException e) { 50 | e.printStackTrace(); 51 | } 52 | } 53 | 54 | //根据forWho属性,将list分组成一个以forWho为key,以相应相同forWho组成的list为值的map 55 | private static Map> groupCodeGenModelListByFowWho(List codeGenModelList) { 56 | Map> result = new HashMap<>(); 57 | for(CodeGenModel codeGenModel : codeGenModelList) { 58 | List codeGenModelFromMap = result.get(codeGenModel.getForWho()); 59 | if(codeGenModelFromMap == null) {//如果指定forWho没找到list,就要新建一个并把codeGenModel加到这个list,最后放到map里面 60 | codeGenModelFromMap = new ArrayList<>(); 61 | } 62 | codeGenModelFromMap.add(codeGenModel); 63 | result.put(codeGenModel.getForWho(), codeGenModelFromMap); 64 | } 65 | return result; 66 | } 67 | } 68 | 69 | class MdReturnParamDetailGenerator implements TemplateMethodModelEx { 70 | 71 | @Override 72 | public Object exec(List list) throws TemplateModelException { 73 | ParamModel responseParamModel = (ParamModel)(((StringModel)list.get(0)).getWrappedObject()); 74 | return generator(responseParamModel, 0); 75 | } 76 | 77 | private String generator(ParamModel responseParamModel, int level) { 78 | String result = ""; 79 | for(int i = 0; i < level; i++) { 80 | result += System.getProperties().get("line.separator") + "    "; 81 | } 82 | result += responseParamModel.getKey(); 83 | result += ""; 84 | 85 | result += responseParamModel.isRequired() ? "必须" : "可选"; 86 | result += ""; 87 | 88 | if(responseParamModel.getConstraintStr() == null || responseParamModel.getConstraintStr().isEmpty()) { 89 | result += " --- "; 90 | } else { 91 | for (String constraintStr : responseParamModel.getConstraintStr()) { 92 | result += constraintStr + "
"; 93 | } 94 | } 95 | result += ""; 96 | 97 | result += responseParamModel.getComment(); 98 | result += ""; 99 | 100 | if(responseParamModel.getSubParamModel() != null && responseParamModel.getSubParamModel().size() > 0) { 101 | level++; 102 | for(ParamModel subResponseParamModel : responseParamModel.getSubParamModel()) { 103 | result += generator(subResponseParamModel, level); 104 | } 105 | } 106 | 107 | return result + ""; 108 | } 109 | 110 | } 111 | 112 | class MdSuccessReturnJsonGenerator implements TemplateMethodModelEx { 113 | 114 | @Override 115 | public Object exec(List list) throws TemplateModelException { 116 | 117 | try { 118 | if (list != null && list.size() == 1) { 119 | return list.get(0).toString(); 120 | } else { 121 | return " --- "; 122 | } 123 | } catch (NullPointerException e) { 124 | return " --- "; 125 | } 126 | 127 | } 128 | } 129 | 130 | class IsMaxDouble implements TemplateMethodModelEx { 131 | @Override 132 | public Object exec(List list) throws TemplateModelException { 133 | Double d = (Double)(((SimpleNumber)list.get(0)).getAsNumber()); 134 | return d == Double.MAX_VALUE; 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /src/main/java/com/jtool/codegenbuilderplugin/model/CodeGenModel.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.model; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | 5 | import java.util.List; 6 | import java.util.Optional; 7 | 8 | public class CodeGenModel implements Comparable { 9 | 10 | private Double docSeq; 11 | private String apiName; 12 | private String apiMethodName; 13 | private String description; 14 | private String url; 15 | private String forWho; 16 | private String httpMethod; 17 | private List requestParamModelList; 18 | private List responseParamModelList; 19 | private String responseDIY; 20 | private String curlExample; 21 | private Optional requestClass; 22 | private Optional responseClass; 23 | private Class[] requestGroups; 24 | private Class[] responseGroups; 25 | 26 | private String successReturnJsonStr; 27 | private String remark; 28 | private boolean isDeprecated; 29 | 30 | private boolean isGenSDK; 31 | 32 | private boolean isRest; 33 | 34 | public boolean isRest() { 35 | return isRest; 36 | } 37 | 38 | public void setRest(boolean rest) { 39 | isRest = rest; 40 | } 41 | 42 | public Optional getRequestClass() { 43 | return requestClass; 44 | } 45 | 46 | public void setRequestClass(Optional requestClass) { 47 | this.requestClass = requestClass; 48 | } 49 | 50 | @Override 51 | public int compareTo(CodeGenModel o) { 52 | return this.getDocSeq().compareTo(o.getDocSeq()); 53 | } 54 | 55 | public Double getDocSeq() { 56 | return docSeq; 57 | } 58 | 59 | public void setDocSeq(Double docSeq) { 60 | this.docSeq = docSeq; 61 | } 62 | 63 | public String getApiName() { 64 | return apiName; 65 | } 66 | 67 | public void setApiName(String apiName) { 68 | this.apiName = apiName; 69 | } 70 | 71 | public String getDescription() { 72 | return description; 73 | } 74 | 75 | public void setDescription(String description) { 76 | this.description = description; 77 | } 78 | 79 | public String getUrl() { 80 | return url; 81 | } 82 | 83 | public void setUrl(String url) { 84 | this.url = url; 85 | } 86 | 87 | public String getHttpMethod() { 88 | return httpMethod; 89 | } 90 | 91 | public void setHttpMethod(String httpMethod) { 92 | this.httpMethod = httpMethod; 93 | } 94 | 95 | public String getSuccessReturnJsonStr() { 96 | return successReturnJsonStr; 97 | } 98 | 99 | public void setSuccessReturnJsonStr(String successReturnJsonStr) { 100 | this.successReturnJsonStr = successReturnJsonStr; 101 | } 102 | 103 | public String getRemark() { 104 | return remark; 105 | } 106 | 107 | public void setRemark(String remark) { 108 | this.remark = remark; 109 | } 110 | 111 | public boolean isDeprecated() { 112 | return isDeprecated; 113 | } 114 | 115 | public void setIsDeprecated(boolean isDeprecated) { 116 | this.isDeprecated = isDeprecated; 117 | } 118 | 119 | public List getRequestParamModelList() { 120 | return requestParamModelList; 121 | } 122 | 123 | public void setRequestParamModelList(List requestParamModelList) { 124 | this.requestParamModelList = requestParamModelList; 125 | } 126 | 127 | public List getResponseParamModelList() { 128 | return responseParamModelList; 129 | } 130 | 131 | public void setResponseParamModelList(List responseParamModelList) { 132 | this.responseParamModelList = responseParamModelList; 133 | } 134 | 135 | public String getForWho() { 136 | return forWho; 137 | } 138 | 139 | public void setForWho(String forWho) { 140 | this.forWho = forWho; 141 | } 142 | 143 | public boolean isGenSDK() { 144 | return isGenSDK; 145 | } 146 | 147 | public void setIsGenSDK(boolean isGenSDK) { 148 | this.isGenSDK = isGenSDK; 149 | } 150 | 151 | public String getApiMethodName() { 152 | return apiMethodName; 153 | } 154 | 155 | public void setApiMethodName(String apiMethodName) { 156 | this.apiMethodName = apiMethodName; 157 | } 158 | 159 | public String getResponseDIY() { 160 | return responseDIY; 161 | } 162 | 163 | public void setResponseDIY(String responseDIY) { 164 | this.responseDIY = responseDIY; 165 | } 166 | 167 | public String getCurlExample() { 168 | return curlExample; 169 | } 170 | 171 | public void setCurlExample(String curlExample) { 172 | this.curlExample = curlExample; 173 | } 174 | 175 | public Optional getResponseClass() { 176 | return responseClass; 177 | } 178 | 179 | public void setResponseClass(Optional responseClass) { 180 | this.responseClass = responseClass; 181 | } 182 | 183 | public Class[] getRequestGroups() { 184 | return requestGroups; 185 | } 186 | 187 | public void setRequestGroups(Class[] requestGroups) { 188 | this.requestGroups = requestGroups; 189 | } 190 | 191 | public Class[] getResponseGroups() { 192 | return responseGroups; 193 | } 194 | 195 | public void setResponseGroups(Class[] responseGroups) { 196 | this.responseGroups = responseGroups; 197 | } 198 | 199 | @Override 200 | public String toString() { 201 | return JSON.toJSONString(this); 202 | } 203 | } -------------------------------------------------------------------------------- /src/main/java/com/jtool/codegenbuilderplugin/model/ExceptionModel.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.model; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | 5 | public class ExceptionModel implements Comparable { 6 | 7 | private Integer code; 8 | private String desc; 9 | 10 | @Override 11 | public int compareTo(ExceptionModel o) { 12 | return o.getCode().compareTo(this.getCode()); 13 | } 14 | 15 | public Integer getCode() { 16 | return code; 17 | } 18 | 19 | public void setCode(Integer code) { 20 | this.code = code; 21 | } 22 | 23 | public String getDesc() { 24 | return desc; 25 | } 26 | 27 | public void setDesc(String desc) { 28 | this.desc = desc; 29 | } 30 | 31 | @Override 32 | public String toString() { 33 | return JSON.toJSONString(this); 34 | } 35 | } -------------------------------------------------------------------------------- /src/main/java/com/jtool/codegenbuilderplugin/model/ParamModel.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.model; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | public class ParamModel implements Comparable { 9 | 10 | protected String key; 11 | protected boolean required; 12 | protected List constraintStr; 13 | protected String comment; 14 | protected String type; 15 | protected List subParamModel = new ArrayList<>(); 16 | 17 | public String getKey() { 18 | return key; 19 | } 20 | 21 | public void setKey(String key) { 22 | this.key = key; 23 | } 24 | 25 | public boolean isRequired() { 26 | return required; 27 | } 28 | 29 | public void setRequired(boolean required) { 30 | this.required = required; 31 | } 32 | 33 | public List getConstraintStr() { 34 | return constraintStr; 35 | } 36 | 37 | public void setConstraintStr(List constraintStr) { 38 | this.constraintStr = constraintStr; 39 | } 40 | 41 | public String getComment() { 42 | return comment; 43 | } 44 | 45 | public void setComment(String comment) { 46 | this.comment = comment; 47 | } 48 | 49 | public String getType() { 50 | return type; 51 | } 52 | 53 | public void setType(String type) { 54 | this.type = type; 55 | } 56 | 57 | public List getSubParamModel() { 58 | return subParamModel; 59 | } 60 | 61 | public void setSubParamModel(List subParamModel) { 62 | this.subParamModel = subParamModel; 63 | } 64 | 65 | @Override 66 | public String toString() { 67 | return JSON.toJSONString(this); 68 | } 69 | 70 | @Override 71 | public int compareTo(ParamModel o) { 72 | return this.getKey().compareTo(o.getKey()); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/jtool/codegenbuilderplugin/parser/MethodParser.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.parser; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.serializer.SerializerFeature; 5 | import com.jtool.codegenannotation.CodeGenApi; 6 | import com.jtool.codegenannotation.CodeGenField; 7 | import com.jtool.codegenannotation.CodeGenRequest; 8 | import com.jtool.codegenannotation.CodeGenResponse; 9 | import com.jtool.codegenbuilderplugin.BuilderMojo; 10 | import com.jtool.codegenbuilderplugin.model.CodeGenModel; 11 | import com.jtool.codegenbuilderplugin.model.ParamModel; 12 | import org.apache.commons.lang3.StringUtils; 13 | import org.apache.commons.lang3.reflect.FieldUtils; 14 | import org.springframework.web.bind.annotation.GetMapping; 15 | import org.springframework.web.bind.annotation.PostMapping; 16 | import org.springframework.web.bind.annotation.RequestMapping; 17 | import org.springframework.web.bind.annotation.RequestMethod; 18 | 19 | import javax.validation.constraints.NotBlank; 20 | import javax.validation.constraints.NotEmpty; 21 | import javax.validation.constraints.NotNull; 22 | import java.io.IOException; 23 | import java.lang.reflect.Field; 24 | import java.lang.reflect.Method; 25 | import java.lang.reflect.ParameterizedType; 26 | import java.lang.reflect.Type; 27 | import java.util.*; 28 | 29 | import static com.jtool.codegenbuilderplugin.util.AnnotationHelper.constrainAnnotationToStr; 30 | import static com.jtool.codegenbuilderplugin.util.AnnotationHelper.hasAnnotation; 31 | import static com.jtool.codegenbuilderplugin.util.CurlExampleHelper.parseCurlExample; 32 | 33 | public class MethodParser { 34 | 35 | public static List parseMethodToCodeGenModel(BuilderMojo builderMojo, List methodLists) throws ClassNotFoundException, IOException, InstantiationException, IllegalAccessException { 36 | 37 | List result = new ArrayList<>(); 38 | 39 | for (Method method : methodLists) { 40 | result.add(parse(builderMojo, method)); 41 | } 42 | 43 | Collections.sort(result); 44 | 45 | return result; 46 | } 47 | 48 | private static CodeGenModel parse(BuilderMojo builderMojo, Method method) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException { 49 | 50 | if (!hasAnnotation(method, CodeGenApi.class)) { 51 | throw new RuntimeException("本方法应该有@CodeGenApi注解才对"); 52 | } 53 | 54 | final CodeGenModel codeGenModel = new CodeGenModel(); 55 | 56 | //分析排序的序号 57 | codeGenModel.setDocSeq(method.getAnnotation(CodeGenApi.class).docSeq()); 58 | 59 | //分析api的名字 60 | codeGenModel.setApiName(method.getAnnotation(CodeGenApi.class).name()); 61 | 62 | //分析说明 63 | codeGenModel.setDescription(method.getAnnotation(CodeGenApi.class).description()); 64 | 65 | //分析http的方法:GET或者POST 66 | codeGenModel.setHttpMethod(paresHttpMethod(method)); 67 | 68 | //分析备注内容 69 | codeGenModel.setRemark(method.getAnnotation(CodeGenApi.class).remark()); 70 | 71 | //分析API的路径值 72 | codeGenModel.setUrl(paresUrl(method)); 73 | 74 | //分析request的class的参数列表 75 | codeGenModel.setRequestClass(parseRequestClass(method)); 76 | codeGenModel.setRequestGroups(parseRequestGroups(method)); 77 | 78 | //分析response的class的参数列表 79 | codeGenModel.setResponseClass(parseResponseClass(method)); 80 | codeGenModel.setResponseGroups(parseResponseGroups(method)); 81 | 82 | //分析是否rest 83 | codeGenModel.setRest(parseIsRest(method)); 84 | 85 | //分析是否弃用的方法 86 | codeGenModel.setIsDeprecated(hasAnnotation(method, Deprecated.class)); 87 | 88 | //分析接口是给谁用的 89 | codeGenModel.setForWho(method.getAnnotation(CodeGenApi.class).forWho()); 90 | 91 | //分析是否生成SDK 92 | codeGenModel.setIsGenSDK(method.getAnnotation(CodeGenApi.class).genSDK()); 93 | 94 | //分析这个方法的名称 95 | codeGenModel.setApiMethodName(method.getName()); 96 | 97 | //分析成功请求返回的字符串 98 | codeGenModel.setSuccessReturnJsonStr(parseSuccessReturnString(builderMojo, codeGenModel)); 99 | 100 | //分析request的pojo的参数列表 和 分析path param的参数列表 101 | codeGenModel.setRequestParamModelList(parseRequestParam(builderMojo, method)); 102 | 103 | //分析response的pojo的参数列表 104 | codeGenModel.setResponseParamModelList(parseResponseParam(builderMojo, method)); 105 | 106 | //分析获得curlExample 107 | codeGenModel.setCurlExample(parseCurlExample(builderMojo, codeGenModel)); 108 | 109 | return codeGenModel; 110 | } 111 | 112 | private static Class[] parseRequestGroups(Method method) { 113 | 114 | CodeGenRequest codeGenRequest = method.getAnnotation(CodeGenRequest.class); 115 | if (codeGenRequest != null) { 116 | return codeGenRequest.groups(); 117 | } else { 118 | return new Class[]{}; 119 | } 120 | 121 | } 122 | 123 | private static Optional parseRequestClass(Method method) { 124 | CodeGenRequest codeGenRequest = method.getAnnotation(CodeGenRequest.class); 125 | if (codeGenRequest != null) { 126 | return Optional.of(codeGenRequest.value()); 127 | } else { 128 | return Optional.empty(); 129 | } 130 | } 131 | 132 | private static Optional parseResponseClass(Method method) { 133 | CodeGenResponse codeGenResponse = method.getAnnotation(CodeGenResponse.class); 134 | if (codeGenResponse != null) { 135 | return Optional.of(codeGenResponse.value()); 136 | } else { 137 | return Optional.empty(); 138 | } 139 | } 140 | 141 | private static Class[] parseResponseGroups(Method method) { 142 | CodeGenResponse codeGenResponse = method.getAnnotation(CodeGenResponse.class); 143 | if (codeGenResponse != null) { 144 | return codeGenResponse.groups(); 145 | } else { 146 | return new Class[]{}; 147 | } 148 | } 149 | 150 | private static boolean parseIsRest(Method method) { 151 | CodeGenRequest codeGenRequest = method.getAnnotation(CodeGenRequest.class); 152 | if (codeGenRequest != null && codeGenRequest.isRest()) { 153 | return true; 154 | } else { 155 | return false; 156 | } 157 | } 158 | 159 | public static boolean isPostFile(List requestParamModelList) { 160 | for(ParamModel requestParamModel : requestParamModelList) { 161 | if("org.springframework.web.multipart.MultipartFile".equals(requestParamModel.getType())) { 162 | return true; 163 | } 164 | } 165 | 166 | return false; 167 | } 168 | 169 | private static String parseSuccessReturnString(BuilderMojo builderMojo, CodeGenModel codeGenModel) { 170 | 171 | if(codeGenModel.getResponseClass().isPresent()) { 172 | try { 173 | return JSON.toJSONString(genParamJsonObj(builderMojo, codeGenModel.getResponseClass().get(), codeGenModel.getResponseGroups()), SerializerFeature.PrettyFormat); 174 | } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) { 175 | e.printStackTrace(); 176 | } 177 | } 178 | 179 | return ""; 180 | } 181 | 182 | /** 183 | * 生成请求成功返回的json表示 184 | */ 185 | public static Object genParamJsonObj(BuilderMojo builderMojo, Class clazz, Class[] responseGroups) throws ClassNotFoundException, 186 | IllegalAccessException, InstantiationException { 187 | 188 | Object obj = clazz.newInstance(); 189 | 190 | for (Field field : FieldUtils.getAllFieldsList(clazz)) { 191 | 192 | field.setAccessible(true); 193 | 194 | CodeGenField codeGenField = field.getAnnotation(CodeGenField.class); 195 | if(codeGenField == null) { 196 | field.set(obj, null); 197 | continue; 198 | } else { 199 | 200 | final boolean shouldShow = isShouldShow(responseGroups, codeGenField.groups()); 201 | 202 | if(!shouldShow) { 203 | field.set(obj, null); 204 | continue; 205 | } 206 | } 207 | 208 | if (field.getType().equals(String.class)) { 209 | field.set(obj, field.getName()); 210 | } else if(field.getType().equals(Boolean.class)){ 211 | field.set(obj, false); 212 | } else if(field.getType().equals(Short.class) || field.getType().equals(Integer.class)) { 213 | field.set(obj, 0); 214 | } else if(field.getType().equals(Long.class)) { 215 | field.set(obj, 0L); 216 | } else if(field.getType().equals(Float.class)){ 217 | field.set(obj, 0.0f); 218 | } else if (field.getType().equals(Double.class)){ 219 | field.set(obj, 0.0d); 220 | } else if(field.getType().equals(Date.class)) { 221 | field.set(obj, new Date()); 222 | } else if (field.getType().equals(List.class) || field.getType().equals(Set.class)) { 223 | 224 | //生成一个list 225 | Collection collection; 226 | 227 | if(field.getType().equals(List.class)) { 228 | collection = new ArrayList<>(); 229 | } else { 230 | collection = new HashSet<>(); 231 | } 232 | 233 | Type fc = field.getGenericType(); 234 | 235 | if (fc == null) { 236 | continue; 237 | } 238 | 239 | if (fc instanceof ParameterizedType) { 240 | ParameterizedType pt = (ParameterizedType) fc; 241 | String className = pt.getActualTypeArguments()[0].getTypeName(); 242 | 243 | //List里面的泛型参数 244 | switch (className) { 245 | case "java.lang.String": 246 | collection.add("string"); 247 | break; 248 | case "java.lang.Boolean": 249 | collection.add(false); 250 | break; 251 | case "java.lang.Short": 252 | case "java.lang.Integer": 253 | case "java.lang.Long": 254 | collection.add(0); 255 | break; 256 | case "java.lang.Float": 257 | case "java.lang.Double": 258 | collection.add(0); 259 | break; 260 | default: 261 | if(builderMojo.getClassLoaderInterface().loadClass(className).isEnum()) { 262 | collection.add(builderMojo.getClassLoaderInterface().loadClass(className).getEnumConstants()[0]); 263 | } else { 264 | collection.add(genParamJsonObj(builderMojo, builderMojo.getClassLoaderInterface().loadClass(className), responseGroups)); 265 | } 266 | } 267 | } 268 | 269 | //设置值到list 270 | field.set(obj, collection); 271 | 272 | } else if (field.getType().isEnum()) { 273 | Enum[] enums = (Enum[])field.getType().getEnumConstants(); 274 | field.set(obj, enums[0]); 275 | } else if(!field.getType().isPrimitive()) { 276 | field.set(obj, genParamJsonObj(builderMojo, field.getType(), responseGroups)); 277 | } 278 | } 279 | 280 | builderMojo.getLog().debug("分析完之后: " + obj); 281 | 282 | return obj; 283 | } 284 | 285 | private static Optional isListWithEnum(BuilderMojo builderMojo, Field field) throws ClassNotFoundException { 286 | if(field.getType().equals(List.class) || field.getType().equals(Set.class)) { 287 | 288 | Type fc = field.getGenericType(); 289 | 290 | if (fc != null && fc instanceof ParameterizedType) { 291 | ParameterizedType pt = (ParameterizedType) fc; 292 | String className = pt.getActualTypeArguments()[0].getTypeName(); 293 | 294 | if(builderMojo.getClassLoaderInterface().loadClass(className).isEnum()) { 295 | return Optional.of(builderMojo.getClassLoaderInterface().loadClass(className)); 296 | } 297 | } 298 | 299 | } 300 | 301 | return Optional.empty(); 302 | } 303 | 304 | private static boolean isShouldShow(Class[] beanGroups, Class[] codeGenGroups) { 305 | 306 | boolean result = false; 307 | 308 | List codeGenFieldGroupsList = Arrays.asList(codeGenGroups); 309 | List beanGroupsList = Arrays.asList(beanGroups); 310 | 311 | for(Class responseGroupClass : beanGroupsList){ 312 | if(codeGenFieldGroupsList.contains(responseGroupClass)) { 313 | result = true; 314 | } 315 | } 316 | return result; 317 | } 318 | 319 | 320 | private static List parseRequestParam(BuilderMojo builderMojo, Method method) throws ClassNotFoundException { 321 | 322 | List requestParamModelList = new ArrayList<>(); 323 | 324 | CodeGenRequest codeGenRequest = method.getAnnotation(CodeGenRequest.class); 325 | if (codeGenRequest != null) { 326 | requestParamModelList.addAll(parseParamModel(builderMojo, codeGenRequest.value(), codeGenRequest.groups())); 327 | } 328 | 329 | Collections.sort(requestParamModelList); 330 | return requestParamModelList; 331 | } 332 | 333 | private static List parseResponseParam(BuilderMojo builderMojo, Method method) throws ClassNotFoundException { 334 | 335 | List responseParamModelList = new ArrayList<>(); 336 | 337 | CodeGenResponse codeGenResponse = method.getAnnotation(CodeGenResponse.class); 338 | if (codeGenResponse != null) { 339 | responseParamModelList.addAll(parseParamModel(builderMojo, codeGenResponse.value(), codeGenResponse.groups())); 340 | } 341 | 342 | Collections.sort(responseParamModelList); 343 | return responseParamModelList; 344 | } 345 | 346 | private static List parseParamModel(BuilderMojo builderMojo, Class clazz, Class[] validateGroups) throws ClassNotFoundException { 347 | 348 | List result = new ArrayList<>(); 349 | 350 | for(Field field : FieldUtils.getAllFieldsList(clazz)) { 351 | 352 | //遍历返回pojo的带有@CodeGenField注解的变量 353 | CodeGenField codeGenField = field.getAnnotation(CodeGenField.class); 354 | 355 | if(codeGenField != null) { 356 | 357 | if(!isShouldShow(validateGroups, codeGenField.groups())) { 358 | continue; 359 | } 360 | 361 | ParamModel paramModel = new ParamModel(); 362 | paramModel.setKey(field.getName()); 363 | paramModel.setRequired(paresFieldIsRequired(field)); 364 | paramModel.setComment(codeGenField.value()); 365 | paramModel.setType(field.getGenericType().getTypeName()); 366 | paramModel.setConstraintStr(constrainAnnotationToStr(field.getAnnotations())); 367 | if(field.getType().isEnum()) { 368 | paramModel.getConstraintStr().add(enumTypeGenConstrainStr(field.getType())); 369 | } 370 | isListWithEnum(builderMojo, field).ifPresent(aClass -> paramModel.getConstraintStr().add(enumTypeGenConstrainStr(aClass))); 371 | 372 | result.add(paramModel); 373 | 374 | if(!field.getType().isPrimitive()) { 375 | if (field.getType().equals(List.class)) {//如果是List集合,就得分析List泛型的类型 376 | 377 | Type type = field.getGenericType(); 378 | 379 | if (type == null) { 380 | continue; 381 | } 382 | 383 | if (type instanceof ParameterizedType) { 384 | ParameterizedType pt = (ParameterizedType) type; 385 | 386 | // List集合中的参数类型的字节码 387 | try { 388 | String genericsTypeStr = pt.getActualTypeArguments()[0].getTypeName(); 389 | List paramModelList = parseParamModel(builderMojo, builderMojo.getClassLoaderInterface().loadClass(genericsTypeStr), validateGroups); 390 | Collections.sort(paramModelList); 391 | paramModel.setSubParamModel(paramModelList); 392 | } catch (ClassNotFoundException e) { 393 | throw new RuntimeException("找不到类:" + pt.getActualTypeArguments()[0].getTypeName()); 394 | } 395 | } 396 | } else if(field.getType().equals(Map.class)) { 397 | throw new RuntimeException("不应该使用Map, 请使用pojo"); 398 | } else { 399 | // 如果是普通的类 400 | List paramModelList = parseParamModel(builderMojo, field.getType(), validateGroups); 401 | Collections.sort(paramModelList); 402 | paramModel.setSubParamModel(paramModelList); 403 | } 404 | } else { 405 | throw new RuntimeException("不应该使用简单类型: " + field.getName()); 406 | } 407 | } 408 | } 409 | 410 | return result; 411 | } 412 | 413 | private static String enumTypeGenConstrainStr(Class anEnum) { 414 | List enumValueList = new ArrayList<>(); 415 | 416 | if(anEnum.isEnum()) { 417 | Enum[] enums = (Enum[])anEnum.getEnumConstants() ; 418 | for(Enum enumItem : enums){ 419 | enumValueList.add(enumItem.name()); 420 | } 421 | } 422 | return "必须为以下可用值之一: [ \"" + StringUtils.join(enumValueList, "\", \"") + "\" ]"; 423 | } 424 | 425 | private static String paresHttpMethod(Method method) { 426 | 427 | if(method.getAnnotation(GetMapping.class) != null) { 428 | return "GET"; 429 | } 430 | 431 | if(method.getAnnotation(PostMapping.class) != null) { 432 | return "POST"; 433 | } 434 | 435 | String result = ""; 436 | for (RequestMethod requestMethod : method.getAnnotation(RequestMapping.class).method()) { 437 | if (result.equals("")) { 438 | result += requestMethod.name(); 439 | } else { 440 | result += ", " + requestMethod.name(); 441 | } 442 | } 443 | if("".equals(result)) { 444 | result = "GET, POST"; 445 | } 446 | return result; 447 | } 448 | 449 | private static String paresUrl(Method method) { 450 | 451 | if(method.getAnnotation(GetMapping.class) != null) { 452 | return String.join(", ", method.getAnnotation(GetMapping.class).value()); 453 | } 454 | 455 | if(method.getAnnotation(PostMapping.class) != null) { 456 | return String.join(", ", method.getAnnotation(PostMapping.class).value()); 457 | } 458 | 459 | return String.join(", ", method.getAnnotation(RequestMapping.class).value()); 460 | } 461 | 462 | private static boolean paresFieldIsRequired(Field field) { 463 | return field.getAnnotation(NotNull.class) != null || 464 | field.getAnnotation(NotEmpty.class) != null || 465 | field.getAnnotation(NotBlank.class) != null; 466 | } 467 | 468 | } 469 | -------------------------------------------------------------------------------- /src/main/java/com/jtool/codegenbuilderplugin/util/AnnotationHelper.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.util; 2 | 3 | import com.jtool.annotation.AvailableValues; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import javax.validation.constraints.*; 7 | import java.lang.annotation.Annotation; 8 | import java.lang.reflect.AnnotatedElement; 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | public class AnnotationHelper { 13 | 14 | public static boolean hasAnnotation(AnnotatedElement annotatedElement, Class annotationClass) { 15 | return annotatedElement.getAnnotation(annotationClass) != null; 16 | } 17 | 18 | public static List constrainAnnotationToStr(Annotation[] annotations) { 19 | 20 | List result = new ArrayList<>(); 21 | 22 | for(Annotation annotation : annotations) { 23 | if (annotation instanceof AssertTrue) { 24 | result.add("必须为True"); 25 | } else if (annotation instanceof AssertFalse) { 26 | result.add("必须为False"); 27 | } else if(annotation instanceof NotEmpty) { 28 | result.add("必须有值,长度大于0"); 29 | } else if(annotation instanceof NotBlank) { 30 | result.add("必须有值,不能是空白字符,长度大于0"); 31 | } else if (annotation instanceof Digits) { 32 | Digits digits = (Digits) annotation; 33 | if (digits.fraction() == 0) { 34 | result.add("必须为一个整型数字"); 35 | } else if (digits.fraction() > 0) { 36 | result.add("必须为一个" + digits.fraction() + "位小数"); 37 | } else { 38 | throw new RuntimeException("Digits的fraction必须为一个非负数"); 39 | } 40 | } else if (annotation instanceof Min) { 41 | result.add("必须大于或等于" + ((Min) annotation).value()); 42 | } else if (annotation instanceof DecimalMin) { 43 | result.add("必须大于或等于" + ((DecimalMin) annotation).value()); 44 | } else if (annotation instanceof Max) { 45 | result.add("必须小于或等于" + ((Max) annotation).value()); 46 | } else if (annotation instanceof DecimalMax) { 47 | result.add("必须小于或等于" + ((DecimalMax) annotation).value()); 48 | } else if (annotation instanceof Size) { 49 | Size size = (Size) annotation; 50 | result.add("长度边界[" + size.min() + " : " + size.max() + "]"); 51 | } else if (annotation instanceof Past) { 52 | result.add("必须为过去的某个时间"); 53 | } else if (annotation instanceof Future) { 54 | result.add("必须为将来的某个时间"); 55 | } else if (annotation instanceof Pattern) { 56 | Pattern pattern = (Pattern) annotation; 57 | result.add("必须匹配正则表达式:" + pattern.regexp()); 58 | } 59 | //自定义限制注解 60 | else if(annotation instanceof AvailableValues) { 61 | AvailableValues availableValues = (AvailableValues)annotation; 62 | String[] values = availableValues.values(); 63 | result.add("必须为以下可用值之一: [ \"" + StringUtils.join(values, "\", \"") + "\" ]"); 64 | } 65 | } 66 | 67 | return result; 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/jtool/codegenbuilderplugin/util/CurlExampleHelper.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.util; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.serializer.SerializerFeature; 5 | import com.jtool.codegenbuilderplugin.BuilderMojo; 6 | import com.jtool.codegenbuilderplugin.model.CodeGenModel; 7 | import com.jtool.codegenbuilderplugin.model.ParamModel; 8 | 9 | import java.io.IOException; 10 | 11 | import static com.jtool.codegenbuilderplugin.parser.MethodParser.genParamJsonObj; 12 | import static com.jtool.codegenbuilderplugin.parser.MethodParser.isPostFile; 13 | 14 | public class CurlExampleHelper { 15 | 16 | public static String parseCurlExample(BuilderMojo builderMojo, CodeGenModel codeGenModel) throws ClassNotFoundException, IOException, InstantiationException, IllegalAccessException { 17 | 18 | String result = ""; 19 | 20 | if(codeGenModel.getHttpMethod() != null && codeGenModel.getHttpMethod().contains("GET")) { 21 | 22 | String queryString = ""; 23 | 24 | for(int i = 0; i < codeGenModel.getRequestParamModelList().size(); i++) { 25 | if(i != 0) { 26 | queryString += "&"; 27 | } 28 | 29 | queryString += codeGenModel.getRequestParamModelList().get(i).getKey() + "="; 30 | } 31 | 32 | result = "curl -X GET \"http://host:port" + codeGenModel.getUrl(); 33 | 34 | if(!"".equals(queryString)) { 35 | result += "?" + queryString; 36 | result += "\""; 37 | } 38 | } 39 | 40 | if(codeGenModel.getHttpMethod() != null && codeGenModel.getHttpMethod().contains("POST")) { 41 | 42 | if(!"".equals(result)) { 43 | result += " \r\n"; 44 | result += " \r\n"; 45 | } 46 | 47 | if(isPostFile(codeGenModel.getRequestParamModelList())) { 48 | String queryString = ""; 49 | 50 | for(ParamModel requestParamModel : codeGenModel.getRequestParamModelList()) { 51 | queryString += " -F \"" + requestParamModel.getKey() + "=" + "\" "; 52 | } 53 | 54 | result += "curl -X POST " + queryString + " \"http://host:port" + codeGenModel.getUrl() + "\""; 55 | } else { 56 | String queryString = ""; 57 | 58 | if(codeGenModel.isRest() && codeGenModel.getRequestClass().isPresent()) { 59 | final Object obj = genParamJsonObj(builderMojo, codeGenModel.getRequestClass().get(), codeGenModel.getRequestGroups()); 60 | queryString += " -H 'Content-Type:application/json' -d '" + JSON.toJSONString(obj, SerializerFeature.PrettyFormat); 61 | 62 | } else { 63 | for(ParamModel requestParamModel : codeGenModel.getRequestParamModelList()) { 64 | queryString += " --data-urlencode \"" + requestParamModel.getKey() + "=" + "\" "; 65 | } 66 | } 67 | 68 | result += "curl -X POST " + queryString + " \"http://host:port" + codeGenModel.getUrl() + "\""; 69 | } 70 | } 71 | 72 | return result; 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/main/resources/template/mdTemplate.md: -------------------------------------------------------------------------------- 1 | # 目录 2 | API列表 3 | 4 | <#list apiModelList as apiModelItem> 5 | * ${apiModelItem.apiName} <#if apiModelItem.docSeq lt 100000000>(${apiModelItem.docSeq}) 6 | 7 | 8 | 错误码定义 9 | 10 | 11 | # 接口列表详情 12 | <#list apiModelList as apiModelItem> 13 | ## [ ${apiModelItem.apiName} ] 14 | ### 接口说明 15 | ```txt 16 | ${apiModelItem.description} 17 | ``` 18 | ### 定义 19 | ```shell 20 | ${apiModelItem.curlExample} 21 | ``` 22 | 23 | <#if (apiModelItem.requestParamModelList?size>0) > 24 | ### 请求参数说明 25 | 26 | 27 | 28 | 29 | <#list apiModelItem.requestParamModelList as paramModelItem> 30 | ${returnParamDetail(paramModelItem)} 31 | 32 |
参数名必要参数限制说明
33 | 34 | 35 | <#if (apiModelItem.responseParamModelList?size>0) > 36 | ### 正确返回 37 | ```json 38 | ${successReturnJson(apiModelItem.successReturnJsonStr)} 39 | ``` 40 | ### 正确返回参数说明 41 | 42 | 43 | 44 | 45 | <#list apiModelItem.responseParamModelList as paramModelItem> 46 | ${returnParamDetail(paramModelItem)} 47 | 48 |
参数名必要参数限制说明
49 | 50 | 51 | 返回api列表 52 | 53 | 54 | 55 | 56 | 57 | 58 | <#if (exceptionModels?size>0) > 59 | ## 错误码定义 60 | 错误码|说明| 61 | :------:|------| 62 | <#list exceptionModels as exceptionModel> 63 | ${exceptionModel.code}|${exceptionModel.desc?html} 64 | 65 | 66 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/Tester.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test; 2 | 3 | import com.jtool.codegenbuilderplugin.BuilderMojo; 4 | import com.jtool.codegenbuilderplugin.test.util.TestContextClassLoader; 5 | import org.apache.maven.artifact.DependencyResolutionRequiredException; 6 | import org.apache.maven.plugin.MojoExecutionException; 7 | import org.apache.maven.plugin.MojoFailureException; 8 | import org.junit.Test; 9 | 10 | import java.io.File; 11 | 12 | public class Tester { 13 | 14 | @Test 15 | public void testGen() throws MojoFailureException, MojoExecutionException, DependencyResolutionRequiredException { 16 | 17 | String baseUrl = this.getClass().getResource("/").getPath().replace("/target/test-classes/", ""); 18 | 19 | BuilderMojo builderMojo = new BuilderMojo(); 20 | TestContextClassLoader testContextClassLoader = new TestContextClassLoader(); 21 | builderMojo.setClassLoaderInterface(testContextClassLoader); 22 | 23 | File baseDirFile = new File(baseUrl); 24 | builderMojo.setOutPath(baseDirFile.getAbsolutePath() + "/target/"); 25 | builderMojo.setBasedir(baseDirFile); 26 | 27 | builderMojo.setProjectName("codegen-builder-plugin"); 28 | builderMojo.setScanSource("/src/test/java/"); 29 | builderMojo.setScanBasePackage("com.jtool.codegenbuilderplugin.test"); 30 | builderMojo.execute(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/Add.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | public interface Add { 4 | } 5 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/AppFullAd.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | import com.jtool.codegenannotation.CodeGenField; 4 | 5 | public class AppFullAd extends BaseAdRequest { 6 | 7 | @CodeGenField("广告状态") 8 | private Boolean adStart; 9 | 10 | @CodeGenField("广告标题") 11 | private String adTitle; 12 | 13 | public Boolean getAdStart() { 14 | return adStart; 15 | } 16 | 17 | public void setAdStart(Boolean adStart) { 18 | this.adStart = adStart; 19 | } 20 | 21 | public String getAdTitle() { 22 | return adTitle; 23 | } 24 | 25 | public void setAdTitle(String adTitle) { 26 | this.adTitle = adTitle; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/AppPopAd.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | public class AppPopAd extends BaseAdRequest { 4 | 5 | private String adImg; 6 | 7 | public String getAdImg() { 8 | return adImg; 9 | } 10 | 11 | public void setAdImg(String adImg) { 12 | this.adImg = adImg; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/BaseAdRequest.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | 5 | public abstract class BaseAdRequest { 6 | 7 | private String id; 8 | private String adName; 9 | 10 | public String getId() { 11 | return id; 12 | } 13 | 14 | public void setId(String id) { 15 | this.id = id; 16 | } 17 | 18 | public String getAdName() { 19 | return adName; 20 | } 21 | 22 | public void setAdName(String adName) { 23 | this.adName = adName; 24 | } 25 | 26 | @Override 27 | public String toString() { 28 | return JSON.toJSONString(this); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/BaseCheckSdkPluginsRequest.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | import com.jtool.codegenannotation.CodeGenField; 4 | 5 | import javax.validation.constraints.NotNull; 6 | import javax.validation.constraints.Size; 7 | 8 | public class BaseCheckSdkPluginsRequest { 9 | 10 | @CodeGenField("sim卡唯一标示,没有请上传999999999999999,15个9") 11 | @NotNull 12 | @Size(min = 15, max = 16) 13 | protected String imsi; 14 | 15 | @CodeGenField("设备唯一标示") 16 | @NotNull 17 | @Size(min = 15, max = 16) 18 | protected String imei; 19 | 20 | public String getImsi() { 21 | return imsi; 22 | } 23 | 24 | public void setImsi(String imsi) { 25 | this.imsi = imsi; 26 | } 27 | 28 | public String getImei() { 29 | return imei; 30 | } 31 | 32 | public void setImei(String imei) { 33 | this.imei = imei; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/BaseResponse.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.jtool.codegenannotation.CodeGenField; 5 | 6 | import javax.validation.constraints.NotNull; 7 | 8 | public class BaseResponse { 9 | 10 | @NotNull 11 | @CodeGenField("状态码, 0:完成") 12 | protected Integer code = 0; 13 | 14 | public BaseResponse() { 15 | } 16 | 17 | public BaseResponse(Integer code) { 18 | this.code = code; 19 | } 20 | 21 | public Integer getCode() { 22 | return code; 23 | } 24 | 25 | public void setCode(Integer code) { 26 | this.code = code; 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return JSON.toJSONString(this); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/Channel.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.jtool.codegenannotation.CodeGenField; 5 | 6 | import javax.validation.constraints.NotNull; 7 | import javax.validation.constraints.Size; 8 | import javax.validation.groups.Default; 9 | 10 | public class Channel { 11 | 12 | @NotNull 13 | @CodeGenField("唯一id") 14 | private String id; 15 | 16 | @NotNull(groups = {Default.class, Add.class}) 17 | @CodeGenField(value = "渠道名称", groups = {Default.class, Add.class}) 18 | @Size(min = 1, max = 200, groups = {Default.class, Add.class}) 19 | private String name; 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public void setName(String name) { 26 | this.name = name; 27 | } 28 | 29 | public String getId() { 30 | return id; 31 | } 32 | 33 | public void setId(String id) { 34 | this.id = id; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return JSON.toJSONString(this); 40 | } 41 | 42 | @Override 43 | public boolean equals(Object o) { 44 | if (this == o) return true; 45 | if (o == null || getClass() != o.getClass()) return false; 46 | 47 | Channel channel = (Channel) o; 48 | 49 | if (id != null ? !id.equals(channel.id) : channel.id != null) return false; 50 | return name != null ? name.equals(channel.name) : channel.name == null; 51 | } 52 | 53 | @Override 54 | public int hashCode() { 55 | int result = id != null ? id.hashCode() : 0; 56 | result = 31 * result + (name != null ? name.hashCode() : 0); 57 | return result; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/Check.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | public interface Check { 4 | } 5 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/CheckSdkPluginsItem.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.jtool.codegenannotation.CodeGenField; 5 | 6 | public class CheckSdkPluginsItem { 7 | 8 | @CodeGenField("插件id") 9 | private String id; 10 | 11 | @CodeGenField(value = "插件版本", groups = Add.class) 12 | private Integer version; 13 | 14 | public String getId() { 15 | return id; 16 | } 17 | 18 | public void setId(String id) { 19 | this.id = id; 20 | } 21 | 22 | public Integer getVersion() { 23 | return version; 24 | } 25 | 26 | public void setVersion(Integer version) { 27 | this.version = version; 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return JSON.toJSONString(this); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/CheckSdkPluginsRequest.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.jtool.codegenannotation.CodeGenField; 5 | 6 | import javax.validation.constraints.NotNull; 7 | import javax.validation.constraints.Size; 8 | import java.util.List; 9 | 10 | public class CheckSdkPluginsRequest extends BaseCheckSdkPluginsRequest { 11 | 12 | @CodeGenField(value = "唯一id") 13 | private String id; 14 | 15 | @CodeGenField(value = "渠道", groups = Add.class) 16 | @NotNull 17 | private String channel; 18 | 19 | @CodeGenField(value = "md5值", groups = {Add.class, Check.class}) 20 | @NotNull 21 | private String md5; 22 | 23 | @CodeGenField(value = "插件信息集合", groups = Add.class) 24 | @NotNull 25 | private List pluginsInfo; 26 | 27 | public String getId() { 28 | return id; 29 | } 30 | 31 | public void setId(String id) { 32 | this.id = id; 33 | } 34 | 35 | public String getChannel() { 36 | return channel; 37 | } 38 | 39 | public void setChannel(String channel) { 40 | this.channel = channel; 41 | } 42 | 43 | public String getMd5() { 44 | return md5; 45 | } 46 | 47 | public void setMd5(String md5) { 48 | this.md5 = md5; 49 | } 50 | 51 | public List getPluginsInfo() { 52 | return pluginsInfo; 53 | } 54 | 55 | public void setPluginsInfo(List pluginsInfo) { 56 | this.pluginsInfo = pluginsInfo; 57 | } 58 | 59 | @Override 60 | public String toString() { 61 | return JSON.toJSONString(this); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/EnumTypeRequest.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | import com.jtool.codegenannotation.CodeGenField; 4 | 5 | import javax.validation.constraints.NotEmpty; 6 | import javax.validation.constraints.NotNull; 7 | import java.util.List; 8 | 9 | public class EnumTypeRequest { 10 | 11 | @NotNull 12 | @CodeGenField("类型枚举") 13 | private Type type; 14 | 15 | @NotEmpty 16 | @CodeGenField("类型枚举列表") 17 | private List typeList; 18 | 19 | public Type getType() { 20 | return type; 21 | } 22 | 23 | public void setType(Type type) { 24 | this.type = type; 25 | } 26 | 27 | public List getTypeList() { 28 | return typeList; 29 | } 30 | 31 | public EnumTypeRequest setTypeList(List typeList) { 32 | this.typeList = typeList; 33 | return this; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/GetSdkConfigResponse.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | import com.jtool.codegenannotation.CodeGenField; 4 | import com.jtool.codegenbuilderplugin.test.validate.Get; 5 | 6 | import javax.validation.constraints.NotNull; 7 | import java.util.List; 8 | 9 | public class GetSdkConfigResponse extends BaseResponse { 10 | 11 | @NotNull 12 | @CodeGenField(value = "sdk插件列表", groups = Get.class) 13 | private List sdkConfigsList; 14 | 15 | public List getSdkConfigsList() { 16 | return sdkConfigsList; 17 | } 18 | 19 | public void setSdkConfigsList(List sdkConfigsList) { 20 | this.sdkConfigsList = sdkConfigsList; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/LoginRequest.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.jtool.codegenannotation.CodeGenField; 5 | 6 | import javax.validation.constraints.NotNull; 7 | 8 | public class LoginRequest { 9 | 10 | public LoginRequest() { 11 | } 12 | 13 | public LoginRequest(@NotNull String accountId, @NotNull String password) { 14 | this.accountId = accountId; 15 | this.password = password; 16 | } 17 | 18 | @CodeGenField("用户账号(目前是手机号)") 19 | @NotNull 20 | private String accountId; 21 | 22 | @CodeGenField("密码") 23 | @NotNull 24 | private String password; 25 | 26 | public String getAccountId() { 27 | return accountId; 28 | } 29 | 30 | public LoginRequest setAccountId(String accountId) { 31 | this.accountId = accountId; 32 | return this; 33 | } 34 | 35 | public String getPassword() { 36 | return password; 37 | } 38 | 39 | public LoginRequest setPassword(String password) { 40 | this.password = password; 41 | return this; 42 | } 43 | 44 | @Override 45 | public String toString() { 46 | return JSON.toJSONString(this); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/PrePaymentRequest.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.jtool.codegenannotation.CodeGenField; 5 | 6 | import javax.validation.constraints.NotNull; 7 | 8 | /** 9 | * Created by chenfengbin on 16-6-24. 10 | */ 11 | public class PrePaymentRequest { 12 | 13 | @NotNull 14 | @CodeGenField("注册时生成的应用ID") 15 | private String appId; 16 | 17 | @NotNull 18 | @CodeGenField("账号ID") 19 | private String accountId; 20 | 21 | @CodeGenField("手机号码") 22 | private String phoneNum; 23 | 24 | @NotNull 25 | @CodeGenField("金额,保留两位小数") 26 | private String amount; 27 | 28 | @NotNull 29 | @CodeGenField(value = "币种", isPathParam = true) 30 | private String currency; 31 | 32 | @NotNull 33 | @CodeGenField("提交支付后回调的结果页面") 34 | private String returnUrl; 35 | 36 | @NotNull 37 | @CodeGenField("应用生成的交易定单Id,") 38 | private String tradeNo; 39 | 40 | @NotNull 41 | @CodeGenField("支付说明,主要用于页面显示") 42 | private String productDesc; 43 | 44 | @NotNull 45 | @CodeGenField("时间戳,13位") 46 | private String timestamp; 47 | 48 | @NotNull 49 | @CodeGenField("(appId+accountId+amount+returnUrl+tradeNo+productDesc+timestamp+appSecret)" + 50 | "用32位小写的MD5加密") 51 | private String sign; 52 | 53 | 54 | @Override 55 | public String toString() { 56 | return JSON.toJSONString(this); 57 | } 58 | 59 | public String getAppId() { 60 | return appId; 61 | } 62 | 63 | public void setAppId(String appId) { 64 | this.appId = appId; 65 | } 66 | 67 | public String getAccountId() { 68 | return accountId; 69 | } 70 | 71 | public void setAccountId(String accountId) { 72 | this.accountId = accountId; 73 | } 74 | 75 | public String getPhoneNum() { 76 | return phoneNum; 77 | } 78 | 79 | public void setPhoneNum(String phoneNum) { 80 | this.phoneNum = phoneNum; 81 | } 82 | 83 | public String getAmount() { 84 | return amount; 85 | } 86 | 87 | public void setAmount(String amount) { 88 | this.amount = amount; 89 | } 90 | 91 | public String getCurrency() { 92 | return currency; 93 | } 94 | 95 | public void setCurrency(String currency) { 96 | this.currency = currency; 97 | } 98 | 99 | public String getReturnUrl() { 100 | return returnUrl; 101 | } 102 | 103 | public void setReturnUrl(String returnUrl) { 104 | this.returnUrl = returnUrl; 105 | } 106 | 107 | public String getTradeNo() { 108 | return tradeNo; 109 | } 110 | 111 | public void setTradeNo(String tradeNo) { 112 | this.tradeNo = tradeNo; 113 | } 114 | 115 | public String getTimestamp() { 116 | return timestamp; 117 | } 118 | 119 | public void setTimestamp(String timestamp) { 120 | this.timestamp = timestamp; 121 | } 122 | 123 | public String getProductDesc() { 124 | return productDesc; 125 | } 126 | 127 | public void setProductDesc(String productDesc) { 128 | this.productDesc = productDesc; 129 | } 130 | 131 | public String getSign() { 132 | return sign; 133 | } 134 | 135 | public void setSign(String sign) { 136 | this.sign = sign; 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/QuerySingleRequest.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | import com.jtool.codegenannotation.CodeGenField; 4 | 5 | import javax.validation.constraints.NotNull; 6 | 7 | /** 8 | * Created by wen on 16-6-30. 9 | */ 10 | public class QuerySingleRequest { 11 | 12 | @NotNull 13 | @CodeGenField(value = "定单id", isPathParam = true) 14 | private String orderId; 15 | 16 | @NotNull 17 | @CodeGenField("账号") 18 | private String accountId; 19 | 20 | @NotNull 21 | @CodeGenField(value = "我的Id", isPathParam = true) 22 | private String myId; 23 | 24 | @NotNull 25 | @CodeGenField("订单") 26 | private String order; 27 | 28 | public String getOrderId() { 29 | return orderId; 30 | } 31 | 32 | public QuerySingleRequest setOrderId(String orderId) { 33 | this.orderId = orderId; 34 | return this; 35 | } 36 | 37 | public String getAccountId() { 38 | return accountId; 39 | } 40 | 41 | public QuerySingleRequest setAccountId(String accountId) { 42 | this.accountId = accountId; 43 | return this; 44 | } 45 | 46 | public String getMyId() { 47 | return myId; 48 | } 49 | 50 | public void setMyId(String myId) { 51 | this.myId = myId; 52 | } 53 | 54 | public String getOrder() { 55 | return order; 56 | } 57 | 58 | public void setOrder(String order) { 59 | this.order = order; 60 | } 61 | 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/SdkConfig.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.jtool.annotation.AvailableValues; 5 | import com.jtool.codegenannotation.CodeGenField; 6 | import com.jtool.codegenbuilderplugin.test.validate.Get; 7 | 8 | import javax.validation.constraints.NotNull; 9 | import java.util.List; 10 | 11 | public class SdkConfig { 12 | 13 | @NotNull 14 | @CodeGenField(value = "唯一id", groups = Get.class) 15 | private String id; 16 | 17 | @CodeGenField(value = "插件对象", groups = Get.class) 18 | private SdkPlugin sdkPlugin; 19 | 20 | @CodeGenField(value = "sdkPlugin的Id", groups = {Add.class}) 21 | @NotNull(groups = {Add.class}) 22 | private String sdkPluginId; 23 | 24 | @CodeGenField(value = "渠道", groups = {Add.class, Get.class}) 25 | @NotNull(groups = {Add.class}) 26 | private String channel; 27 | 28 | @CodeGenField(value = "国家列表", groups = {Add.class, Get.class}) 29 | @NotNull(groups = {Add.class}) 30 | private List countryList; 31 | 32 | @CodeGenField(value = "运营商列表", groups = {Add.class, Get.class}) 33 | @NotNull(groups = {Add.class}) 34 | private List operatorsList; 35 | 36 | @CodeGenField(value = "品牌", groups = {Add.class, Get.class}) 37 | @NotNull(groups = {Add.class}) 38 | @AvailableValues(values = {"ALL", "TRANSSION", "OTHERS"}) 39 | private String brand; 40 | 41 | @CodeGenField(value = "目标包名", groups = {Add.class, Get.class}) 42 | private String targetPackageName = "ALL"; 43 | 44 | public String getId() { 45 | return id; 46 | } 47 | 48 | public void setId(String id) { 49 | this.id = id; 50 | } 51 | 52 | public SdkPlugin getSdkPlugin() { 53 | return sdkPlugin; 54 | } 55 | 56 | public void setSdkPlugin(SdkPlugin sdkPlugin) { 57 | this.sdkPlugin = sdkPlugin; 58 | } 59 | 60 | public String getSdkPluginId() { 61 | return sdkPluginId; 62 | } 63 | 64 | public void setSdkPluginId(String sdkPluginId) { 65 | this.sdkPluginId = sdkPluginId; 66 | } 67 | 68 | public String getChannel() { 69 | return channel; 70 | } 71 | 72 | public void setChannel(String channel) { 73 | this.channel = channel; 74 | } 75 | 76 | public List getCountryList() { 77 | return countryList; 78 | } 79 | 80 | public void setCountryList(List countryList) { 81 | this.countryList = countryList; 82 | } 83 | 84 | public List getOperatorsList() { 85 | return operatorsList; 86 | } 87 | 88 | public void setOperatorsList(List operatorsList) { 89 | this.operatorsList = operatorsList; 90 | } 91 | 92 | public String getBrand() { 93 | return brand; 94 | } 95 | 96 | public void setBrand(String brand) { 97 | this.brand = brand; 98 | } 99 | 100 | public String getTargetPackageName() { 101 | return targetPackageName; 102 | } 103 | 104 | public void setTargetPackageName(String targetPackageName) { 105 | this.targetPackageName = targetPackageName; 106 | } 107 | 108 | @Override 109 | public String toString() { 110 | return JSON.toJSONString(this); 111 | } 112 | 113 | @Override 114 | public boolean equals(Object o) { 115 | if (this == o) return true; 116 | if (o == null || getClass() != o.getClass()) return false; 117 | 118 | SdkConfig sdkConfig = (SdkConfig) o; 119 | 120 | if (id != null ? !id.equals(sdkConfig.id) : sdkConfig.id != null) return false; 121 | if (sdkPlugin != null ? !sdkPlugin.equals(sdkConfig.sdkPlugin) : sdkConfig.sdkPlugin != null) return false; 122 | if (sdkPluginId != null ? !sdkPluginId.equals(sdkConfig.sdkPluginId) : sdkConfig.sdkPluginId != null) 123 | return false; 124 | if (channel != null ? !channel.equals(sdkConfig.channel) : sdkConfig.channel != null) return false; 125 | if (countryList != null ? !countryList.equals(sdkConfig.countryList) : sdkConfig.countryList != null) 126 | return false; 127 | if (operatorsList != null ? !operatorsList.equals(sdkConfig.operatorsList) : sdkConfig.operatorsList != null) 128 | return false; 129 | if (brand != null ? !brand.equals(sdkConfig.brand) : sdkConfig.brand != null) return false; 130 | return targetPackageName != null ? targetPackageName.equals(sdkConfig.targetPackageName) : sdkConfig.targetPackageName == null; 131 | } 132 | 133 | @Override 134 | public int hashCode() { 135 | int result = id != null ? id.hashCode() : 0; 136 | result = 31 * result + (sdkPlugin != null ? sdkPlugin.hashCode() : 0); 137 | result = 31 * result + (sdkPluginId != null ? sdkPluginId.hashCode() : 0); 138 | result = 31 * result + (channel != null ? channel.hashCode() : 0); 139 | result = 31 * result + (countryList != null ? countryList.hashCode() : 0); 140 | result = 31 * result + (operatorsList != null ? operatorsList.hashCode() : 0); 141 | result = 31 * result + (brand != null ? brand.hashCode() : 0); 142 | result = 31 * result + (targetPackageName != null ? targetPackageName.hashCode() : 0); 143 | return result; 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/SdkPlugin.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.jtool.codegenannotation.CodeGenField; 5 | import com.jtool.codegenbuilderplugin.test.validate.Get; 6 | import com.jtool.codegenbuilderplugin.test.validate.Modify; 7 | 8 | import javax.validation.constraints.Min; 9 | import javax.validation.constraints.NotNull; 10 | import javax.validation.constraints.Size; 11 | import javax.validation.groups.Default; 12 | 13 | public class SdkPlugin { 14 | 15 | @NotNull(groups = {Default.class, Modify.class}) 16 | @CodeGenField(value = "唯一id", groups = {Default.class, Modify.class}) 17 | private String id; 18 | 19 | @NotNull(groups = Default.class) 20 | @CodeGenField(value = "插件包的md5计算值", groups = Default.class) 21 | private String md5; 22 | 23 | @CodeGenField(value = "插件名称", groups = {Default.class, Add.class, Modify.class, Get.class}) 24 | @NotNull(groups = {Default.class, Add.class, Modify.class}) 25 | @Size(min = 1, max = 50, groups = {Default.class, Add.class, Modify.class}) 26 | private String name; 27 | 28 | @CodeGenField(value = "插件包的下载url", groups = {Default.class, Add.class, Modify.class}) 29 | @NotNull(groups = {Default.class, Add.class, Modify.class}) 30 | @Size(min = 1, max = 500, groups = {Default.class, Add.class, Modify.class}) 31 | private String url; 32 | 33 | @CodeGenField(value = "插件版本", groups = {Default.class, Add.class, Modify.class, Get.class}) 34 | @NotNull(groups = {Default.class, Add.class, Modify.class}) 35 | @Min(value = 0, groups = {Default.class, Add.class, Modify.class}) 36 | private Integer version; 37 | 38 | @CodeGenField(value = "插件包大小", groups = {Default.class, Add.class, Modify.class}) 39 | @NotNull(groups = {Default.class, Add.class, Modify.class}) 40 | @Min(value = 0, groups = {Default.class, Add.class, Modify.class}) 41 | private Integer size; 42 | 43 | @CodeGenField(value = "插件包描述", groups = {Default.class, Add.class, Modify.class}) 44 | @NotNull(groups = {Default.class, Add.class, Modify.class}) 45 | @Size(min = 1, groups = {Default.class, Add.class, Modify.class}) 46 | private String desc; 47 | 48 | @CodeGenField(value = "启动类的名称", groups = {Default.class, Add.class, Modify.class}) 49 | @Size(min = 1, max = 1000, groups = {Default.class, Add.class, Modify.class}) 50 | @NotNull(groups = {Default.class, Add.class, Modify.class}) 51 | private String startPoint; 52 | 53 | @Size(min = 1, max = 1000, groups = {Default.class, Add.class, Modify.class}) 54 | @NotNull(groups = {Default.class, Add.class, Modify.class}) 55 | @CodeGenField(value = "插件的包名", groups = {Default.class, Add.class, Modify.class}) 56 | private String pluginPackageName; 57 | 58 | public String getId() { 59 | return id; 60 | } 61 | 62 | public void setId(String id) { 63 | this.id = id; 64 | } 65 | 66 | public String getMd5() { 67 | return md5; 68 | } 69 | 70 | public void setMd5(String md5) { 71 | this.md5 = md5; 72 | } 73 | 74 | public String getName() { 75 | return name; 76 | } 77 | 78 | public void setName(String name) { 79 | this.name = name; 80 | } 81 | 82 | public String getUrl() { 83 | return url; 84 | } 85 | 86 | public void setUrl(String url) { 87 | this.url = url; 88 | } 89 | 90 | public Integer getVersion() { 91 | return version; 92 | } 93 | 94 | public void setVersion(Integer version) { 95 | this.version = version; 96 | } 97 | 98 | public Integer getSize() { 99 | return size; 100 | } 101 | 102 | public void setSize(Integer size) { 103 | this.size = size; 104 | } 105 | 106 | public String getDesc() { 107 | return desc; 108 | } 109 | 110 | public void setDesc(String desc) { 111 | this.desc = desc; 112 | } 113 | 114 | public String getStartPoint() { 115 | return startPoint; 116 | } 117 | 118 | public void setStartPoint(String startPoint) { 119 | this.startPoint = startPoint; 120 | } 121 | 122 | public String getPluginPackageName() { 123 | return pluginPackageName; 124 | } 125 | 126 | public void setPluginPackageName(String pluginPackageName) { 127 | this.pluginPackageName = pluginPackageName; 128 | } 129 | 130 | @Override 131 | public String toString() { 132 | return JSON.toJSONString(this); 133 | } 134 | 135 | @Override 136 | public boolean equals(Object o) { 137 | if (this == o) return true; 138 | if (o == null || getClass() != o.getClass()) return false; 139 | 140 | SdkPlugin sdkPlugin = (SdkPlugin) o; 141 | 142 | if (id != null ? !id.equals(sdkPlugin.id) : sdkPlugin.id != null) return false; 143 | if (name != null ? !name.equals(sdkPlugin.name) : sdkPlugin.name != null) return false; 144 | if (url != null ? !url.equals(sdkPlugin.url) : sdkPlugin.url != null) return false; 145 | if (version != null ? !version.equals(sdkPlugin.version) : sdkPlugin.version != null) return false; 146 | if (size != null ? !size.equals(sdkPlugin.size) : sdkPlugin.size != null) return false; 147 | if (desc != null ? !desc.equals(sdkPlugin.desc) : sdkPlugin.desc != null) return false; 148 | if (startPoint != null ? !startPoint.equals(sdkPlugin.startPoint) : sdkPlugin.startPoint != null) return false; 149 | if (pluginPackageName != null ? !pluginPackageName.equals(sdkPlugin.pluginPackageName) : sdkPlugin.pluginPackageName != null) 150 | return false; 151 | return md5 != null ? md5.equals(sdkPlugin.md5) : sdkPlugin.md5 == null; 152 | } 153 | 154 | @Override 155 | public int hashCode() { 156 | int result = id != null ? id.hashCode() : 0; 157 | result = 31 * result + (name != null ? name.hashCode() : 0); 158 | result = 31 * result + (url != null ? url.hashCode() : 0); 159 | result = 31 * result + (version != null ? version.hashCode() : 0); 160 | result = 31 * result + (size != null ? size.hashCode() : 0); 161 | result = 31 * result + (desc != null ? desc.hashCode() : 0); 162 | result = 31 * result + (startPoint != null ? startPoint.hashCode() : 0); 163 | result = 31 * result + (pluginPackageName != null ? pluginPackageName.hashCode() : 0); 164 | result = 31 * result + (md5 != null ? md5.hashCode() : 0); 165 | return result; 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/SearchUserApiRequest.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | import com.jtool.annotation.AvailableValues; 4 | import com.jtool.codegenannotation.CodeGenField; 5 | 6 | import javax.validation.constraints.*; 7 | 8 | public class SearchUserApiRequest { 9 | 10 | @NotNull 11 | @Size(min = 1, max = 20) 12 | @CodeGenField("用户所在国家") 13 | private String country; 14 | 15 | @NotNull 16 | @Min(0) 17 | @Max(120) 18 | @Digits(integer = 3, fraction = 0) 19 | @CodeGenField("年龄") 20 | private Integer age; 21 | 22 | @NotNull 23 | @Min(0) 24 | @Max(250) 25 | @Digits(integer = 3, fraction = 2) 26 | @CodeGenField("身高") 27 | private Double height; 28 | 29 | @CodeGenField("是否已婚, 0代表没结婚,1代表结婚") 30 | @AvailableValues(values={"0", "1"}) 31 | private String isMarried; 32 | 33 | public String getCountry() { 34 | return country; 35 | } 36 | 37 | public void setCountry(String country) { 38 | this.country = country; 39 | } 40 | 41 | public Integer getAge() { 42 | return age; 43 | } 44 | 45 | public void setAge(Integer age) { 46 | this.age = age; 47 | } 48 | 49 | public Double getHeight() { 50 | return height; 51 | } 52 | 53 | public void setHeight(Double height) { 54 | this.height = height; 55 | } 56 | 57 | public String getIsMarried() { 58 | return isMarried; 59 | } 60 | 61 | public void setIsMarried(String isMarried) { 62 | this.isMarried = isMarried; 63 | } 64 | 65 | @Override 66 | public String toString() { 67 | return "SearchUserApiRequest{" + 68 | "country='" + country + '\'' + 69 | ", age=" + age + 70 | ", height=" + height + 71 | ", isMarried=" + isMarried + 72 | '}'; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/Type.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | public enum Type { 4 | PRODUCE("A"),DEMO("B"),FREE("C"),COSTOM("D"); 5 | 6 | Type() { 7 | } 8 | 9 | Type(String value){ 10 | this.setValue(value); 11 | } 12 | 13 | private String value; 14 | 15 | public String getValue() { 16 | return value; 17 | } 18 | 19 | public void setValue(String value) { 20 | this.value = value; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/request/UploadAvatarApiRequest.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.request; 2 | 3 | import com.jtool.codegenannotation.CodeGenField; 4 | import org.springframework.web.multipart.MultipartFile; 5 | 6 | import javax.validation.constraints.DecimalMax; 7 | import javax.validation.constraints.DecimalMin; 8 | import javax.validation.constraints.NotNull; 9 | import javax.validation.constraints.Size; 10 | 11 | public class UploadAvatarApiRequest { 12 | 13 | @NotNull 14 | @Size(min = 1, max = 100) 15 | @CodeGenField("文件的md5值") 16 | private String md5; 17 | 18 | @NotNull 19 | @DecimalMin("1") 20 | @DecimalMax("9") 21 | @CodeGenField("整数类型,上传资源序号标记,就是资源是这个广播的第几个资源的序号,以1为开始") 22 | private Integer seq; 23 | 24 | @NotNull 25 | @CodeGenField("上传文件,multipart/form-data格式上传文件") 26 | private MultipartFile file; 27 | 28 | public String getMd5() { 29 | return md5; 30 | } 31 | 32 | public void setMd5(String md5) { 33 | this.md5 = md5; 34 | } 35 | 36 | public Integer getSeq() { 37 | return seq; 38 | } 39 | 40 | public void setSeq(Integer seq) { 41 | this.seq = seq; 42 | } 43 | 44 | public MultipartFile getFile() { 45 | return file; 46 | } 47 | 48 | public void setFile(MultipartFile file) { 49 | this.file = file; 50 | } 51 | 52 | @Override 53 | public String toString() { 54 | return "UploadAvatarRequest{" + 55 | "md5='" + md5 + '\'' + 56 | ", seq=" + seq + 57 | ", file=" + file + 58 | '}'; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/response/Account.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.response; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.jtool.codegenannotation.CodeGenField; 5 | import org.apache.commons.lang3.builder.EqualsBuilder; 6 | import org.apache.commons.lang3.builder.HashCodeBuilder; 7 | 8 | import java.util.List; 9 | 10 | public class Account { 11 | 12 | @CodeGenField("id") 13 | private String id; 14 | 15 | @CodeGenField("公司id") 16 | private String companyId; 17 | 18 | @CodeGenField("公司名称") 19 | private String companyName; 20 | 21 | @CodeGenField("账号登录ID") 22 | private String accountId; 23 | 24 | @CodeGenField("账号显示名称") 25 | private String accountName; 26 | 27 | //账号登录密码 28 | private String accountPw; 29 | 30 | @CodeGenField("角色权限") 31 | private List roleList; 32 | 33 | //保存用户登录的token,每次成功登录刷新 34 | private String token; 35 | 36 | @CodeGenField("角色:超组管理员、管理员、班组") 37 | private RoleCategoryEnum roleCategory; 38 | 39 | @CodeGenField("角色可操作的功能模块,客户端点击某模块时进行验证") 40 | private List roleOperator; 41 | 42 | public String getId() { 43 | return id; 44 | } 45 | 46 | public Account setId(String id) { 47 | this.id = id; 48 | return this; 49 | } 50 | 51 | public String getCompanyId() { 52 | return companyId; 53 | } 54 | 55 | public Account setCompanyId(String companyId) { 56 | this.companyId = companyId; 57 | return this; 58 | } 59 | 60 | public String getCompanyName() { 61 | return companyName; 62 | } 63 | 64 | public Account setCompanyName(String companyName) { 65 | this.companyName = companyName; 66 | return this; 67 | } 68 | 69 | public String getAccountId() { 70 | return accountId; 71 | } 72 | 73 | public Account setAccountId(String accountId) { 74 | this.accountId = accountId; 75 | return this; 76 | } 77 | 78 | public String getAccountName() { 79 | return accountName; 80 | } 81 | 82 | public Account setAccountName(String accountName) { 83 | this.accountName = accountName; 84 | return this; 85 | } 86 | 87 | public String getAccountPw() { 88 | return accountPw; 89 | } 90 | 91 | public Account setAccountPw(String accountPw) { 92 | this.accountPw = accountPw; 93 | return this; 94 | } 95 | 96 | public List getRoleList() { 97 | return roleList; 98 | } 99 | 100 | public Account setRoleList(List roleList) { 101 | this.roleList = roleList; 102 | return this; 103 | } 104 | 105 | public String getToken() { 106 | return token; 107 | } 108 | 109 | public Account setToken(String token) { 110 | this.token = token; 111 | return this; 112 | } 113 | 114 | public RoleCategoryEnum getRoleCategory() { 115 | return roleCategory; 116 | } 117 | 118 | public Account setRoleCategory(RoleCategoryEnum roleCategory) { 119 | this.roleCategory = roleCategory; 120 | return this; 121 | } 122 | 123 | public List getRoleOperator() { 124 | return roleOperator; 125 | } 126 | 127 | public Account setRoleOperator(List roleOperator) { 128 | this.roleOperator = roleOperator; 129 | return this; 130 | } 131 | 132 | @Override 133 | public String toString() { 134 | return JSON.toJSONString(this); 135 | } 136 | 137 | @Override 138 | public boolean equals(Object o) { 139 | if (this == o) return true; 140 | 141 | if (o == null || getClass() != o.getClass()) return false; 142 | 143 | Account account = (Account) o; 144 | 145 | return new EqualsBuilder() 146 | .append(id, account.id) 147 | .append(companyId, account.companyId) 148 | .append(companyName, account.companyName) 149 | .append(accountId, account.accountId) 150 | .append(accountName, account.accountName) 151 | .append(accountPw, account.accountPw) 152 | .append(roleList, account.roleList) 153 | .isEquals(); 154 | } 155 | 156 | @Override 157 | public int hashCode() { 158 | return new HashCodeBuilder(17, 37) 159 | .append(id) 160 | .append(companyId) 161 | .append(companyName) 162 | .append(accountId) 163 | .append(accountName) 164 | .append(accountPw) 165 | .append(roleList) 166 | .toHashCode(); 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/response/BaseResponse.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.response; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.jtool.codegenannotation.CodeGenField; 5 | 6 | import javax.validation.constraints.NotNull; 7 | 8 | public class BaseResponse { 9 | 10 | @NotNull 11 | @CodeGenField("状态码, 0:完成") 12 | protected Integer code = 0; 13 | 14 | public BaseResponse() { 15 | } 16 | 17 | public BaseResponse(Integer code) { 18 | this.code = code; 19 | } 20 | 21 | public Integer getCode() { 22 | return code; 23 | } 24 | 25 | public void setCode(Integer code) { 26 | this.code = code; 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return JSON.toJSONString(this); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/response/BaseSdkPluginInfo.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.response; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.jtool.codegenannotation.CodeGenField; 5 | 6 | import javax.validation.constraints.Min; 7 | import javax.validation.constraints.NotNull; 8 | import javax.validation.constraints.Size; 9 | 10 | public abstract class BaseSdkPluginInfo { 11 | 12 | @CodeGenField("插件名称") 13 | @NotNull 14 | @Size(min = 1, max = 50) 15 | protected String name; 16 | 17 | @CodeGenField("插件包的下载url") 18 | @NotNull 19 | protected String url; 20 | 21 | @CodeGenField("插件版本") 22 | @NotNull 23 | @Min(0) 24 | protected Integer version; 25 | 26 | @CodeGenField("插件包大小") 27 | @NotNull 28 | @Min(0) 29 | protected Integer size; 30 | 31 | @CodeGenField("插件包描述") 32 | @Size(min = 1) 33 | protected String desc; 34 | 35 | public String getName() { 36 | return name; 37 | } 38 | 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | 43 | public String getUrl() { 44 | return url; 45 | } 46 | 47 | public void setUrl(String url) { 48 | this.url = url; 49 | } 50 | 51 | public Integer getVersion() { 52 | return version; 53 | } 54 | 55 | public void setVersion(Integer version) { 56 | this.version = version; 57 | } 58 | 59 | public Integer getSize() { 60 | return size; 61 | } 62 | 63 | public void setSize(Integer size) { 64 | this.size = size; 65 | } 66 | 67 | public String getDesc() { 68 | return desc; 69 | } 70 | 71 | public void setDesc(String desc) { 72 | this.desc = desc; 73 | } 74 | 75 | @Override 76 | public String toString() { 77 | return JSON.toJSONString(this); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/response/CheckSdkPluginsResponse.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.response; 2 | 3 | import com.jtool.codegenannotation.CodeGenField; 4 | 5 | import javax.validation.constraints.NotNull; 6 | import java.util.List; 7 | 8 | public class CheckSdkPluginsResponse extends BaseResponse { 9 | 10 | @NotNull 11 | @CodeGenField("下次来更新的时间间隔,单位分钟") 12 | private Integer interval = 10; 13 | 14 | @CodeGenField("插件检查结果集合") 15 | @NotNull 16 | private List pluginsInfo; 17 | 18 | public Integer getInterval() { 19 | return interval; 20 | } 21 | 22 | public void setInterval(Integer interval) { 23 | this.interval = interval; 24 | } 25 | 26 | public List getPluginsInfo() { 27 | return pluginsInfo; 28 | } 29 | 30 | public void setPluginsInfo(List pluginsInfo) { 31 | this.pluginsInfo = pluginsInfo; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/response/CommonResponse.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.response; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.jtool.codegenannotation.CodeGenField; 5 | 6 | import javax.validation.constraints.NotNull; 7 | 8 | public class CommonResponse { 9 | 10 | @NotNull 11 | @CodeGenField("状态码, 0:完成") 12 | private String code; 13 | 14 | public String getCode() { 15 | return code; 16 | } 17 | 18 | public void setCode(String code) { 19 | this.code = code; 20 | } 21 | 22 | @Override 23 | public String toString() { 24 | return JSON.toJSONString(this); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/response/GetSdkPluginsResponse.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.response; 2 | 3 | import com.jtool.codegenannotation.CodeGenField; 4 | import com.jtool.codegenbuilderplugin.test.api.request.SdkPlugin; 5 | 6 | import javax.validation.constraints.NotNull; 7 | import java.util.List; 8 | 9 | public class GetSdkPluginsResponse extends BaseResponse { 10 | 11 | @NotNull 12 | @CodeGenField("sdk插件列表") 13 | private List sdkPluginsList; 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/response/LoginResponse.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.response; 2 | 3 | import com.jtool.codegenannotation.CodeGenField; 4 | 5 | import javax.validation.constraints.NotNull; 6 | 7 | public class LoginResponse extends BaseResponse { 8 | 9 | @NotNull 10 | @CodeGenField("令牌,后续接口需要带上,用来校验登陆状态") 11 | private String token; 12 | 13 | @NotNull 14 | @CodeGenField("用户的信息") 15 | private Account account; 16 | 17 | public Account getAccount() { 18 | return account; 19 | } 20 | 21 | public LoginResponse setAccount(Account account) { 22 | this.account = account; 23 | return this; 24 | } 25 | 26 | public String getToken() { 27 | return token; 28 | } 29 | 30 | public void setToken(String token) { 31 | this.token = token; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/response/OrderDetail.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.response; 2 | 3 | import com.jtool.codegenannotation.CodeGenField; 4 | 5 | import javax.validation.constraints.NotNull; 6 | import java.util.Date; 7 | 8 | /** 9 | * Created by wen on 16-6-29. 10 | */ 11 | public class OrderDetail { 12 | 13 | public OrderDetail() { 14 | } 15 | 16 | @NotNull 17 | @CodeGenField("id, Integer") 18 | private Integer id; 19 | 20 | @NotNull 21 | @CodeGenField("定单id, String") 22 | private String orderId; 23 | 24 | @NotNull 25 | @CodeGenField("定单状态(0成功,-1失败,1等待), Integer") 26 | private Integer orderStatus; 27 | 28 | @NotNull 29 | @CodeGenField("应用id,这定单对应哪个应用的, String") 30 | private String appId; 31 | 32 | @NotNull 33 | @CodeGenField("生成的交易id, String") 34 | private String tradeNo; 35 | 36 | @NotNull 37 | @CodeGenField("账号, String") 38 | private String accountId; 39 | 40 | @NotNull 41 | @CodeGenField("金额, double") 42 | private Double amount; 43 | 44 | @NotNull 45 | @CodeGenField("国家, String") 46 | private String country; 47 | 48 | @NotNull 49 | @CodeGenField("币种, String") 50 | private String currency; 51 | 52 | @NotNull 53 | @CodeGenField("支付成功后,告知商家或应用回调的接口, String") 54 | private String redirectUrl; 55 | 56 | @NotNull 57 | @CodeGenField("支付商品的说明, String") 58 | private String productDesc; 59 | 60 | @NotNull 61 | @CodeGenField("支付渠道(如:paga or interswitch), String") 62 | private String payChannel; 63 | 64 | @NotNull 65 | @CodeGenField("结果说明, String") 66 | private String resultDesc; 67 | 68 | @NotNull 69 | @CodeGenField("定单创建的时间, Date") 70 | private Date createTime; 71 | 72 | @NotNull 73 | @CodeGenField("最后修改时间, Date") 74 | private Date updateTime; 75 | 76 | public Integer getId() { 77 | return id; 78 | } 79 | 80 | public OrderDetail setId(Integer id) { 81 | this.id = id; 82 | return this; 83 | } 84 | 85 | public String getOrderId() { 86 | return orderId; 87 | } 88 | 89 | public OrderDetail setOrderId(String orderId) { 90 | this.orderId = orderId; 91 | return this; 92 | } 93 | 94 | public Integer getOrderStatus() { 95 | return orderStatus; 96 | } 97 | 98 | public OrderDetail setOrderStatus(Integer orderStatus) { 99 | this.orderStatus = orderStatus; 100 | return this; 101 | } 102 | 103 | public String getAppId() { 104 | return appId; 105 | } 106 | 107 | public OrderDetail setAppId(String appId) { 108 | this.appId = appId; 109 | return this; 110 | } 111 | 112 | public String getTradeNo() { 113 | return tradeNo; 114 | } 115 | 116 | public OrderDetail setTradeNo(String tradeNo) { 117 | this.tradeNo = tradeNo; 118 | return this; 119 | } 120 | 121 | public String getAccountId() { 122 | return accountId; 123 | } 124 | 125 | public OrderDetail setAccountId(String accountId) { 126 | this.accountId = accountId; 127 | return this; 128 | } 129 | 130 | public Double getAmount() { 131 | return amount; 132 | } 133 | 134 | public OrderDetail setAmount(Double amount) { 135 | this.amount = amount; 136 | return this; 137 | } 138 | 139 | public String getCountry() { 140 | return country; 141 | } 142 | 143 | public OrderDetail setCountry(String country) { 144 | this.country = country; 145 | return this; 146 | } 147 | 148 | public String getCurrency() { 149 | return currency; 150 | } 151 | 152 | public OrderDetail setCurrency(String currency) { 153 | this.currency = currency; 154 | return this; 155 | } 156 | 157 | public String getRedirectUrl() { 158 | return redirectUrl; 159 | } 160 | 161 | public OrderDetail setRedirectUrl(String redirectUrl) { 162 | this.redirectUrl = redirectUrl; 163 | return this; 164 | } 165 | 166 | public String getProductDesc() { 167 | return productDesc; 168 | } 169 | 170 | public OrderDetail setProductDesc(String productDesc) { 171 | this.productDesc = productDesc; 172 | return this; 173 | } 174 | 175 | public String getPayChannel() { 176 | return payChannel; 177 | } 178 | 179 | public OrderDetail setPayChannel(String payChannel) { 180 | this.payChannel = payChannel; 181 | return this; 182 | } 183 | 184 | public String getResultDesc() { 185 | return resultDesc; 186 | } 187 | 188 | public OrderDetail setResultDesc(String resultDesc) { 189 | this.resultDesc = resultDesc; 190 | return this; 191 | } 192 | 193 | public Date getCreateTime() { 194 | return createTime; 195 | } 196 | 197 | public OrderDetail setCreateTime(Date createTime) { 198 | this.createTime = createTime; 199 | return this; 200 | } 201 | 202 | public Date getUpdateTime() { 203 | return updateTime; 204 | } 205 | 206 | public OrderDetail setUpdateTime(Date updateTime) { 207 | this.updateTime = updateTime; 208 | return this; 209 | } 210 | 211 | @Override 212 | public String toString() { 213 | return "OrderDetail{" + 214 | "id=" + id + 215 | ", orderId='" + orderId + '\'' + 216 | ", orderStatus=" + orderStatus + 217 | ", appId='" + appId + '\'' + 218 | ", tradeNo='" + tradeNo + '\'' + 219 | ", accountId='" + accountId + '\'' + 220 | ", amount=" + amount + 221 | ", country='" + country + '\'' + 222 | ", currency='" + currency + '\'' + 223 | ", redirectUrl='" + redirectUrl + '\'' + 224 | ", productDesc='" + productDesc + '\'' + 225 | ", payChannel='" + payChannel + '\'' + 226 | ", resultDesc='" + resultDesc + '\'' + 227 | ", createTime=" + createTime + 228 | ", updateTime=" + updateTime + 229 | '}'; 230 | } 231 | } 232 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/response/Pages.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.response; 2 | 3 | import com.jtool.codegenannotation.CodeGenField; 4 | 5 | public class Pages { 6 | 7 | @CodeGenField("结果共有都少页") 8 | private Integer totalPage; 9 | 10 | @CodeGenField("结果集的版本号") 11 | private String version; 12 | 13 | public Integer getTotalPage() { 14 | return totalPage; 15 | } 16 | 17 | public void setTotalPage(Integer totalPage) { 18 | this.totalPage = totalPage; 19 | } 20 | 21 | public String getVersion() { 22 | return version; 23 | } 24 | 25 | public void setVersion(String version) { 26 | this.version = version; 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return "Pages{" + 32 | "totalPage=" + totalPage + 33 | ", version='" + version + '\'' + 34 | '}'; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/response/QuerySingleResponse.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.response; 2 | 3 | import com.jtool.codegenannotation.CodeGenField; 4 | 5 | import javax.validation.constraints.NotNull; 6 | 7 | /** 8 | * Created by wen on 16-6-30. 9 | */ 10 | public class QuerySingleResponse { 11 | 12 | public QuerySingleResponse() { 13 | } 14 | 15 | public QuerySingleResponse(OrderDetail order) { 16 | this.code = "0"; 17 | this.order = order; 18 | } 19 | 20 | @NotNull 21 | @CodeGenField("0:成功") 22 | private String code; 23 | 24 | @NotNull 25 | @CodeGenField("订单详情") 26 | private OrderDetail order; 27 | 28 | public String getCode() { 29 | return code; 30 | } 31 | 32 | public QuerySingleResponse setCode(String code) { 33 | this.code = code; 34 | return this; 35 | } 36 | 37 | public OrderDetail getOrder() { 38 | return order; 39 | } 40 | 41 | public QuerySingleResponse setOrder(OrderDetail order) { 42 | this.order = order; 43 | return this; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/response/RoleCategoryEnum.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.response; 2 | 3 | public enum RoleCategoryEnum { 4 | SUPER("SUPER"), MANAGER("MANAGER"), TEAM("TEAM"); 5 | 6 | private RoleCategoryEnum(String type){ 7 | this.type = type; 8 | } 9 | 10 | private String type; 11 | 12 | public String getType() { 13 | return type; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/response/RoleOperatorEnum.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.response; 2 | 3 | public enum RoleOperatorEnum { 4 | //新建工单/派发/删除、接收/编辑/分派、审核、领用材料 5 | CREATE("CREATE"), MANAGE("MANAGE"), CHECK("CHECK"), TAKE("TAKE"); 6 | 7 | private RoleOperatorEnum(String type){ 8 | this.type = type; 9 | } 10 | 11 | private String type; 12 | 13 | public String getType() { 14 | return type; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/response/SdkPluginsResponseItem.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.response; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.jtool.annotation.AvailableValues; 5 | import com.jtool.codegenannotation.CodeGenField; 6 | 7 | import javax.validation.constraints.NotNull; 8 | 9 | public class SdkPluginsResponseItem { 10 | 11 | @CodeGenField("插件id") 12 | @NotNull 13 | private String id; 14 | 15 | @CodeGenField("插件下载url") 16 | @NotNull 17 | private String url; 18 | 19 | @CodeGenField("插件版本") 20 | @NotNull 21 | private Integer version; 22 | 23 | @CodeGenField("插件状态, 可能值有: OK, NEED_UPDATE, DELETE") 24 | @NotNull 25 | @AvailableValues(values = {"OK", "NEED_UPDATE", "DELETE"}) 26 | private String status; 27 | 28 | public String getId() { 29 | return id; 30 | } 31 | 32 | public void setId(String id) { 33 | this.id = id; 34 | } 35 | 36 | public String getUrl() { 37 | return url; 38 | } 39 | 40 | public void setUrl(String url) { 41 | this.url = url; 42 | } 43 | 44 | public Integer getVersion() { 45 | return version; 46 | } 47 | 48 | public void setVersion(Integer version) { 49 | this.version = version; 50 | } 51 | 52 | public String getStatus() { 53 | return status; 54 | } 55 | 56 | public void setStatus(String status) { 57 | this.status = status; 58 | } 59 | 60 | @Override 61 | public String toString() { 62 | return JSON.toJSONString(this); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/response/SearchUserApiResponse.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.response; 2 | 3 | import com.jtool.codegenannotation.CodeGenField; 4 | 5 | import javax.validation.constraints.NotNull; 6 | import java.util.List; 7 | import java.util.Set; 8 | 9 | public class SearchUserApiResponse { 10 | 11 | @NotNull 12 | @CodeGenField("状态码, 0:完成") 13 | private String code; 14 | 15 | @CodeGenField("查找返回的用户列表") 16 | private List users; 17 | 18 | @CodeGenField("结果的分页信息") 19 | private Pages pages; 20 | 21 | @CodeGenField("url的字符串列表") 22 | private List urls; 23 | 24 | @CodeGenField("电话字符串列表") 25 | private Set tels; 26 | 27 | public String getCode() { 28 | return code; 29 | } 30 | 31 | public void setCode(String code) { 32 | this.code = code; 33 | } 34 | 35 | public List getUsers() { 36 | return users; 37 | } 38 | 39 | public void setUsers(List users) { 40 | this.users = users; 41 | } 42 | 43 | public Pages getPages() { 44 | return pages; 45 | } 46 | 47 | public void setPages(Pages pages) { 48 | this.pages = pages; 49 | } 50 | 51 | public List getUrls() { 52 | return urls; 53 | } 54 | 55 | public void setUrls(List urls) { 56 | this.urls = urls; 57 | } 58 | 59 | public Set getTels() { 60 | return tels; 61 | } 62 | 63 | public void setTels(Set tels) { 64 | this.tels = tels; 65 | } 66 | 67 | @Override 68 | public String toString() { 69 | return "SearchUserApiResponse{" + 70 | "code='" + code + '\'' + 71 | ", users=" + users + 72 | ", pages=" + pages + 73 | ", urls=" + urls + 74 | ", tels=" + tels + 75 | '}'; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/response/UploadAvatarApiResponse.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.response; 2 | 3 | import com.jtool.codegenannotation.CodeGenField; 4 | 5 | import javax.validation.constraints.NotNull; 6 | 7 | public class UploadAvatarApiResponse { 8 | 9 | @NotNull 10 | @CodeGenField("状态码, 0:完成") 11 | private String code; 12 | 13 | @CodeGenField("文件base64") 14 | private String fileContent; 15 | 16 | public String getCode() { 17 | return code; 18 | } 19 | 20 | public void setCode(String code) { 21 | this.code = code; 22 | } 23 | 24 | public String getFileContent() { 25 | return fileContent; 26 | } 27 | 28 | public void setFileContent(String fileContent) { 29 | this.fileContent = fileContent; 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return "UploadAvatarResponse{" + 35 | "code='" + code + '\'' + 36 | ", fileContent='" + fileContent + '\'' + 37 | '}'; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/api/response/User.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.api.response; 2 | 3 | import com.jtool.codegenannotation.CodeGenField; 4 | 5 | import javax.validation.constraints.NotNull; 6 | 7 | public class User { 8 | 9 | @NotNull 10 | @CodeGenField("用户名称") 11 | private String name; 12 | 13 | @NotNull 14 | @CodeGenField("用户所在国家") 15 | private String country; 16 | 17 | @NotNull 18 | @CodeGenField("年龄") 19 | private Integer age; 20 | 21 | @NotNull 22 | @CodeGenField("身高") 23 | private Double height; 24 | 25 | @CodeGenField("是否已婚") 26 | private Boolean isMarried; 27 | 28 | public String getName() { 29 | return name; 30 | } 31 | 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | 36 | public String getCountry() { 37 | return country; 38 | } 39 | 40 | public void setCountry(String country) { 41 | this.country = country; 42 | } 43 | 44 | public Integer getAge() { 45 | return age; 46 | } 47 | 48 | public void setAge(Integer age) { 49 | this.age = age; 50 | } 51 | 52 | public Double getHeight() { 53 | return height; 54 | } 55 | 56 | public void setHeight(Double height) { 57 | this.height = height; 58 | } 59 | 60 | public Boolean getIsMarried() { 61 | return isMarried; 62 | } 63 | 64 | public void setIsMarried(Boolean isMarried) { 65 | this.isMarried = isMarried; 66 | } 67 | 68 | @Override 69 | public String toString() { 70 | return "User{" + 71 | "name='" + name + '\'' + 72 | ", country='" + country + '\'' + 73 | ", age=" + age + 74 | ", height=" + height + 75 | ", isMarried=" + isMarried + 76 | '}'; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/controller/DemoController.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.controller; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.jtool.codegenannotation.CodeGenApi; 5 | import com.jtool.codegenannotation.CodeGenRequest; 6 | import com.jtool.codegenannotation.CodeGenResponse; 7 | import com.jtool.codegenbuilderplugin.test.api.request.BaseResponse; 8 | import com.jtool.codegenbuilderplugin.test.api.request.User; 9 | import com.jtool.codegenbuilderplugin.test.api.request.*; 10 | import com.jtool.codegenbuilderplugin.test.api.response.*; 11 | import com.jtool.codegenbuilderplugin.test.exception.BackEndException; 12 | import com.jtool.codegenbuilderplugin.test.exception.ParamException; 13 | import com.jtool.codegenbuilderplugin.test.validate.Get; 14 | import com.jtool.validator.ParamBeanValidator; 15 | import org.slf4j.Logger; 16 | import org.slf4j.LoggerFactory; 17 | import org.springframework.stereotype.Controller; 18 | import org.springframework.web.bind.annotation.RequestBody; 19 | import org.springframework.web.bind.annotation.RequestMapping; 20 | import org.springframework.web.bind.annotation.RequestMethod; 21 | import org.springframework.web.bind.annotation.ResponseBody; 22 | 23 | import javax.servlet.http.HttpServletRequest; 24 | import java.io.IOException; 25 | import java.util.ArrayList; 26 | import java.util.Base64; 27 | import java.util.List; 28 | 29 | @Controller 30 | public class DemoController { 31 | 32 | private Logger log = LoggerFactory.getLogger(this.getClass()); 33 | 34 | @CodeGenApi(name = "查找用户", description = "根据用户国家,年纪,身高,是否结婚等条件过滤查找用户", genSDK = true) 35 | @CodeGenRequest(SearchUserApiRequest.class) 36 | @CodeGenResponse(SearchUserApiResponse.class) 37 | @ResponseBody 38 | @RequestMapping(value = "/searchUser", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") 39 | public String searchUser(SearchUserApiRequest searchUserApiRequest) throws ParamException, BackEndException { 40 | 41 | 42 | log.debug("请求获得参数:" + searchUserApiRequest); 43 | 44 | log.debug("请求参数是否合法:" + !ParamBeanValidator.isNotValid(searchUserApiRequest)); 45 | 46 | if(ParamBeanValidator.isNotValid(searchUserApiRequest)) { 47 | return "{\"code\":0}"; 48 | } 49 | 50 | Pages pages = new Pages(); 51 | pages.setTotalPage(100); 52 | pages.setVersion("abcdefghijklmn"); 53 | 54 | List urls = new ArrayList<>(); 55 | urls.add("http://www.google.com"); 56 | urls.add("http://www.facebook.com"); 57 | 58 | List userList = new ArrayList<>(); 59 | 60 | User user = new User(); 61 | user.setHeight(searchUserApiRequest.getHeight()); 62 | user.setName("用户1"); 63 | user.setAge(searchUserApiRequest.getAge()); 64 | user.setCountry(searchUserApiRequest.getCountry()); 65 | user.setIsMarried(!searchUserApiRequest.getIsMarried().equals("0")); 66 | 67 | userList.add(user); 68 | 69 | User user1 = new User(); 70 | user1.setHeight(searchUserApiRequest.getHeight()); 71 | user1.setName("用户2"); 72 | user1.setAge(searchUserApiRequest.getAge()); 73 | user1.setCountry(searchUserApiRequest.getCountry()); 74 | user1.setIsMarried(searchUserApiRequest.getIsMarried().equals("0")); 75 | 76 | userList.add(user1); 77 | 78 | SearchUserApiResponse searchUserApiResponse = new SearchUserApiResponse(); 79 | searchUserApiResponse.setCode("0"); 80 | searchUserApiResponse.setPages(pages); 81 | searchUserApiResponse.setUrls(urls); 82 | searchUserApiResponse.setUsers(userList); 83 | 84 | return JSON.toJSONString(searchUserApiResponse); 85 | } 86 | 87 | @CodeGenApi(name = "上传用户头像", description = "上传用户头像") 88 | @CodeGenRequest(UploadAvatarApiRequest.class) 89 | @CodeGenResponse(UploadAvatarApiResponse.class) 90 | @ResponseBody 91 | @RequestMapping(value = "/uploadAvatar", method = {RequestMethod.POST}, produces = "application/json;charset=UTF-8") 92 | public String uploadAvatar(UploadAvatarApiRequest uploadAvatarRequest) throws ParamException, BackEndException, IOException { 93 | 94 | log.debug("请求获得参数:" + uploadAvatarRequest); 95 | 96 | log.debug("请求参数是否合法:" + !ParamBeanValidator.isNotValid(uploadAvatarRequest)); 97 | 98 | if(ParamBeanValidator.isNotValid(uploadAvatarRequest)) { 99 | return "{\"code\":0}"; 100 | } 101 | 102 | UploadAvatarApiResponse uploadAvatarResponse = new UploadAvatarApiResponse(); 103 | uploadAvatarResponse.setCode("0"); 104 | uploadAvatarResponse.setFileContent(Base64.getEncoder().encodeToString(uploadAvatarRequest.getFile().getBytes())); 105 | 106 | return JSON.toJSONString(uploadAvatarResponse); 107 | } 108 | 109 | @CodeGenApi(name = "获取信息", description = "无参数请求获取信息", forWho = "client") 110 | @CodeGenResponse(SearchUserApiResponse.class) 111 | @ResponseBody 112 | @RequestMapping(value = "/getUser", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") 113 | public String getUser() throws ParamException, BackEndException { 114 | 115 | SearchUserApiResponse searchUserApiResponse = new SearchUserApiResponse(); 116 | searchUserApiResponse.setCode("0"); 117 | 118 | return JSON.toJSONString(searchUserApiResponse); 119 | } 120 | 121 | @CodeGenApi(name = "response是DIY", description = "测试response是DIY", genSDK = false) 122 | @ResponseBody 123 | @RequestMapping(value = "/getDIY", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") 124 | public String getDIY() throws ParamException, BackEndException { 125 | 126 | SearchUserApiResponse searchUserApiResponse = new SearchUserApiResponse(); 127 | searchUserApiResponse.setCode("0"); 128 | 129 | return JSON.toJSONString(searchUserApiResponse); 130 | } 131 | 132 | @CodeGenApi(name = "/order/querySingle/", description = "创建订单") 133 | @CodeGenRequest(QuerySingleRequest.class) 134 | @CodeGenResponse(QuerySingleResponse.class) 135 | @RequestMapping(value = "/{order}/querySingle/{myId}", method = RequestMethod.POST, produces = "application/json;charset=utf-8") 136 | public @ResponseBody 137 | QuerySingleResponse querySingle(QuerySingleRequest querySingleRequest){ 138 | return null; 139 | } 140 | 141 | @RequestMapping(value = "/{currency}/pay/pre", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json;charset=utf-8") 142 | @CodeGenApi(name = "预支付", description = "提交支付信息,生成定单,并显示确认页面") 143 | @CodeGenRequest(PrePaymentRequest.class) 144 | public String paymentPre(HttpServletRequest request, PrePaymentRequest preRequest) throws Exception { 145 | return null; 146 | } 147 | 148 | 149 | @CodeGenApi(name = "添加广告", description = "添加不同的广告") 150 | @CodeGenRequest(value = AppFullAd.class, isRest = true) 151 | @CodeGenResponse(CommonResponse.class) 152 | @ResponseBody 153 | @RequestMapping(value = "/subClass", method = {RequestMethod.POST}, produces = "application/json;charset=UTF-8") 154 | public String subClass(@RequestBody String requestBodyStr) throws ParamException, BackEndException, IOException { 155 | return null; 156 | } 157 | 158 | @CodeGenApi(name="检查插件", description = "检查插件是否需要更新") 159 | @CodeGenRequest(value = CheckSdkPluginsRequest.class, isRest = true, groups = Add.class) 160 | @CodeGenResponse(CheckSdkPluginsResponse.class) 161 | @RequestMapping(value = "/checkSdkPlugins", method = RequestMethod.POST) 162 | public String checkSdkPlugins() { 163 | 164 | return null; 165 | } 166 | 167 | @CodeGenApi(name = "获取sdk配置", description = "根据channel的名字获取该channel下的全部配置", docSeq = 7) 168 | @CodeGenRequest(value = Channel.class, isRest = true, groups = Add.class) 169 | @CodeGenResponse(value = GetSdkConfigResponse.class, groups = Get.class) 170 | @RequestMapping(value = "/getSdkConfig", method = RequestMethod.GET) 171 | @ResponseBody 172 | public String getSdkConfig(Channel channel) throws ParamException { 173 | return null; 174 | } 175 | 176 | 177 | @CodeGenApi(name = "测试枚举请求", description = "测试枚举请求", docSeq = 7) 178 | @CodeGenRequest(value = EnumTypeRequest.class, isRest = true) 179 | @CodeGenResponse(value = BaseResponse.class) 180 | @RequestMapping(value = "/enumType", method = RequestMethod.POST) 181 | @ResponseBody 182 | public String enumType(EnumTypeRequest enumTypeRequest) throws ParamException { 183 | return null; 184 | } 185 | 186 | @CodeGenApi(name = "获取sdk插件列表", description = "获取整个sdk插件列表", docSeq = 1) 187 | @CodeGenResponse(GetSdkPluginsResponse.class) 188 | @RequestMapping(value = "/getSdkPlugins", method = RequestMethod.GET) 189 | public String getSdkPlugins() { 190 | return null; 191 | } 192 | 193 | @CodeGenApi(name = "登陆", description = "使用账号(目前是手机号)和密码登陆系统", docSeq = 1) 194 | @CodeGenRequest(value = LoginRequest.class, isRest = true) 195 | @CodeGenResponse(LoginResponse.class) 196 | @RequestMapping(value = "/login", method = RequestMethod.POST) 197 | public String login() { 198 | return null; 199 | } 200 | 201 | @CodeGenApi(name = "用户", description = "用户", docSeq = 1) 202 | @CodeGenRequest(value = AddUserRequest.class, isRest = true) 203 | @CodeGenResponse(LoginResponse.class) 204 | @RequestMapping(value = "/addUser", method = RequestMethod.POST) 205 | public String addUser() { 206 | return null; 207 | } 208 | 209 | } 210 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/exception/BackEndException.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.exception; 2 | 3 | public class BackEndException extends Exception { 4 | public ExceptionTypeEnum exceptionTypeEnum = ExceptionTypeEnum.BACK_END_EXCEPTION; 5 | } 6 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/exception/ExceptionTypeEnum.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.exception; 2 | 3 | import com.jtool.codegenannotation.CodeGenExceptionTypeEnum; 4 | 5 | @CodeGenExceptionTypeEnum 6 | public enum ExceptionTypeEnum { 7 | 8 | PARAMS_EXCEPTION("-3","参数错误"), 9 | BACK_END_EXCEPTION("-98", "后端错误"); 10 | 11 | private String code; 12 | private String desc; 13 | 14 | private ExceptionTypeEnum(String code, String desc) { 15 | this.code = code; 16 | this.desc = desc; 17 | } 18 | 19 | public String getCode() { 20 | return code; 21 | } 22 | 23 | public void setCode(String code) { 24 | this.code = code; 25 | } 26 | 27 | public String getDesc() { 28 | return desc; 29 | } 30 | 31 | public void setDesc(String desc) { 32 | this.desc = desc; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/exception/ParamException.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.exception; 2 | 3 | public class ParamException extends Exception { 4 | public ExceptionTypeEnum exceptionTypeEnum = ExceptionTypeEnum.PARAMS_EXCEPTION; 5 | } 6 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/util/TestContextClassLoader.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.util; 2 | 3 | import com.jtool.codegenbuilderplugin.classLoader.ClassLoaderInterface; 4 | 5 | /** 6 | * Created by jialechan on 15/6/24. 7 | */ 8 | public class TestContextClassLoader implements ClassLoaderInterface { 9 | 10 | public Class loadClass(String name) throws ClassNotFoundException { 11 | return Class.forName(name); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/validate/Add.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.validate; 2 | 3 | public interface Add { 4 | } 5 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/validate/Get.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.validate; 2 | 3 | public interface Get { 4 | } 5 | -------------------------------------------------------------------------------- /src/test/java/com/jtool/codegenbuilderplugin/test/validate/Modify.java: -------------------------------------------------------------------------------- 1 | package com.jtool.codegenbuilderplugin.test.validate; 2 | 3 | public interface Modify { 4 | } 5 | --------------------------------------------------------------------------------