You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求支持多文件导出POJO生成JSON Schema的工具库

Great question! Generating split JSON schemas from POJOs with proper cross-references is a common need, and while mbknor's library works well for single schemas, there are better tools tailored for multi-file output. Here are my top recommendations:

1. Victools JSON Schema Generator

This library is purpose-built for flexible JSON Schema generation and explicitly supports splitting schemas into individual files with correct $ref references. It integrates seamlessly with Jackson and gives you full control over how each POJO's schema is named, exported, and referenced.

Key Highlights:

  • Automatically tracks all referenced POJOs and generates separate schema files for each.
  • Configurable reference naming (e.g., use simple class names or fully qualified paths as filenames).
  • Supports all major JSON Schema draft versions (draft-07, draft-2019-09, etc.).
  • Extensible with modules for custom annotations, type handling, or validation rules.

Quick Setup & Example:

First, add the Maven dependencies (use the latest version available):

<dependency>
    <groupId>com.github.victools</groupId>
    <artifactId>jsonschema-generator</artifactId>
    <version>4.21.0</version>
</dependency>
<dependency>
    <groupId>com.github.victools</groupId>
    <artifactId>jsonschema-module-jackson</artifactId>
    <version>4.21.0</version>
</dependency>

Then, configure the generator to output split schemas:

import com.github.victools.jsonschema.generator.*;
import com.github.victools.jsonschema.module.jackson.JacksonModule;
import com.fasterxml.jackson.databind.JsonNode;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class SplitSchemaGenerator {
    public static void main(String[] args) throws IOException {
        // Initialize config with Jackson module to introspect POJOs
        SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(
                SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON)
                .with(new JacksonModule());
        
        // Define how referenced schemas are named (matches output file names)
        configBuilder.with(new SchemaGeneratorConfigBuilder.ReferenceSchemaNamingStrategy() {
            @Override
            public String getReferenceName(TypeScope scope) {
                // Use simple class name as the schema file name (e.g., "User.json")
                return scope.getType().getSimpleName() + ".json";
            }
        });
        
        SchemaGenerator generator = new SchemaGenerator(configBuilder.build());
        
        // Generate schema for your root POJO (triggers generation of all referenced POJOs)
        JsonNode rootSchema = generator.generateSchema(RootPojo.class);
        
        // Create output directory if it doesn't exist
        File schemaDir = new File("schemas");
        schemaDir.mkdirs();
        
        // Write root schema to file
        Files.write(new File(schemaDir, "RootPojo.json").toPath(), 
                    rootSchema.toPrettyString().getBytes());
        
        // Write all referenced POJO schemas to individual files
        generator.getGeneratedSchemas().forEach((type, schema) -> {
            try {
                String fileName = type.getSimpleName() + ".json";
                Files.write(new File(schemaDir, fileName).toPath(), 
                            schema.toPrettyString().getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
}

This will create a schemas folder with each POJO's schema as a standalone JSON file, and cross-references between schemas will use relative paths like "$ref": "User.json".

2. Jackson JSON Schema Module with Custom Output Handling

If you want to stick with the Jackson ecosystem, you can extend its built-in JsonSchemaGenerator to split schemas into files. The default implementation generates a single schema, but you can hook into its reference resolution process to track and write each referenced POJO's schema separately.

Approach:

  • Use a custom SchemaFactoryWrapper to track all referenced types during schema generation.
  • After generating the root schema, iterate over the tracked types, write each to its own file, and update references to point to the correct relative file paths.

This requires more custom code than Victools, but it’s a solid option if you’re already heavily invested in Jackson.

3. OpenAPI Generator (For API-Focused POJOs)

If your POJOs are part of a REST API model (e.g., used in Spring Boot endpoints), the OpenAPI Generator can generate a full OpenAPI specification that includes separate schema definitions in the components/schemas section. You can then extract these into individual files using a simple script or tool.

Example:

Use the OpenAPI Maven plugin to scan your POJOs and generate the OpenAPI YAML/JSON spec, then split the schemas into separate files using a lightweight parser (like Jackson) to extract each entry from the schemas section.


All these tools ensure that cross-POJO relationships are correctly represented with $ref pointers instead of inline schemas, making your schema set modular and maintainable.

内容的提问来源于stack exchange,提问作者Plamen Paskov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:16:44