I am using Apache CXF for Web service development . We have written Java code and the WSDL will be automatically generated by the CXF Engine
I have a String named xmlMessage which must have minoccurs set to 1
For this I used:
private String xmlMessage ;
#XmlElement(name = "xmlMessage", required = true)
public String getXmlMessage()
{
return xmlMessage;
}
But when the WSDL File got generated , it still showed as
<xs:element minOccurs="0" name="xmlMessage" type="xs:string"/>
Please tell me how can i have minOccurs="1" for a Input Message .
This seems to be a bug.
The sun api defines required as:
Customize the element declaration to be required.
If required() is true, then Javabean property is mapped to an XML schema element declaration with minOccurs="1". maxOccurs is "1" for a
single valued property and "unbounded" for a multivalued property.
If required() is false, then the Javabean property is mapped to XML Schema element declaration with minOccurs="0". maxOccurs is "1"
for a single valued property and "unbounded" for a multivalued
property.
Default: false
Might be this bug, don't know if your version is a new one.
Related
I am generating a java class from xsd which is used in to marshal/un-marshal xml.
I have an element currently defined in my xsd as
<xs:element maxOccurs="1" minOccurs="0" name="versionLabel" type="xs:string"/>
which results in a java class containing
String versionLabel
and setters and getters, setVersionLabel()/getVersionLabel().
I want the incoming/outgoing xml element to be <version> and for that to translate to/from the java class property "versionLabel". How do I do define that behavior in the xsd?
add #XmlElement annotation to the attribute and update your xsd if it used for any validation
#XmlElement(name = "version")
String versionLabel;
We use pysimplesoap module for writting of services. Clients were written on Java. There are problems with convertation of xml datatype to java. I suppose, we need to set minOccurs="0" or nillable="true" to wsdl elements. But in pysimplesoap file with name server.py there is such conversion for Arrays only. Is it possible to make it without kludges and how to do it?
Configure the dispatcher as usual:
dispatcher.register_function('test_function', test,
returns={'Success': str},
args={'wanted': str,'optional': str,'another_optional':str })
When defining your method, mark optional fields with defaults:
def test_request(wanted, optional=None, another_optional="Optional"):
return "Success"
I am working with a set of DTDs from a third party system. Our goal is to map the XML request (which conforms to those DTDs into java and then, send an XML response back to the system).
DTDs are written in stone (I don't have any control in changing those).
So, in order to map, I converted DTDs to XML Schemas (xsd) using XMLSpy and then, created Jaxb binding classes using XJC compiler. I am using Java 7.
The problem is, the DTDs don't really have a namespace.. and I have 20 different DTDs.. 10 for request and 10 for response. When I generated the schemas, I had to do one-one mapping.. and created the same 10 request XSDs and 10 response XSDs.
Now, the jaxb xjc compiler generated binding classes.. but they are far from practical use. There is no inheritance 'cus these schemas are not related to each other (although they seem to have similar content - request types and response types).
Can someone please help me if there is a way to customize jaxb bindings to override the default bindings and create more reasonable bindings?
For example consider this simple case:
DTD:
<!ELEMENT FromDate (#PCDATA)>
<!ATTLIST FromDate
year CDATA #REQUIRED
month CDATA #REQUIRED
day CDATA #REQUIRED
>
Schema that I generated using XMLSpy:
<xs:element name="FromDate">
<xs:complexType mixed="true">
<xs:attribute name="year" use="required"/>
<xs:attribute name="month" use="required"/>
<xs:attribute name="day" use="required"/>
</xs:complexType>
</xs:element>
The binding classes that generated out of XJC compiler (java 1.7):
public class FromDate {
#XmlValue
protected String content;
#XmlAttribute(name = "year", required = true)
#XmlSchemaType(name = "anySimpleType")
protected String year;
#XmlAttribute(name = "month", required = true)
#XmlSchemaType(name = "anySimpleType")
protected String month;
#XmlAttribute(name = "day", required = true)
#XmlSchemaType(name = "anySimpleType")
protected String day;
...
...
If you look at how fromDate finally evolved, it doesn't make any sense 'cus just to get the date from this request, I need to do
setMyDate(request.getFromDate().getMonth() + request.getFromDate().getDay() + request.getFromDate().getYear());
which obviously doesn't make sense. Plus, the types are way off.
How can I customize/override jaxb bindings to achieve these two things:
1. inheritance (some kind of abstraction to reduce redundancy)
2. appropriate types
Please help.
OMG someone tries to compile DTDs in 2014. :)
Few links for you:
https://jaxb.java.net/guide/Compiling_DTD.html
http://xml.coverpages.org/jaxb0530spec.pdf - see ยง6 for customizations syntax
https://svn.java.net/svn/ogc~svn/ogc-schemas/trunk/wms/1.1.0/ - Maven project that compiles a DTD
As another approach I'd suggest converting DTDs to schemas an processing schemas. Will be better long-term. DTD support is quite limited
I'm working on a xml schema resolver and I'm using JAXB with XMLSchema.xsd.
I experience problems with JAXB, because I don't get classes for all the top level elements. For example for
<xs:element name="maxLength" id="maxLength" type="xs:numFacet">
I do not get a class MaxLength or anything like that. Only NumFacet exists.
Anyone else experienced that and could please help me?
Cheers,
XLR
As far as I remember jaxb, the schema compiler xjc creates classes for each complex type of the schema given. Thus, if you like to have a class MaxLength you should add a complex type declaration to your schema:
<xs:complexType name="MaxLength">
<xs:attribute name="value" type="xs:int"/>
</xs:complexType>
<xs:element name="MyMaxLength" type="MaxLength"/>
You should now get a class MaxLength with a member variable value of type integer.
JAXB will not generate a class for anything that already has a type, and neither do you need one.
If you unmarshal a global element like your maxLength element, then JAXB will return you a JAXBElement wrapping the NumFacet type. Something like this:
JAXBElement<?> root = unmarshaller.unmarshal(myStream);
NumFacet value = (NumFacet) root.getValue();
There are other methods on JAXBElement to find out what the element name was, etc.
Do people have any recommendations on how i could write my own custom JAXB annotation handling class to support the generation of xs:annotation/xs:documentation elements in the xsd schema?. I'd like to create a new java annotation "#XmlAnnotation" which would include a "documentation" attribute. I'd then make these classes available to the JAXB schema generator via the classpath. The schema generator would then take this sample java code
#XmlRootElement(name="ClientData")
public class ClientData {
/**
* The first address field of the person
*/
#XmlAnnotation(documentation="The first address field of the client")
private String address1 = null;
}
and create this xsd schema
<xs:complexType name="clientData">
<xs:sequence>
<xs:element minOccurs="0" name="address1" type="xs:string">
<xs:annotation>
<xs:documentation>The first address field of the client</xs:documentation>
</xs:annotation>
Would it be easier to extend from the existing #XmlElement annotation class, and just add support of an extra documentation attribute?
This XmlRootElement is almost empty ( http://docjar.org/src/api/javax/xml/bind/annotation/XmlRootElement.java, there is not 'active' code inside
The main modification would be to change the code using this annotation and generating the xsd file.
That would only allow the construct for the root element. It is valid in just about ALL the xs namespace elements.
I don't understand why it wasn't supported as standard.