JAXB different datatypes parsing per namespace - java

I have got the JAXB bindings config file like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.1">
<jaxb:globalBindings generateElementProperty="false">
<jaxb:javaType name="java.util.Date" xmlType="xs:dateTime"
parseMethod="com.utils.EmptyDateTimeAdapter.parseDateTime"
printMethod="com.utils.EmptyDateTimeAdapter.printDateTimeWithZone"/>
<xjc:serializable uid="-1"/>
</jaxb:globalBindings>
</jaxb:bindings>
In xml above I declared that every wsdl dateTime element will be parsed by these methods.
My question is:
How can I change this JAXB config file to specify multiple parse methods for element type per package name or per wsdl?

<jaxws:bindings wsdlLocation="SampleService.wsdl"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<jaxws:bindings node="wsdl:definitions/wsdl:types/xs:schema[#targetNamespace='http://example.com/service/SampleService/']">
<jxb:globalBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:javaType name="java.util.Date" xmlType="xs:date"
parseMethod="org.apache.cxf.tools.common.DataTypeAdapter.parseDateTime"
printMethod="org.apache.cxf.tools.common.DataTypeAdapter.printDateTime"/>
</jxb:globalBindings>
</jaxws:bindings>
</jaxws:bindings>
references :
JBoss Community Archive (Read Only)
JAXB bindings and xs:date to java.util.Date
Custom Jax-b bindings for xs:date to JAVA 8 java.time.LocalDate using Adapter

Related

Two classes have the same XML type name "objectFactory" - org.glassfish.jaxb.runtime and jakarta.xml.bind

In my maven based project,
import jakarta.xml.bind.JAXBContext;
...
private String contextPath = "com.abc.core:com.bcd.model:com.efg.xyz";
...
JAXBContext jc = JAXBContext.newInstance(contextPath);
When i try to call JAXBContext.newInstance(contextPath), i get the below runtime exception,
org.glassfish.jaxb.runtime.v2.runtime.IllegalAnnotationsException: 3 counts of IllegalAnnotationExceptions
Two classes have the same XML type name "objectFactory". Use #XmlType.name and #XmlType.namespace to assign different names to them.
this problem is related to the following location:
at com.abc.core.ObjectFactory
this problem is related to the following location:
at com.bcd.model.ObjectFactory
Two classes have the same XML type name "objectFactory". Use #XmlType.name and #XmlType.namespace to assign different names to them.
this problem is related to the following location:
at com.bcd.model.ObjectFactory
this problem is related to the following location:
at com.efg.xyz.ObjectFactory
In the pom.xml of the current package where i am getting this error, i have added
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>3.0.1</version>
<scope>runtime</scope>
</dependency>
However, i haven't added this glassfish jaxb explicitly in the packages com.abc.core,com.bcd.model,com.efg.xyz.
I tried checking if apart from my declaration, someone else also creating an objectFactory due to which i get this exception, but no clue yet. Can you please help here?
.xjb bindings in package : com.abc.core
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 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" jaxb:version="2.1">
<jaxb:globalBindings>
<jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime" parseMethod="jakarta.xml.bind.DatatypeConverter.parseDateTime" printMethod="jakarta.xml.bind.DatatypeConverter.printDateTime" />
<xjc:simple />
</jaxb:globalBindings>
</jaxb:bindings>
and in com.bcd.model
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings 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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
version="2.1">
<jaxb:globalBindings>
<jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime" parseMethod="jakarta.xml.bind.DatatypeConverter.parseDateTime" printMethod="jakarta.xml.bind.DatatypeConverter.printDateTime" />
<xjc:simple />
</jaxb:globalBindings>
</jaxb:bindings>

How to rename a xsd element during xjc java class generation?

I'm generating java classes from xsd with cxf-xjc-plugin.
<?xml version="1.0" encoding="utf-16"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="result">
...
</xsd>
I'm trying to rename the root element using a jaxb-binding.xml file:
<jaxb:bindings
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"
jaxb:version="2.1">
<jaxb:bindings schemaLocation="xsd/myxsd.xsd" node="//xsd:element[#name='result']">
<jaxb:class name="MyNewName" />
</jaxb:bindings>
</jaxb:bindings>
But it does not work. The generated java class is named Result. What might be wrong here?

How to trim String during JAXB MARSHALLING

I am using JAXB to marshal Java Objects to XML.
http://highlevelbits.com/2009/08/jaxb-with-maven_16.html
I have tried setting the collapsedstringadapter Adapter to my JAXB generated classes.
But it appears collapsedstringadapter will only Trim the String values during unmarshal and during marshal it returns the String un-trimmed.
Can some one point to me on how to trim String during marshal via JAXB?
Write and configure your own adapter? Just as you'd configure CollapsedStringAdapter?
<?xml version="1.0" encoding="UTF-8"?>
<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">
<jaxb:bindings schemaLocation="myschema.xsd" node="/xs:schema">
<jaxb:globalBindings>
<xjc:javaType name="java.lang.String" xmlType="xs:string"
adapter="com.acme.foo.bind.annotation.adapters.BiCollapsedStringAdapter"/>
</jaxb:globalBindings>
</jaxb:bindings>
</jaxb:bindings>

JAXB Root Class Rename using binding.xml

I use the Dali plugin in Eclipse to generate Java classes using an xsd file, which essentially just invokes xjc on the schema file. I used the advice here to resolve naming conflicts by applying an XML binding file to the class generation. This worked well, but I tried to take it a step further by renaming the root element, and the result was that I lost the XmlRootElement annotation. I tried using annox to add the root element annotation back in, but I get this error: Unsupported binding namespace "http://annox.dev.java.net". Perhaps you meant "http://java.sun.com/xml/ns/jaxb/xjc"?
Here is my initial binding.xml file (without annox):
<jaxb:bindings 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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
version="2.1">
<!-- Force all classes to be generated at the top level, this will potentially cause name conflicts -->
<jaxb:globalBindings localScoping="toplevel"/>
<jaxb:bindings schemaLocation="mySchema-1.0.0.xsd">
<!-- Rename the root element -->
<jaxb:bindings node="//xs:element[#name='MyRootClassNameIsReallyLong']/xs:complexType">
<jaxb:class name="ShorterName"/>
</jaxb:bindings>
<!-- Rename the Bar class to resolve a naming conflict -->
<jaxb:bindings node="//xs:element[#name='Foo']/xs:complexType/xs:sequence/xs:element[#name='Bar']/xs:complexType">
<jaxb:class name="FooBar"/>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
By the way, it's worth noting that the schema file came from a third party, so I have no interest in modifying it. Likewise, I'd rather not tamper with the generated Java files, so that's why I am interested in the binding xml approach.
Edit (9/11/2013) - Here is the binding XML with annox:
<jaxb:bindings 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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
xmlns:annox="http://annox.dev.java.net"
version="2.1">
<!-- Force all classes to be generated at the top level, this will potentially cause name conflicts -->
<jaxb:globalBindings localScoping="toplevel"/>
<jaxb:bindings schemaLocation="mySchema-1.0.0.xsd">
<!-- Rename the root element -->
<jaxb:bindings node="//xs:element[#name='MyRootClassNameIsReallyLong']/xs:complexType">
<jaxb:class name="ShorterName"/>
<annox:annotate>
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="MyRootClassNameIsReallyLong" />
</annox:annotate>
</jaxb:bindings>
<!-- Rename the Bar class to resolve a naming conflict -->
<jaxb:bindings node="//xs:element[#name='Foo']/xs:complexType/xs:sequence/xs:element[#name='Bar']/xs:complexType">
<jaxb:class name="FooBar"/>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
Annox is an XJC add on, so you in addition to declaring the namespace prefx (xmlns:annox="http://annox.dev.java.net), you also need to declare it as an extensionBindingPrefix.
Your openning tag should then look like this:
<jaxb:bindings 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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb
http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
jaxb:extensionBindingPrefix="annox"
version="2.1">

How to rename complex type in a WSDL during stub generation

I am using wsimport
I need to rename a xs:complextype name=Address to prevent some build conflicts.
Here is a snippet of the WSDL:
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:ns="http://fedex.com/ws/rate/v13" xmlns:s1="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://fedex.com/ws/rate/v13" name="RateServiceDefinitions">
<types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://fedex.com/ws/rate/v13">
<xs:complexType name="Address">…</xs:complexType>
....
</types>
....
</definitions>
I am using an external binding file:
<jxb:bindings
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:ns="http://fedex.com/ws/rate/v13"
xmlns:s1="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="2.1"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<jxb:globalBindings>
<jxb:javaType name="java.util.Calendar" xmlType="xs:dateTime" parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime" printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />
</jxb:globalBindings>
<jxb:bindings node="definitions/types/xs:schema/xs:complexType[#name='Address']/xs:complexType">
<!-- change java method name from addNumbers() to add() -->
<jxb:class name="FedExAddress"/>
</jxb:bindings>
</jxb:bindings>
When I execute the build I get the following message:
[wsimport] [ERROR] XPath evaluation of "definitions/types/xs:schema/xs:complexType[#name='Address']/xs:complexType" results in empty target node
[wsimport] line 14 of file:/Users/davidboyd/projects/heritage/hybris/bin/custom/cpdeliveryservice/fedex_binding.xml
I have looked at the following postings here, here and a couple of references as well reference 1 and reference 2
But do not understand as to why this is not working.
After taking a breather I was able to solve the issue my taking a second look at this.
So my resulting binding is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxws:bindings
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="2.1"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
wsdlLocation="/RateService_v13.wsdl"
>
<enableWrapperStyle>true</enableWrapperStyle>
<enableAsyncMapping>false</enableAsyncMapping>
<!-- convert all xs:dateTime to java type of Calendar -->
<jaxws:globalBindings>
<jxb:javaType name="java.util.Calendar" xmlType="xs:dateTime" parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime" printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />
</jaxws:globalBindings>
<!-- Rename Address to FedExAddress -->
<jaxws:bindings node="//xs:complexType[#name='Address']">
<jxb:class name="FedExAddress"/>
</jaxws:bindings>
</jaxws:bindings>

Categories

Resources