简体   繁体   中英

Generating Jackson attributes for JAXB binding class from schema

Working on an application, I want to use my JAXB classes for JSON bindings as well.

So for some classes I need to annotate with @JsonTypeInfo .

So I figured I would use Customized bindings.

Here is how my XJB file looks like:

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

This is what I want on my class:

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

But I keep getting following error:

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)

Am i doing something wrong?

UPDATE

I did as mentioned in answer: It worked in my TEST standalone project but when I do that same in my main project I get this following 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).

I am not sure which other api is causing this conflict. but its really hard to understand from this above message.

Disclaimer: I am the author of the jaxb2-annotate-plugin .

The correct syntax would be:

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

Basically exactly how you would write this in Java but with FQCNs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM