I hace deployed a java WebService in an application using WebSphere AST. I need to limit the length of some fields (Strings) and I wondered if I can do that at the wsdl instead of coding some validations at java level.
I mean, if rigth now I have the elements defined like this:
<element name="code" nillable="true" type="xsd:string"/>
Can I set some property that restrict the max length of "code"?
Thank you.
<element name="code" nillable="true" type="xsd:string"/>
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:minLength value="2"/>
<xsd:maxLength value="10"/>
</xsd:restriction>
</xsd:simpleType>
Related
I'm getting the following error when generating sources for my project. I have extracted a few common types to a schema called CommonTypes.xsd and I get the following error:
org.xml.sax.SAXParseException: src-resolve.4.1: Error resolving component 'nonEmptyString'. It was detected that 'nonEmptyString' has no namespace, but components with no target namesp
ace are not referenceable from schema document 'file:/C:/Workspace/CommonTypes.xsd'. If 'nonEmptyString' is
intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'nonEmptyString' has no namespace, then an 'import' without a "namespace" attribute should
be added to 'file:/C:/Workspace/lps-performance-calculation-service/pcs-data/src/main/resources/xsd/calc/lps/CommonTypes.xsd'.
The following simple type is defined in my CommonTypes.xsd schema as below:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:unit="http://www.mywebsite.com/unit"
xmlns:types="http://www.mywebsite.com/types"
elementFormDefault="qualified" attributeFormDefault="unqualified"
targetNamespace="http://www.mywebsite.com//types">
<!-- import types -->
<xsd:import namespace="http://www.mywebsite.com/unit"/>
<!-- other common types -->
<xsd:simpleType name="nonEmptyString">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
<xsd:pattern value=".*[^\s].*"/>
</xsd:restriction>
</xsd:simpleType>
And line 241 which causes the error is below:
<xsd:complexType name="Message">
<xsd:simpleContent>
<xsd:extension base="nonEmptyString">
<xsd:attribute type="xsd:string" name="code" use="required"/>
<xsd:attribute name="category" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:token">
<xsd:enumeration value="Error"/>
<xsd:enumeration value="Info"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute type="xsd:string" name="component" use="required"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
Do you have any idea what may cause the error? I've tried searching through StackOverflow and experimenting with the targetNamespace and xmlns, but with no success.
You are trying to refer to a simple type which has
name 'nonEmptyString'
namespace ""
But the simple type 'nonEmptyString' is defined in this XSD, which has targetNamespace="http://www.mywebsite.com//types". So you should be referring to a simple type which has
name 'nonEmptyString'
namespace "http://www.mywebsite.com//types"
You simply need to change this:
<xsd:extension base="nonEmptyString">
to this:
<xsd:extension base="types:nonEmptyString">
You need to import your nonEmptyString in the corresponding namespace and make this namespace referenceable via the prefix.
To do this, add xmlns:types="http://www.mywebsite.com/types" to xsd:schema of the importing schema.
Also provide the namespace in xsd:import of the importing schema. Should be something like:
<xsd:import
namespace="http://www.mywebsite.com/types"
schemaLocation="calc/lps/CommonTypes.xsd"/>
Then you should be able to reference your nonEmptyString type as types:nonEmptyString.
I have the following XML file:
<el id="el1">content</el>
The part of my file which defines the element above is as follows:
<xsd:element name="el" type="myns:el" />
<xsd:simpleType name="state">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="enabled" />
<xsd:enumeration value="disabled" />
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="el">
<xsd:attribute name="id" type="xsd:string" use="required" />
<xsd:attribute name="state" type="myns:state"
default="enabled" />
</xsd:complexType>
My Java code which is supposed to obtain the default value for state looks like the following:
Element e = (Element) ns.getElementsByTagName("el").item(0);
System.out.println(e.getAttribute("state"));
The code above always prints an empty string if I don't set the state attribute in the XML file. Why is that the case? According to the Java documentation, getAttribute returns
The Attr value as a string, or the empty string if that attribute does
not have a specified or default value.
My question is: why doesn't getAttribute return the default value when a value is not specified?
I'm dealing with a problem of connecting to web-service with RPC/encoded WSDL file to my Java/Spring service. I cannot change this WSDL.
I figured out that I have to use Apache Axis 1.4 to create client (according to this problem: https://dzone.com/articles/wsdltojava-error-rpcencoded ).
Then I had problem with login/password/api_key parameters with such message:
<message name="login_message">
<part name="login" type="xsd:string"/>
<part name="password" type="xsd:string"/>
<part name="api_key" type="xsd:int"/>
</message>
Error Element 'api_key': '' is not a valid value of the atomic type 'xs:int'
I solved this problem by adding:
webapi_locator.getEngine().setOption("sendMultiRefs", Boolean.FALSE);
Now I can login and fetch some data from this service but I cannot pushed messages with null arguments like:
<message name="add_offer_input">
<part name="session" type="xsd:string"/>
<part name="category_id" type="xsd:int"/>
<part name="offer" type="tns:offer"/>
</message>
where offer is defined as:
<xsd:complexType name="offer">
<xsd:all>
<xsd:element name="price" type="xsd:float" minOccurs="0" maxOccurs="1"/>
<xsd:element name="price_m2" type="xsd:int" minOccurs="0" maxOccurs="1"/>
[...]
</xsd:all>
</xsd:complexType>
Now I am getting exception like this one:
org.apache.axis.AxisFault: Wrong parameters input xml
Element 'price': '' is not a valid value of the atomic type 'xs:int'. line: 1 column: 0'
at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222) ~[axis-1.4.jar:na]
I have already tried setting
elemField.setNillable(false);
to
elemField.setNillable(true);
in Offer.java.
I am creating Offer message in following way:
Offer offer = new Offer(null,null);
I will be very gratefull for found solution for this error. I don't need to stick with axis 1.4 - any other solution which letting me to connect to this service via SOAP with be usefull for me. Thank you very much for help!
You are probably missing the nillable="true" attribute in your WSDL.
The WSDL should look something similar to below:
..
<xsd:element name="price" type="xsd:float" nillable="true" minOccurs="0" maxOccurs="1"/>
..
nillable="true" allows null arguments to be passed. Java2WSDL using Axis 1.4 doesn't do it, as the axis code method writeWrappedParameter() in org/apache/axis/wsdl/fromJava/Types.java doesn't have it.
More info on bug: https://issues.apache.org/jira/browse/AXIS-243
I have a webservice operation where i'll be getting SAML Assertion as part of the request Body.
I have following XSD:
<xsd:element name="CreateRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="info" type="SomeRequestObj"/>
<xsd:element ref="saml:Assertion" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
The saml:Assertion refers to:
<xsd:import namespace="urn:oasis:names:tc:SAML:2.0:assertion"schemaLocation="../samlv2_0/saml-schema-assertion-2.0.xsd"/>
This saml schema is copied from SAML 2.0.
This generates classes with name *Type.java.
And i am having a hard time creating a unit test for this (which is a separate application with UI).
My Request requires a SAML AssertionType element in the request Body.
So, i cannot use OpneSaml for generating that as it gives me a SAML Assertion object and not AssertionType.
I tried generating the AssertionType object manually but i am having a hard time doing so.
Is there a way to use OpenSaml for generating this?
As i see the xml is going to be the same that i would get in case i just use OpenSaml to generate Assertion object.
Is there a way to simplify this?
EDIT: Added XSD snippet of Assertion
<element name="Assertion" type="saml:AssertionType"/>
<complexType name="AssertionType">
<sequence>
<element ref="saml:Issuer"/>
<element ref="ds:Signature" minOccurs="0"/>
<element ref="saml:Subject" minOccurs="0"/>
<element ref="saml:Conditions" minOccurs="0"/>
<element ref="saml:Advice" minOccurs="0"/>
<choice minOccurs="0" maxOccurs="unbounded">
<element ref="saml:Statement"/>
<element ref="saml:AuthnStatement"/>
<element ref="saml:AuthzDecisionStatement"/>
<element ref="saml:AttributeStatement"/>
</choice>
</sequence>
<attribute name="Version" type="string" use="required"/>
<attribute name="ID" type="ID" use="required"/>
<attribute name="IssueInstant" type="dateTime" use="required"/>
</complexType>
This generates AssertionType Object.
SAML Assertions are of complex type "AssertionType", but the element name is "Assertion". The <Assertion> element generated by OpenSaml should be just fine.
The element is defined in section 2.3.3 in the SAML core spec.
Try to use an external binding file when generating the classes from the XSD with JAXB. See this topic (I guess the second answer of it is what you're looking for): JAXB: How to change XJC-generated classes names when attr type is specified in XSD?
I want to expose a Simple SOAP web service in Mule 3.4 Community.
Some fields of the service need to be mandatory/required. How can that be done?
Here is the web service method:
public String methodname(String field1, String field2, String field3);
Here is the resulting wsdl:
<xsd:element minOccurs="0" name="field1" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="0" name="field2" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="0" name="field3" nillable="true" type="xsd:string"/>
How can I make these fields minOccurs="1" and nillable="false"
Please note #XmlElement(required=true) doesn't work with my Java version 1.6
minOccurs="1" makes the element mandatory and minOccurs="0" makes it optional
So make the following change to make it mandatory:-
<xsd:element minOccurs="1" name="field1" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="1" name="field2" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="1" name="field3" nillable="true" type="xsd:string"/>
For your reference :- https://social.msdn.microsoft.com/Forums/en-US/f59a3ee2-5997-4ee7-8c09-8d371c923267/creating-required-elements-in-xsd?forum=xmlandnetfx
and
How to tell if XML element is marked as required in the XSD file
and nillable is specifies whether an explicit null value can be assigned to the element
reference:-http://www.w3schools.com/schema/el_element.asp
So you need to make change in your WSDL so that the Java class generated from to implements those properties of attributes
I'm afraid you can't do this with a simple service (as it's simple by definition, it would probably work latter versions of java as you point).
You should switch to a fullfeatured jaxws service without autogenerated wdsl.