从架构为JAXB绑定类生成Jackson属性

时间:2018-05-02 09:01:55

标签: java annotations jaxb

在应用程序上工作时,我也希望将JAXB类用于JSON绑定。

因此,对于某些课程,我需要使用@JsonTypeInfo进行注释。

所以我想我会使用自定义绑定。

以下是我的XJB文件的样子:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<jaxb:bindings version="1.0" 
              xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
              xmlns:xs="http://www.w3.org/2001/XMLSchema" 
              xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
              xmlns:annox="http://annox.dev.java.net"
              jaxb:extensionBindingPrefixes="xjc">

   <jaxb:bindings schemaLocation="../schema/im/vehicle.xsd">
      <jaxb:bindings node="xs:complexType[@name='Vehicle']">
        <annox:annotate target="class">
          <annox:annotate annox:class="com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY, property = 'subVehicleType')"/>
        </annox:annotate>
      </jaxb:bindings>
   </jaxb:bindings>

</jaxb:bindings>

这就是我想要的课程:

@JsonTypeInfo(use=Id.CLASS, include=As.PROPERTY, property="subVehicleType")

但我一直收到以下错误:

org.jvnet.annox.parser.exception.AnnotationElementParseException: Could not parse the annotation element.
    at org.jvnet.jaxb2_commons.plugin.annotate.AnnotatePlugin.annotate(AnnotatePlugin.java:364)
    at org.jvnet.jaxb2_commons.plugin.annotate.AnnotatePlugin.annotateClassOutline(AnnotatePlugin.java:311)
    at org.jvnet.jaxb2_commons.plugin.annotate.AnnotatePlugin.processClassOutline(AnnotatePlugin.java:190)
    at org.jvnet.jaxb2_commons.plugin.annotate.AnnotatePlugin.run(AnnotatePlugin.java:152)
    at com.sun.tools.xjc.model.Model.generateCode(Model.java:294)
    at org.jvnet.mjiip.v_2_2.XJC22Mojo.generateCode(XJC22Mojo.java:61)
    at org.jvnet.mjiip.v_2_2.XJC22Mojo.doExecute(XJC22Mojo.java:36)
    at org.jvnet.mjiip.v_2_2.XJC22Mojo.doExecute(XJC22Mojo.java:22)
    at org.jvnet.jaxb2.maven2.RawXJC2Mojo.doExecute(RawXJC2Mojo.java:282)
    at org.jvnet.jaxb2.maven2.RawXJC2Mojo.execute(RawXJC2Mojo.java:147)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:90)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
    at java.lang.reflect.Method.invoke(Method.java:508)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)

我做错了吗?

更新

我在答案中提到过:它在我的TEST独立项目中有效,但是当我在我的主项目中执行相同操作时,我得到以下API Incompatibility error

[ERROR] Failed to execute goal org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:0.13.2:generate (default) on project Domain: Execution default of goal org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:0.13.2:generate failed: An API incompatibility was encountered while executing org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:0.13.2:generate: java.lang.NoSuchMethodError: japa/parser/JavaParser.parse(Ljava/io/Reader;Z)Ljapa/parser/ast/CompilationUnit; (loaded from file:/C:/JavaDev/.m2/repository/com/google/code/javaparser/javaparser/1.0.8/javaparser-1.0.8.jar by java.net.URLClassLoader@ce3ebdfb) called from class org.jvnet.annox.japa.parser.AnnotationExprParser (loaded from file:/C:/JavaDev/.m2/repository/org/jvnet/annox/annox/1.0.2/annox-1.0.2.jar by java.net.URLClassLoader@ce3ebdfb).

我不确定哪个其他api导致了这种冲突。但是从上面的消息中很难理解。

1 个答案:

答案 0 :(得分:1)

免责声明:我是jaxb2-annotate-plugin的作者。

正确的语法是:

<annox:annotate target="class">
      @com.fasterxml.jackson.annotation.JsonTypeInfo(
          use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME,
          include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY,
          property = "subVehicleType")
</annox:annotate>

基本上你将如何用Java编写它,但使用FQCN。