custom annotation from xsd

2.4k views Asked by At

Trying to use high source/jaxb2-annotate-plugin library to generate custom annotations on the generated classes from XSD but getting some errors. Need to generate the class with JsonView annotation but it fails to parse that annotation. The Jackson library is already in the path ( which supports/has JsonView) but still it fails. Unable to decipher what else could be wrong.

@lexicore - I believe you might be able to help as I see you are the developer of this library on Github. I did follow the docs on GitHub but couldn't figure out. Please advise if I am missing anything in setting up.

ERROR snippet:

[INFO] Sources are not up-to-date, XJC will be executed.
[ERROR] Error while generating code.Location [ file:/C:/dev/workspace/JSONVIEW/jsonViewModel/schema/src/main/resources/address.xsd{20,56}].
org.xml.sax.SAXParseException; systemId: file:/C:/dev/workspace/JSONVIEW/jsonViewModel/schema/src/main/resources/address.xsd; lineNumber: 20; columnNumber: 56; Error parsing annotation.
    at org.jvnet.jaxb2_commons.plugin.annotate.AnnotatePlugin.annotate(AnnotatePlugin.java:460)
    at org.jvnet.jaxb2_commons.plugin.annotate.AnnotatePlugin.annotate(AnnotatePlugin.java:418)
    at org.jvnet.jaxb2_commons.plugin.annotate.AnnotatePlugin.processFieldOutline(AnnotatePlugin.java:200)
    at org.jvnet.jaxb2_commons.plugin.annotate.AnnotatePlugin.processClassOutline(AnnotatePlugin.java:188)
    at org.jvnet.jaxb2_commons.plugin.annotate.AnnotatePlugin.run(AnnotatePlugin.java:146)

Test_1.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    jaxb:version="2.1"
    xmlns:annox="http://annox.dev.java.net"
    jaxb:extensionBindingPrefixes="annox">

    <xsd:complexType name="TType">
        <xsd:annotation>
            <xsd:appinfo>
                <annox:annotate>@java.lang.SuppressWarnings({"unchecked","rawtypes"})</annox:annotate>
                <annox:annotate target="package">@javax.annotation.Generated({"XJC","JAXB2 Annotate Plugin"})</annox:annotate>
            </xsd:appinfo>
        </xsd:annotation>
        <xsd:sequence>
            <xsd:element name="TField" type="xsd:string">
                <xsd:annotation>
                    <xsd:appinfo>
                        <annox:annotate target="field">@com.fasterxml.jackson.annotation.JsonView(Views.Public.class)</annox:annotate>
                    </xsd:appinfo>
                </xsd:annotation>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

**POM.xml** 

( showing dependency jars which support JsonView annotation and supporting classes ). http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 A View 0.0.1-SNAPSHOT

  <artifactId>schema</artifactId>

    <properties>
        <xsd.build.dir>${basedir}/src/main/resources</xsd.build.dir>
        <generated.source.location>${basedir}/target/generated-sources/src</generated.source.location>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics</artifactId>
            <version>0.6.4</version>
        </dependency>
        <dependency>
            <groupId>B</groupId>
            <artifactId>View</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
       <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.12</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.12</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <generateDirectory>${generated.source.location}</generateDirectory>
                    <schemaDirectory>${xsd.build.dir}</schemaDirectory>
                    <addIfExistsToEpisodeSchemaBindings>true</addIfExistsToEpisodeSchemaBindings>
                    <extension>true</extension>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics-annotate</artifactId>
                             <version>1.0.2</version>
                        </plugin>
                    </plugins>
                    <args>
                        <arg>-Xannotate</arg>
                    </args>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>attach-sources</id>
                        <phase>DISABLE_FORKED_LIFECYCLE_MSOURCES-13</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Note: Jackson-annotations-2.8.3 jar is in the classpath which has JsonView class.

1

There are 1 answers

2
lexicore On

You apparently have a problem with this annotation:

@com.fasterxml.jackson.annotation.JsonView(Views.Public.class)

There may be two reasons:

You have to make Views.Public.class fully-qualified class name.

You have to include the dependency as plugin in maven-jaxb2-plugin, like this:

        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <configuration>
                <extension>true</extension>
                <args>
                    <arg>-Xannotate</arg>
                </args>
                <plugins>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics-annotate</artifactId>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.jackson</groupId>
                        <artifactId>whatever-artifactId-is</artifactId>
                        <version>whatever-version-is</version>
                    </plugin>
                </plugins>
            </configuration>
        </plugin>

Include all you dependencies.

If nothing helps, send me a PR with MCVE here: https://github.com/highsource/jaxb2-annotate-plugin-support

Update

I've checked your test project:

<annox:annotate target="field">@com.fasterxml.jackson.annotation.JsonView(baseModel.Views.Internal.class)</annox:annotate>

I got the following error:

Caused by: org.jvnet.annox.parser.exception.ValueParseException: Could not parse the value [baseModel.Views.Internal.class] into the target class [[Ljava.lang.Class;].
        at org.jvnet.annox.parser.java.visitor.ExpressionVisitor.visitDefault(ExpressionVisitor.java:20)
        at org.jvnet.annox.parser.java.visitor.ExpressionVisitor.visitDefault(ExpressionVisitor.java:9)
        at org.jvnet.annox.japa.parser.ast.visitor.AbstractGenericExpressionVisitor.visit(AbstractGenericExpressionVisitor.java:290)
        at japa.parser.ast.expr.ClassExpr.accept(ClassExpr.java:49)
        at org.jvnet.annox.parser.XArrayAnnotationFieldParser.parse(XArrayAnnotationFieldParser.java:97)
        ... 38 more

What this says is that baseModel.Views.Internal.class cannot be parsed as an array of Classes.

Chaning your customization to:

<annox:annotate target="field">@com.fasterxml.jackson.annotation.JsonView({baseModel.Views.Internal.class})</annox:annotate>

Solves the problem.

Two remarks.

  • I've asked you to create a pull request in https://github.com/highsource/jaxb2-annotate-plugin-support, you've created your own repo/project instead, with the GPLv3 license. I will not be able to copy/keep this example jaxb2-annotate-plugin-support for future reference and help for other users.
  • You could have posted the full error log from the very start, the solution would have been pretty obvious without test project then.