I'm using CXF's wsdl2java tool to create a java web service.
I have a wsdl file and a few XSD files and I know that it's possible to use a binding file to map namespaces and packages. My binding file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" jaxb:version="2.0">
<jaxb:bindings schemaLocation="SchemeA.xsd" node="/xsd:schema">
<jaxb:schemaBindings>
<jaxb:package name="com.test.package.a" />
</jaxb:schemaBindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="SchemeB.xsd" node="/xsd:schema">
<jaxb:schemaBindings>
<jaxb:package name="com.test.package.b" />
</jaxb:schemaBindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="SchemeC.xsd" node="/xsd:schema">
<jaxb:schemaBindings>
<jaxb:package name="com.test.package.c" />
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
My files:
A.wsdl (imports all .xsd files)
SchemeA.xsd
SchemeB.xsd
SchemeC.xsd
It works great for everything in this XSD schemes but not for wsdl's definition. I mean at the end my packages looks like this:
com.test.package.a
com.test.package.b
com.test.package.c
https.package_test_com.a.service
The last line bother me, and I would like it to look like this: com.test.package.a.service
The binding file is used by jaxb that manages bindings of paramater and responses but webservice is directly managed by cxf or jax-ws so you will need to specify this binding with -p option of wsdl2java as specified here http://cxf.apache.org/docs/wsdl-to-java.html
Related
I'm trying to generate JAVA code with jaxb and spring but I can't get it to work when I have as wsdl file with 2 simpleTypes with the same name but in different namespaces. Does anyone know how I can solve this?
I've been trying out the jaxb:factoryMethod tag but I can't get the syntax correct. But maybe there is a simpler way?
binding.xjb
<?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"
xmlns:annox="http://annox.dev.java.net"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
jaxb:extensionBindingPrefixes="xjc annox"
version="2.1"
targetNamespace="http://com.company/generated"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<jaxb:globalBindings>
</jaxb:globalBindings>
<jaxb:bindings
node="/wsdl:definitions/wsdl:types/xs:schema[namespace::*[.='http://com.company/storetaxinformation']]/xs:simpleType[#name='TypeOfTax']"
schemaLocation="../../../target/classes/disb.wsdl">
<annox:annotateClass>#javax.xml.bind.annotation.XmlRootElement(name="TypeOfTaxStoreTax")</annox:annotateClass>
</jaxb:bindings>
<jaxb:bindings
node="/wsdl:definitions/wsdl:types/xs:schema[namespace::*[.='http://com.company/gettaxinformation']]/xs:simpleType[#name='TypeOfTax']"
schemaLocation="../../../target/classes/disb.wsdl">
<annox:annotateClass>#javax.xml.bind.annotation.XmlRootElement(name="TypeOfTaxInfo")</annox:annotateClass>
</jaxb:bindings>
</jaxb:bindings>
error.log
[ERROR] Error while generating code.Location [ file:/C:/wsdl/disb.wsdl{49,52}].
com.sun.istack.SAXParseException2; systemId: file:/C:/wsdl/disb.wsdl;
lineNumber: 49; columnNumber: 52; Two declarations cause a collision in the ObjectFactory class.
No, #XmlRootElement won't help. It's about method names in ObjectFactory.
Are you sure it's simple types which cause a collision? XJC is pointing out to methods in the ObjectFactory, so it should be global elments, not simple types
Here's an example of the factoryMethod customization:
<jaxb:bindings
schemaLocation="http://schemas.opengis.net/citygml/texturedsurface/1.0/texturedSurface.xsd"
node="/xs:schema">
<jaxb:bindings node="xs:element[#name='_Appearance']">
<jaxb:factoryMethod name="AAppearance"/>
</jaxb:bindings>
</jaxb:bindings>
You'll need to find out which elements cause a collision and customize them. Not the simple types.
We have solved this now and it was the <generatePackage> element that was causing part of the problem. We also made sure to bind each schema/namespace to its own package. This way the ObjectFactory Classes won't complain.
pom.xml
...
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>
<executions>
<execution>
<id>generate-sources-servicename</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaLanguage>WSDL</schemaLanguage>
<!-- see binding.xjb
<generatePackage>com.company.generated</generatePackage>
-->
<extension>true</extension>
<forceRegenerate>true</forceRegenerate>
<bindingIncludes>
...
binding.xjb
<jaxb:bindings schemaLocation="../../../target/classes/disb.wsdl"
node="/wsdl:definitions/wsdl:types/xs:schema[#targetNamespace='http://com.company/generated/storetax']" >
<jaxb:schemaBindings>
<jaxb:package name="com.company.generated.storetax"></jaxb:package>
</jaxb:schemaBindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="../../../target/classes/disb.wsdl"
node="/wsdl:definitions/wsdl:types/xs:schema[#targetNamespace='http://com.company/storeaccount']" >
<jaxb:schemaBindings>
<jaxb:package name="com.company.generated.storeaccount"></jaxb:package>
</jaxb:schemaBindings>
</jaxb:bindings>
Is there a way to customize the JAXB binding for xs:list? The folowing example:
<simpleType name="doubleList">
<list itemType="double" />
</simpleType>
Will be bound by xjc to: List<Double>. However, I'd like to bind it to: List<BigDecimal>.
My initial setup was to define a binding like this:
<jaxb:bindings multiple="true" node="//xs:simpleType[#name='doubleList']/xs:list/#itemType">
<jaxb:property>
<jaxb:baseType name="java.math.BigDecimal" />
</jaxb:property>
</jaxb:bindings>
However, this gives the following problem:
XPath evaluation of "//xs:simpleType[#name='doubleList']/xs:list/#itemType" needs to result in an element.
Is there a way to do this without resorting to writing your own custom adapters?
The doubleList above was used elsewhere. Applying the binding on those spots, resulting in a correct Javaclass. Supprisingly, only choosing the proper base type was sufficient.
So place where the doubleList was used elsewhere:
<complexType name="DirectPositionType">
<simpleContent>
<extension base="gml:doubleList">
<attributeGroup ref="gml:SRSReferenceGroup" />
</extension>
</simpleContent>
</complexType>
Binding
<jaxb:bindings schemaLocation="http://schemas.opengis.net/gml/3.2.1/geometryBasic0d1d.xsd" node="/xs:schema">
<jaxb:bindings multiple="true" node="//xs:complexType[#name='DirectPositionType']">
<jaxb:property>
<jaxb:baseType name="java.math.BigDecimal" />
</jaxb:property>
</jaxb:bindings>
</jaxb:bindings>
resulting Java class:
public class DirectPositionType
{
#XmlValue
protected List<BigDecimal> value;
#XmlAttribute(name = "srsName")
I'm trying to generate a client with maven and jaxb from a wsdl file with 2 schemas inside and some elements with the same name from different schemas
When I try to execute the compilation I'm getting the next error:
Two declarations cause a collision in the ObjectFactory class.
WSDL schemas:
<wsdl:types>
<schema targetNamespace="http://ws.services" xmlns="http://www.w3.org/2001/XMLSchema">...</schema>
<schema targetNamespace="http://ws.models" xmlns="http://www.w3.org/2001/XMLSchema">...</schema>
</wsdl:types>
I tried renaming the elements that produce the error, but then my spring client receive the correct SOAP message, but it doesn't populate the response object properly (all its attributes are null). I guess the problem might come from renaming the response classes, so that's why I'm trying to generate different packages keeping the original name of all the classes.
In order to do that, I wrote the next bindings file but I don't know what I'm doing wrong that it is not working.
bindings.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.1"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<jaxb:bindings schemaLocation="mywsdl.wsdl#types?schema1"
node="/xs:schema[#targetNamespace='http://ws.services']">
<jaxb:schemaBindings>
<jaxb:package name="package1" />
</jaxb:schemaBindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="mywsdl.wsdl#types?schema2"
node="/xs:schema[#targetNamespace='http://ws.models']">
<jaxb:schemaBindings>
<jaxb:package name="package2" />
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
My configuration part in the maven file is the next, just in case it is useful:
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<wsdlLocation>wsdl/mywsdl.wsdl</wsdlLocation>
<wsdlDirectory>src/main/resources/wsdl</wsdlDirectory>
<wsdlFiles>
<wsdlFile>mywsdl.wsdl</wsdlFile>
</wsdlFiles>
<bindingDirectory>src/main/resources/wsdl</bindingDirectory>
<bindingFiles>
<bindingFile>bindings.xml</bindingFile>
</bindingFiles>
<packageName>original.package</packageName>
<sourceDestDir>${basedir}/src/main/java</sourceDestDir>
</configuration>
When I compile with this bindings files, the same error appears. So I think that maybe it isn't right.
Do you find any mistakes?
Thanks.
From my experience it is best to create 2 binding files (one for each WSDL file).
Update your pom.xml accordingly and make sure that the root element of the binding files is jaxws:bindings (and not jaxb:bindings!)
Some hints:
Make sure to set the "wsdlLocation" attribute correctly! It must point to the WSDL file using a relative path!
The jaxws:package determines the package that will be used for the generated service classes. (the stuff annotated with #WebService)
Enable or disable wrapperStyle and asyncMapping as you wish. ;-)
Example binding file for "package1":
<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
wsdlLocation="mywsdl.wsdl"
version="2.0">
<jaxws:package name="package1"/>
<jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle>
<jaxws:enableAsyncMapping>true</jaxws:enableAsyncMapping>
<jaxws:bindings node="//wsdl:definitions/wsdl:types/xs:schema[#targetNamespace='http://ws.services']">
<jaxb:schemaBindings>
<jaxb:package name="package1"/>
</jaxb:schemaBindings>
</jaxws:bindings>
</jaxws:bindings>
Hi stackoverflow world,
I want to specify in a XSD that a specific element can be used as a XmlRootElement by JAXB.
I know how to add the annotation to the generated class: what I want to do is to specify that a element can be generated as a root element before the code generation.
I use external JAXB customizations (.xjb files).
The purpose is here to not modifying the schemas (as they are defining standards).
Anybody knows how do that?
Thanks!
NJ
Problem solved.
The JAXB plugin Annotate http://confluence.highsource.org/display/J2B/Annotate+Plugin do the job.
Add the following fragment in your jaxb binding file (external binding, i.e. a .xjb file):
<jaxb:bindings schemaLocation="csw/2.0.2/CSW-discovery.xsd" node="/xs:schema">
<jaxb:bindings node="xs:complexType[#name='GetRecordsType']">
<annox:annotate>
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement"
name="GetRecordsType" />
</annox:annotate>
</jaxb:bindings>
</jaxb:bindings>
Do not forget to declare the namespaces:
<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"
xmlns:annox="http://annox.dev.java.net"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
jaxb:extensionBindingPrefixes="xjc annox" version="2.1">
...
</jaxb:bindings>
And use a ANT or MAVEN task http://confluence.highsource.org/display/J2B/User+Guide to proceed the generation of the sources.
I still search how to specify manually (without an xjc task with ant or maven) the JAXB extensions but it works now. (I have my own ANT script what's why I search to manually call XJC).
The JAXB extension mechanism is very convenient, have a look to JAXB2 Basics:
http://confluence.highsource.org/display/J2B/Home
How do I customize the packages of the namespaces when using jax-ws to generate the java artifacts.
I'm running jax-ws iwsmport via maven.
I don't want to change the default package, I want to be able to map from more than one namespace to different packages.
<jaxb:bindings
schemaLocation="../../wscontract/src/main/resources/wsdl/address.xsd"
node="//xsd:schema[#targetNamespace='http://demo.iae.ws/address']">
<jaxb:schemaBindings>
<jaxb:package name="demo.ws.address" />
<jaxb:nameXmlTransform>
<jaxb:typeName prefix="Customer" />
</jaxb:nameXmlTransform>
</jaxb:schemaBindings>
</jaxb:bindings>
Use JAXB bindings with the wsimport -b switch. You can find some sample files here.