Gradle - Generating serializable classes with jaxb - java

I'm using gradle as build tool and configuring jaxb plugin jacobono. Have the below entry in build.gradle file:
jaxb {
bindingsDir = "src/main/resources/xjb"
xjc {
xsdDir = "src/main/resources/xsd"
generatePackage = "some.package"
}
}
under xjb directory, have binding.xml with the below content:
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xsi:schemaLocation="
http://java.sun.com/xml/ns/jaxb
http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
version="2.1">
<globalBindings>
<serializable uid="1" />
</globalBindings>
</bindings>
Although the files are getting generated, none of them implements serializable interface as i have defined in binding xml. I doubt on the binding dir configurations.
Any advice will be much helpful.

Moved the binding information to xsds, and it worked.
Not sure whether the plugin works properly with binging dir paramter as it didnt work even with absolute path.

Related

Jaxb: How to specify a default class for an XSD element

When generating Java classes from a XSD, how can I specify that for some specific node, a specific and already existent Java class should be used instead of trying to generate one?
Thank you very much.
You can use episode file to reference the existing classes. .episode files are just jaxb bindings file and has mappings between elements and java classes.
a) if those existing classes are also generated from (another) xsd. use below option to first create .episode file.
xjc -episode a.episode a.xsd
then use this a.episode that contains the mappings as input to the next xjc generation.
xjc b.xsd -extension -b a.episode
b) If you want to refer some random classes, then you may have to write your own episode file providing mapping between element and class reference like below.
sample.episode
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" if-exists="true" version="2.1">
<jaxb:bindings scd="x-schema::">
<jaxb:bindings scd="employee">
<jaxb:class ref="www1.example.Employee"/>
<jaxb:package name="www1.example" />
</jaxb:bindings>
</jaxb:bindings>
and use xjc b.xsd -extension -b sample.episode
You should use following binding customization
<bindings version="2.0" xmlns="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" xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix">
<bindings schemaLocation="../schema/yourSchema.xsd">
<bindings node="//xs:complexType[#name='Foo']">
<class ref="com.FooImpl"/>
</bindings>
</bindings>
</bindings>

Creating custom binding for generating wsdl client proxy using wsimport

I have a requirement to generate client proxy java files in Eclipse IDE.
I tried to generate this via wsimport command. Below is the command I used.
wsimport -keep -b bindings.xml -p com.aasc.carrier.shipexec.proxy -implServiceName wcfSoxContract -implPortName wcfShip http://shipexec.com/demo/wcf/soap?wsdl -B-XautoNameResolution
After executing this I am getting below issue.
[ERROR] Invalid operation "InstantiateWCF", can't generate java method parameter. Local name of the wrapper child "package" in the global element "{......}InstantiateWCF" is a java keyword. Use customization to change the parametername.
line 1 of http://shipexec.com/demo/wcf/soap?wsdl=wsdl0
I tried to resolve this issue using the custom binding xml file as below.
<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
wsdlLocation="http://shipexec.com/demo/wcf/soap?wsdl">
<enableWrapperStyle>false</enableWrapperStyle>
<jaxws:bindings node="wsdl:definitions/wsdl:import/wsdl:portType[#name='IwcfShip']/wsdl:operation[#name='InstantiateWCF']">
<jaxws:parameter part="wsdl:definitions/wsdl:message[#name='IwcfShip_InstantiateWCF_InputMessage']/wsdl:part[#name='parameters']" childElementName="package" name="paramPackage" />
</jaxws:bindings>
</jaxws:bindings>
But the node I am trying to get is not recognizing.
Can anyone please help on finding the exact node path for changing the name of the parameter for 'InstantiateWCF' operation.
Thanks,
Y Pradeep
I have resolved this issue by changing binding.xml file to as below.
<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
wsdlLocation="http://shipexec.com/demo/wcf/soap?wsdl=wsdl0">
<jaxws:bindings node="wsdl:definitions" >
<jaxws:bindings node="//wsdl:portType[#name='IwcfShip']/wsdl:operation[#name='InstantiateWCF']">
<jaxws:parameter part="wsdl:definitions/wsdl:message[#name='IwcfShip_InstantiateWCF_InputMessage']/wsdl:part[#name='parameters']" childElementName="tns:package" name="paramPackage" />
</jaxws:bindings>
</jaxws:bindings>
</jaxws:bindings>
And also modified wsimport command as below.
wsimport -keep -p com.aasc.carrier.shipexec.proxy -implServiceName wcfSoxContract -implPortName wcfShip http://shipexec.com/demo/wcf/soap?wsdl -B-XautoNameResolution -b bindings.xml
Thanks,
Y Pradeep

JAXB marshalling CDATA with Maven plugin

This is my general problem: I want everything to be automated as much as it can be.
This is my specific problem: I want to marshal/unmarshal CDATA. I know that I can specify my own adapter as here. For building process I use Maven and its maven-jaxb2-plugin. Is there some option how to annotate elements in .xsd so they will be automatically annotated by my custom adapter? I really don't want to manually change those after each build.
Any other ways how to solve my problem are more than welcome. :)
EDIT: I've ran to a subproblem and it is described there: JAXB binding - "unable to honor this conversion customization".
try this configuration
<bindings version="2.0" xmlns="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"
xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix">
<bindings schemaLocation="yourXSD.xsd">
<bindings node="//xsd:complexType[#name='Certificate']//xsd:sequence//xsd:element[#name='certificate']">
<xjc:javaType name="java.security.cert.X509Certificate" adapter="adapters.X509CertificateAdapter" />
</bindings>
<bindings node="//xsd:complexType[#name='User']//xsd:sequence//xsd:element[#name='certificate']">
<xjc:javaType name="java.security.cert.X509Certificate" adapter="adapters.X509CertificateAdapter" />
</bindings>
</bindings>
</bindings>
i solved this issue on this link
this configuration work fine if is an xmltype like xs:string, xs:date, etc.
Try this configuration to solve your problem, using XmlJavaTypeAdapter.

How to generate #HandlerChain using wsimport

I'm generating Java from WSDL using wsimport (JAX-WS 2.1.3) and need to make it generate a #HandlerChain annotation. So I create a JAX-WS binding file:
<jaxws:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:javaee="http://java.sun.com/xml/ns/javaee"
wsdlLocation="../etc/MessageStudio.wsdl">
<jaxws:bindings node="wsdl:definitions">
<javaee:handler-chain>
<javaee:handler-chain-name>StrongmailHandlers</javaee:handler-chain-name>
<javaee:handler>
<javaee:handler-name>OrganizationTokenHandler</javaee:handler-name>
<javaee:handler-class>com.bossmedia.strongmailadapter.deliveryadapter.OrganizationTokenHandler</javaee:handler-class>
</javaee:handler>
</javaee:handler-chain>
</jaxws:bindings>
and run the wsimport Ant task:
<wsimport
wsdl="../etc/MessageStudio.wsdl"
sourcedestdir="../src/gen"
destdir="../classes"
verbose="false"
binding="../etc/jaxws.bindings.xml">
</wsimport>
but I get no annotation and no handler chain XML file. Googling only finds me solutions for changing packages, methods and arguments and the JAX-WS RI page from where I copied the XML.
Could you help me find the flaw in my configuration or another way, short of modifying the generated code, to get my handler into the chain?
The answer is that the RI example is incorrect. There needs to be a handler-chains wrapper tag:
<jaxws:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:javaee="http://java.sun.com/xml/ns/javaee"
wsdlLocation="../etc/MessageStudio.wsdl">
<jaxws:bindings node="wsdl:definitions">
<javaee:handler-chains>
<javaee:handler-chain>
<javaee:handler-chain-name>StrongmailHandlers</javaee:handler-chain-name>
<javaee:handler>
<javaee:handler-name>OrganizationTokenHandler</javaee:handler-name>
<javaee:handler-class>com.bossmedia.strongmailadapter.deliveryadapter.OrganizationTokenHandler</javaee:handler-class>
</javaee:handler>
</javaee:handler-chain>
</javaee:handler-chains>
</jaxws:bindings>
It turns out that the generated handler chain file is put in the output directory (target/package) where the compiled class files are. If you want to change that, you can use the "destDir" option to do that. Also, set "xnocompile" to false else the generated class files will end up in the same directory as the handler file. Wish they didn't do such a half a**ed job at documentation.
There is still an error in the example. The tag <javaee:handler-chain-name> does not exist according to the XML scheme (http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/javaee_web_services_metadata_handler_2_0.xsd).
The correction is:
<jaxws:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:javaee="http://java.sun.com/xml/ns/javaee"
wsdlLocation="../etc/MessageStudio.wsdl">
<jaxws:bindings node="wsdl:definitions">
<javaee:handler-chains>
<javaee:handler-chain>
<javaee:handler>
<javaee:handler-name>OrganizationTokenHandler</javaee:handler-name>
<javaee:handler-class>com.bossmedia.strongmailadapter.deliveryadapter.OrganizationTokenHandler</javaee:handler-class>
</javaee:handler>
</javaee:handler-chain>
</javaee:handler-chains>

wsdl:import issue with custom binding in CXF

I am not able to get a custom binding work for an XSD imported in a WSDL using wsdl:import tag.
I guess wsdl:type and xsd:import is the best way to import schema from an XSD but I am in need of a solution where I do not have to change the WSDL.
I am using CXF for generating the artifacts from the WSDL and it works fine when I do not use any custom binding but since my requirement is to change the name of the classes defined in the schema I intend to use a custom binding.
WSDL contains:
<definitions name="MMMWS" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xmmmd="http://xyz.com/abcdata"
xmlns:xmmms="http://xyz.com/abcservice"
targetNamespace="http://xyz.com/abcservice">
<import namespace="http://xyz.com/abcdata" location="abcdata.xsd"/>
.....
Binding file (.xjb) contains:
<jaxb:bindings version="2.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"
jaxb:extensionBindingPrefixes="xjc"
schemaLocation="abcdata.xsd">
.....
The error I get is:
[ERROR] "file:abcdata.xsd" is not a part of this compilation. Is this a mistake for "file:abcdata.xjb"?
[ERROR] at line 7 column 51 of schema file:abcdata.xjb

Categories

Resources