来自xsd的自定义注释

时间:2016-12-23 20:35:54

标签: java xsd jaxb annotations jaxb2-annotate-plugin

尝试使用高源/ jaxb2-annotate-plugin库从XSD生成的类上生成自定义注释,但会出现一些错误。 需要使用JsonView批注生成类,但无法解析该批注。 Jackson库已经在路径中(支持/拥有JsonView),但它仍然失败。无法解读还有什么可能是错的。

@lexicore - 我相信你可以提供帮助,因为我看到你是Github上这个库的开发者。我确实按照GitHub上的文档进行了操作,但无法弄清楚。 请告知我是否遗漏了任何设置。

错误摘录

[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** 

(显示支持JsonView注释和支持类的依赖项jar)。          http://maven.apache.org/xsd/maven-4.0.0.xsd">       4.0.0                一个         视图         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>

注意:Jackson-annotations-2.8.3 jar在类路径中,它有JsonView类。

1 个答案:

答案 0 :(得分:1)

您显然对此注释有问题:

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

可能有两个原因:

您必须使Views.Public.class完全合格的班级名称。

您必须在[{1}}中将依赖项包含为plugin,如下所示:

maven-jaxb2-plugin

包括所有依赖项。

如果没有任何帮助,请在此处向我发送有关MCVE的公关:https://github.com/highsource/jaxb2-annotate-plugin-support

<强>更新

我已检查过您的test project

        <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>

我收到以下错误:

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

这说明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 无法解析为baseModel.Views.Internal.class es的数组。

将您的自定义分配到:

Class

解决问题。

两个评论。

  • 我已经要求您在https://github.com/highsource/jaxb2-annotate-plugin-support中创建拉取请求,您已使用GPLv3许可证创建了自己的回购/项目。我将无法复制/保留此示例<annox:annotate target="field">@com.fasterxml.jackson.annotation.JsonView({baseModel.Views.Internal.class})</annox:annotate> 以供将来参考和帮助其他用户。
  • 您可以从一开始就发布完整的错误日志,那么在没有测试项目的情况下解决方案就会非常明显。