在应用程序上工作时,我也想将我的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导致此冲突。但从以上信息中很难理解。
最佳答案
免责声明:我是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>
基本上与使用FQCN时用Java编写的方式完全相同。
关于java - 从模式生成JAXB绑定(bind)类的Jackson属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50130787/