首页 > 解决方案 > Generate JSON Schema as several files using Jsonix

问题描述

I have several XSD files, separated by requests and subjects, however the JSON Schema that I get as an output is one big file with thousands of lines. I would like to know if there is a setting to output the generated JSON Schema as several files using Jsonix. It would be easier to navigate in.

Below is the Jsonix part of my pom.xml:

    <plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <version>0.13.3</version>
        <executions>
            <execution>
                <goals>
                    <goal>generate</goal>
                </goals>
                <configuration>

                    <extension>true</extension>
                    <args>
                        <arg>-Xannotate</arg>
                        <arg>-Xjsonix</arg>
                        <arg>-Xjsonix-generateJsonSchema</arg>  
                    </args>
                    <plugins>
                        <plugin>
                            <groupId>org.hisrc.jsonix</groupId>
                            <artifactId>jsonix-schema-compiler</artifactId>
                            <version>2.3.10</version>
                        </plugin>

                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics-annotate</artifactId>
                            <version>1.0.2</version>
                        </plugin>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-annotate-plugin-test-annox-annotations</artifactId>
                            <version>1.0.0</version>
                        </plugin>
                        <plugin>
                            <groupId>io.swagger</groupId>
                            <artifactId>swagger-annotations</artifactId>
                            <version>1.5.10</version>
                        </plugin>
                    </plugins>


                    <schemaDirectory>src/main/resources</schemaDirectory>
                    <schemaIncludes>
                        <include>**/*.xsd</include>
                    </schemaIncludes>

                    <bindingDirectory>src/main/resources</bindingDirectory>
                    <bindingIncludes>
                        <include>**/*.xjb</include>
                    </bindingIncludes>

                    <generatePackage>com.my.awesome.package</generatePackage>
                    <generateDirectory>${project.build.directory}/generated-sources/jsonSchema</generateDirectory>

                    <verbose>true</verbose>

                </configuration>
            </execution>
        </executions>
    </plugin>

标签: maven-pluginjsonix

解决方案


Author of Jsonix here.

I would like to know if there is a setting to output the generated JSON Schema as several files using Jsonix.

Yes, please read about modules and mappings.

In short a mapping more-or-less corresponds to one package in Java. A module is a collection of one or more mappings, is also a unit of generation.

You can configure the compiler to generate modules containing specific mappings.

If I understand your case correctly, you have a large schema and you'd like to divide mappings or JSON schemas into several files.

Here's how I'd approach it.

  • Configure several mappings for your schema. You can specify exactly which type, elements, etc. go in which mappings. See includes configuration for this. Make sure to give your mappings unique names.
  • Condigure several modules each of which would contain a subset of mappings.

I have to say I haven't really tried it in this scenario. Normally people do the opposite thing - group several mappings in one module. But I see no reason for this not to work.

An example configuration might look something like:

<jsonix:module
  name="MyRequests">
  <jsonix:mapping package="com.my.awesome.package" name="MyRequests">
    <jsonix:includes>
      <jsonix:type name="MyRequestType"/>
      ...
    </jsonix:includes>
  <jsonix:mapping>
</jsonix:module>
<jsonix:module
  name="MyResponses">
  <jsonix:mapping package="com.my.awesome.package" name="MyResponses">
    <jsonix:includes>
      <jsonix:type name="MyResponseType"/>
      ...
    </jsonix:includes>
  <jsonix:mapping>
</jsonix:module>

I'm not 100% sure this will work, but this is a good starting point.


推荐阅读