package com.zhangmeng.tools.json2Pojo; import org.jsonschema2pojo.*; import org.jsonschema2pojo.rules.RuleFactory; /** * @author : 芊芊墨客 * @version : 1.0 * @date : 2023-04-21 09:20 * * * 源代码链接地址: https://blog.csdn.net/weixin_39651356/article/details/127155659 */ public class MyJsonschema2pojoConfig { /** * 总体配置 * @param includeGetAndSetFlag 是否需要get\set代码 * @return */ public static GenerationConfig getGenerationConfig(boolean includeGetAndSetFlag) { return new DefaultGenerationConfig() { @Override public boolean isIncludeAllPropertiesConstructor() { return false; } /** * 使用json内容进行构建javaBean * @return */ @Override public SourceType getSourceType() { return SourceType.JSON; } @Override public boolean isGenerateBuilders() { // set config option by overriding method return false; } @Override public AnnotationStyle getAnnotationStyle() { return AnnotationStyle.NONE; } @Override public boolean isIncludeAdditionalProperties() { return false; } @Override public boolean isIncludeGetters() { return includeGetAndSetFlag; } @Override public boolean isIncludeSetters() { return includeGetAndSetFlag; } @Override public boolean isIncludeToString() { return false; } @Override public boolean isSerializable() { return true; } @Override public boolean isIncludeGeneratedAnnotation() { return false; } @Override public boolean isIncludeHashcodeAndEquals() { return false; } @Override public String getTargetVersion() { return "1.8"; } @Override public InclusionLevel getInclusionLevel() { return InclusionLevel.ALWAYS; } }; } public static GenerationConfig getGenerationConfig() { return getGenerationConfig(false); } /** * 生成的注解配置 * @param generationConfig * @return */ public static MyAbstractTypeInfoAwareAnnotator getAnnotator(GenerationConfig generationConfig) { return new MyAbstractTypeInfoAwareAnnotator(generationConfig); } /** * 自定义总体配置+注解配置 * @return */ public static SchemaMapper getSchemaMapper(GenerationConfig config, MyAbstractTypeInfoAwareAnnotator myAbstractTypeInfoAwareAn) { return new SchemaMapper(new RuleFactory(config, myAbstractTypeInfoAwareAn, new SchemaStore()), new SchemaGenerator()); } /** * 默认总体配置+注解配置 * @return */ public static SchemaMapper getDefaultSchemaMapper(AnnotateConfig annotateConfig) { GenerationConfig generationConfig = getGenerationConfig(); MyAbstractTypeInfoAwareAnnotator annotator = getAnnotator(generationConfig); annotator.setLombokAnnotFlag(annotateConfig.isLombook_enable()); annotator.setSwaggerAnnotFlag(annotateConfig.isSwagger_enable()); annotator.setMybatisPlusAnnotFlag(annotateConfig.isMybatis_plus_enable()); annotator.setJacksonAnnotFlag(annotateConfig.isJackson_enable()); return getSchemaMapper(generationConfig,annotator); } }