How to Generate class with JAXB from XSD with imports - java

I have an XSD where use types of another XSDs , exemple:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
targetNamespace="http://www.tce.sp.gov.br/audesp/xml/tagcomum" version="1.0" xml:lang="pt-BR"
xmlns:tag="http://www.tce.sp.gov.br/audesp/xml/tagcomum"
xmlns:gen="http://www.tce.sp.gov.br/audesp/xml/generico"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://www.tce.sp.gov.br/audesp/xml/generico"
schemaLocation="../generico/AUDESP_TIPOSGENERICOS_2016_A.XSD"/>
How to make a JAXB code generator understand that a reference to another ?
my tentatives were frustrated by not filenotfound errors

Related

JAXB maven plugin bindings does not generate classes with interface

I'm trying to generate the java files from XSD with use of jaxb2-maven-plugin. This works without any issues and I can see generated classes in target directory.
Now I decided to let all generated classes implement some interface. So I set up bindings.xjb file where I'm defining the interface. The issue is the plugin can not recognize correct namespace which defines inheritance.
Unsupported binding namespace "http://jaxb2-commons.dev.java.net/basic/inheritance". Perhaps you meant "http://jaxb.dev.java.net/plugin/code-injector"?
I think code-injector is not what I'm looking for, since this allow to define custom pieces of code to add to generated file.
I'm trying to use latest maven plugin:
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>3.1.0</version>
The bindings.xjb file contains following content:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
jaxb:extensionBindingPrefixes="inheritance"
version="3.0">
<jaxb:bindings schemaLocation="application.xsd" node="/xs:schema">
<jaxb:bindings node="//xs:complexType[#name='applicationType']">
<inheritance:implements>com.example.SomeInterface</inheritance:implements>
<jaxb:property name="inheritance"/>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
And the application.xsd file:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="application" type="applicationType"/>
<xs:complexType name="applicationType">
<xs:sequence>
<xs:element type="xs:string" name="language"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
I have found this namespace xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance" on the internet.
Also notice plugin requires version 3.0 of bindings.xjb file.
What is the correct definition of namespace to let plugin generate classes with interface?

How to make java class Serializable which is generated by wsdl

In my project, classes are generated by wsdl. One of these classes is User class. This class must be Serializable. How can I change my pom.xml file for making User Serializable?
I can find example but can't apply it to my project
https://pragmaticintegrator.wordpress.com/2009/03/14/make-serializable-jax-ws-clients-with-maven2/
Finally I could find answer for my question. In our project we use org.apache.cxf plugin to generate classes. I created binding.xml file in resources folder.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 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"
elementFormDefault="qualified" attributeFormDefault="unqualified"
jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.1">
<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings>
<xjc:serializable uid="1337" />
</jaxb:globalBindings>
</xs:appinfo>
</xs:annotation>
</xs:schema>
Then I referenced to this xml file from my pom.xml, for this I added
<bindingFiles>
<bindingFile>${basedir}/src/main/resources/binding.xml</bindingFile>
</bindingFiles>
under wsdlOptions/wsdlOption tag. That's all

Multiple <schemaBindings> for the target namespace in single file

I'm using maven-jaxb2-plugin for generating java files from wsdl one. After running "generate-sources" goal I get the following error
[ERROR] Error while parsing schema(s).Location[
file:/home/*/src/main/resources/soap/binding.xjb{8,30} ].
com.sun.istack.SAXParseException2; systemId: file:/home/*/src/main/resources/soap/binding.xjb; lineNumber: 8; columnNumber: 30;
Multiple <schemaBindings> are defined for the target namespace "http://schemas.***"
There are several wsdl files and for each of them I need different target package, so I tried using binding file, but for only 1 wsdl for now.
Here is my plugin configuraiton
<configuration>
<schemaLanguage>WSDL</schemaLanguage>
<schemaDirectory>
${basedir}/src/main/resources/soap
</schemaDirectory>
<schemaIncludes>
<include>manager/*.wsdl</include>
</schemaIncludes>
<bindingDirectory>
${basedir}/src/main/resources/soap
</bindingDirectory>
</configuration>
Here is binding.xjb file
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
version="2.1"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jaxb:bindings schemaLocation="manager/service.wsdl" multiple="true" node="//xs:schema">
<jaxb:schemaBindings>
<jaxb:package name="com.test.manager"/>
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
Beginning of service.wsdl file
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:sch0="http://schemas.***"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://schemas.***"
targetNamespace="http://schemas.***">
<wsdl:types>
<xs:schema xmlns="http://schemas.***"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="http://schemas.***">
<xs:simpleType name="NumericReference">
***
</xs:simpleType>
<xs:simpleType name="EntityNumber">
***
</xs:simpleType>
</xs:schema>
<xs:schema xmlns="http://schemas.***"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="http://schemas.***">
<xs:complexType name="DisplayGroup">
Looks like the problem is connected with multiple xs:schema elements with same targetNamespace, but I can't find how to fix it without modifying wsdl.
JAXB typically maps one target namespace onto one package, so you can't specify different schemaBindings for the same target namespace.

get <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" > in wsdl

Currently my WSDLhas <xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified"
I want o make it as <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
Can some one tell the way to achieve it.
Thanks.
Example 2 in this can help you. You have to specify the annotation something like:
xmlns = { #javax.xml.bind.annotation.XmlNs(prefix = "po",
namespaceURI="http://www.example.com/myPO1") }
in #XmlSchema or wherever you provide the schema config.
EDIT :
Also, let me show you two examples of XML Schemas:
In the first case, XML namespace is prefixed with wsdl. This is an example of qualified.
<wsdl:definitions xmlns:wsdl='http://www.w3.org/2002/06/wsdl' >
<wsdl:message />
</wsdl:definitions>
However, in this case, XML namespace is not prefixed. This is an example of unqualified.
<definitions xmlns='http://www.w3.org/2002/06/wsdl' >
<message />
</definitions>

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