-1

尝试使用高源/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)

测试_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 类的类路径中。

4

1 回答 1

1

您显然对此注释有疑问:

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

可能有两个原因:

您必须制作Views.Public.class完全限定的类名。

您必须在 中包含依赖项,如下plugin所示maven-jaxb2-plugin

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

包括所有依赖项。

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

更新

我检查了你的测试项目

<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不能被解析为Classes 数组。

将您的自定义更改为:

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

解决问题。

两句话。

  • 我已经要求您在https://github.com/highsource/jaxb2-annotate-plugin-support中创建一个拉取请求,您已经使用 GPLv3 许可证创建了自己的存储库/项目。我将无法复制/保留此示例jaxb2-annotate-plugin-support以供将来参考和帮助其他用户。
  • 您可以从一开始就发布完整的错误日志,如果没有测试项目,解决方案将非常明显。
于 2016-12-26T10:35:26.470 回答