Custom annotation with jaxb2-annotate-plugin and XJC tool

痞子三分冷 提交于 2019-12-25 03:51:10

问题


I'm trying to convert bunch of XSD files to Java source POJOs at runtime. These XML schemas will be generated based on some protocol specific model definitions.

I have used the JAXB's XJC to compile the schema to pojo. Started facing problem when I used Jaxb2-annotate-plugin for doing custom annotation.

For testing, I have added following in my schema,

<xs:schema xmlns:aa="http://acme.example.com/system" 
           xmlns:another="http://acme.example.com/another" 
           xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
           xmlns:ncn="urn:ietf:params:xml:ns:netconf:notification:1.0" 
           jaxb:extensionBindingPrefixes="annox" 
           xmlns:annox="http://annox.dev.java.net"  
           targetNamespace="http://acme.example.com/system" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

XJC started giving the following error from there after,

Unsupported binding namespace "http://annox.dev.java.net". Perhaps you meant "http://java.sun.com/xml/ns/jaxb/xjc"?

As per suggestions provided in various sources, added required JARS in XJC's classpath as follows, but nothing seems to workout for me.

xjc -p com.test -classpath="jaxb2-basics-annotate-1.0.2.jar;tools-0.4.1.5.jar;comm‌​on‌​s-logging-1.1.1.jar;jaxb2-basics-runtime-0.9.5;annox-1.0.1.ja‌​r;javaparser-1.0.11.jar;jaxb2-basics-tools-0.9.5.jar" -extension myschema.xsd

I couldn't find a proper example for generating java sources using XJC with jaxb2-annotate-plugin. I cannot add this in my maven/ant, as I mentioned I've to generate xsd and pojo at runtime.

If using the XJC tool as java process is the only way, please let me know,

  • What are the dependent jars to be included in classpath?
  • How to activate the plugin and -Xannotate switch?

If the pojo generation can be done in other ways with this plugin, please suggest the same.

I'm a newbie in jaxb. Any suggestions would be really helpful. Thanks in advance!


回答1:


Disclaimer: I'm the author of the jaxb2-annotate-plugin.

First, iff with xjc you mean the binary xjc distributed with JDK (like xjc.exe under Windows), XJC plugins (including jaxb2-annotate-plugin) don't work with xjc binary from JDK.

The reason is that when packaging XJC for JDK, the XJC packages get renamed from com.sun.tools.xjc to com.sun.tools.internal.xjc. Third-party XJC plugins extend com.sun.tools.xjc.Plugin, not com.sun.tools.internal.xjc.Plugin, this is why they don't work with JDK-packaged XJC.

Next, you say you generate code in the runtime. I don't quite understand how this is supposed to work, you'll need to compile the classes afterwards etc. But OK, I'll assume you know what you do. Anyway, you should not execute XJC via command line in this case. You have a programmatic API via com.sun.tools.xjc.api.XJC. Check this question and answers:

  • Add toString, hashCode, equals while generating JAXB classes in Java
  • How do I add an xjc plugin programmatically at runtime?

Related:

  • How to use jaxb2-annotate-plugin with XJC in command line


来源:https://stackoverflow.com/questions/33729913/custom-annotation-with-jaxb2-annotate-plugin-and-xjc-tool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!