I generate a WADL with CXF as described in CXF – Missing WADL method parameter element types with JSON JAX-RS services.
I tried to generate the JSON request body in SoapUI ("Recreates a default representation from the schema"), but I get the wrong JSON including namespace.
Code:
CXF configuration:
<bean id="wadlGenerator" class="org.apache.cxf.jaxrs.model.wadl.WadlGenerator">
<property name="linkJsonToXmlSchema" value="true" />
</bean>
<jaxrs:server address="/rest/1" id="test" staticSubresourceResolution="true">
<jaxrs:serviceBeans>
<ref bean="testResource" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider" />
<ref bean="wadlGenerator" />
</jaxrs:providers>
</jaxrs:server>
WADL:
<?xml version="1.0"?>
<application xmlns:prefix1="http://www.test.com/test" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://wadl.dev.java.net/2009/02">
<grammars>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.test.com/test" targetNamespace="http://www.test.com/test" elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xs:import/>
<xs:element name="testModel" type="testModel"/>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.test.com/test" targetNamespace="http://www.test.com/test" elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xs:complexType name="testModel">
<xs:sequence>
<xs:element name="id" type="xs:string"/>
<xs:element name="name" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</grammars>
<resources base="http://localhost:8080/test-app/services/rest/1">
<resource path="/test">
<method name="POST">
<request>
<representation mediaType="application/json" element="prefix1:testModel"/>
</request>
<response status="204"/>
</method>
</resource>
</resources>
</application>
JSON:
{
"#xmlns:test": "http://www.test.com/test",
"id": "?",
"name": "?"
}
Is that a bug/missing feature of SoapUI or is there something wrong with my WADL?
Related
I have defined a xml scehema below
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
<xs:element name="Root">
<xs:complexType>
<xs:all>
<xs:element name="bblist">
<xs:complexType>
<xs:sequence>
<xs:element name="item" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
I want to generate the Json below using a pipeline.
{
"bblist":
[
"13403"
]
}
but the BizTalk pipeline converts it to
{"bblist": "13403"}
Just wondering if my schema is correct. Am I defining the xsd to generate Json arrays correctly?
There are three issues with your XSD schema
You haven't defined a target Namespace. Which means when it goes through the XML Receive it sets the MessageType to a default set of values which doesn't reference the schema. Which means it might not know which schema to use in the JSON Encoder.
MessageType Root Promoted http://schemas.microsoft.com/BizTalk/2003/system-properties
You have used a <xs:all> rather than a <xs:sequence> in your schema definition. That the JSON Encoder doesn't handle.
If you define your schema as this
<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns="http://bblist" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" targetNamespace="http://bblist" vc:minVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" name="bblist">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="unbounded" name="item" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
With a payload of
<ns0:Root xmlns:ns0="http://bblist">
<bblist>
<item>item_0</item>
</bblist>
</ns0:Root>
You get a output of
{
"bblist": {
"item": [
"item_0"
]
}
}
This is closer to your expected JSON, with it making an array of the repeating element.
Your structure is incorrect for the JSON you are expecting as you have the repeat on the item and not on the blist.
If you define your schema as
<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns="http://bblist" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" targetNamespace="http://bblist" vc:minVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="unbounded" name="blist" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML is
<ns0:Root xmlns:ns0="http://bblist">
<blist>blist_0</blist>
</ns0:Root>
JSON is
{
"blist": [
"blist_0"
]
}
I am currently mapping Json and XSD.
In Json there is array contains several items with same elements.
the Json array is like:
"Item":[
{
"ItemSequenceNo":0,
"AQuantity":{
"code":"aaa",
"quantity":1
},
"DQuantity":{
"code":"ddd",
"quantity":4
},
"Amount":{
"currencyID":"USD",
"value":111
},
},
{
"ItemSequenceNo":1,
"AQuantity":{
"code":"aaa",
"quantity":4
},
"DQuantity":{
"code":"ddd",
"quantity":9
},
"Amount":{
"currencyID":"USD",
"value":123
},
}
]
I tried to map this Json array with XSD:
<xs:element name="Item">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="50">
<xs:element ref="p:ItemSequenceNo" minOccurs="1" maxOccurs="1"/>
<xs:element ref="p:AQuantity" minOccurs="1" maxOccurs="1"/>
<xs:element ref="p:DQuantity" minOccurs="1" maxOccurs="1"/>
<xs:element ref="p:Amount" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
where the ref is like:
<xs:element name="ItemSequenceNo">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:totalDigits value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="AQuantity">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="code">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="0"/>
<xs:maxLength value="3"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="DQuantity">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="code">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="0"/>
<xs:maxLength value="3"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="Amount">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="CurrencyID">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="0"/>
<xs:maxLength value="3"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
The thing is when I am mapping Json array with XSD, the output XML ordered by array item(elements) like below:
<item>
<ItemSequenceNo >0</ItemSequenceNo>
<ItemSequenceNo >1</ItemSequenceNo>
<AQuantity code="aaa">1</AQuantity>
<AQuantity code="aaa">4</AQuantity>
<DQuantity code="ddd">4</DQuantity>
<DQuantity code="ddd">9</DQuantity>
<Amount CurrencyID="USD">111</Amount>
<Amount CurrencyID="USD">123</Amount>
</item>
while the result I am expecting is two separate blocks:
<item>
<ItemSequenceNo >0</ItemSequenceNo>
<AQuantity code="aaa">1</AQuantity>
<DQuantity code="ddd">4</DQuantity>
<Amount CurrencyID="USD">111</Amount>
</item>
<item>
<ItemSequenceNo >1</ItemSequenceNo>
<AQuantity code="aaa">4</AQuantity>
<DQuantity code="ddd">9</DQuantity>
<Amount CurrencyID="USD">123</Amount>
</item>
Does anyone have any idea on how can I get this? By modifying Json/XSD, or is there any function in MapForce that could achieve this?
You already solved it, as you said in your comment, but for people who are coming here with the same problem, looking for a solution, it would be nice to provide an answer here.
It may be a bit counterintuitive, that in MapForce you don't draw the line from the Item-Array in the JSON input to the Item node in the XML output. If you do so, you'll get the unified output that BOSubuntu has described. Instead, you draw the line from the objects that the array contains (see picture below, line marked in red). This would give the desired output.
I have a schema A.xsd that imports B.xsd and its one of the complex element <complex-element>. Now I have created the .episode file from compiling the B.xsd and used as input to A.xsd. But except for the <complex-element>, all other child elements classes are regenerated again.
A.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/a" xmlns="http://example.com/a"
xmlns:tns="http://example.com/b" elementFormDefault="qualified">
<xs:import namespace="http://example.com/b" schemaLocation="b.xsd" />
<xs:element name="root">
<xs:complexType>
<xs:all>
<xs:element name="element1" type="xs:string" minOccurs="0" />
<!-- more elements -->
<xs:element name="elementx" type="xs:string" />
<xs:element ref="tns:complex-element" minOccurs="0" />
</xs:all>
<xs:attribute name="version" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:schema>
B.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/b" xmlns="http://example.com/b"
elementFormDefault="qualified">
<xs:element name="complex-element">
<xs:complexType>
<xs:all>
<xs:element name="list" type="list" minOccurs="0"
maxOccurs="1" />
<xs:element name="code" type="code" minOccurs="0"
maxOccurs="1" />
<xs:element name="message" type="xs:string" minOccurs="0"
maxOccurs="1" />
</xs:all>
</xs:complexType>
</xs:element>
<xs:complexType name="list">
<xs:sequence>
<xs:element name="file" type="file" minOccurs="1"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:simpleType name="code">
<xs:restriction base="xs:string">
<xs:enumeration value="S1" />
<xs:enumeration value="S2" />
<xs:enumeration value="S3" />
</xs:restriction>
</xs:simpleType>
<!-- more elements -->
</xs:schema>
pom.xml
<execution>
<id>aschema</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generateDirectory>src/main/java</generateDirectory>
<schemaIncludes>
<include>a.xsd</include>
</schemaIncludes>
<bindingIncludes>
<include>a.xjb</include>
</bindingIncludes>
<cleanPackageDirectories>false</cleanPackageDirectories>
<episode>false</episode>
<args>
<arg>-b</arg>
<arg>${basedir}/src/main/resources/b-episode</arg>
<arg>-Xinheritance</arg>
<arg>-Xxew</arg>
<arg>-Xannotate</arg>
</args>
<verbose>true</verbose>
</configuration>
</execution>
After execution, the Class ComplexElement gets correctly referenced to existing package, but all of the child elements <list> and <code> generates class under the package of org.example.com.a instead to refer existing classes inside org.example.com.b package.
b-episode
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb" if-exists="true"
version="2.1">
<bindings xmlns:tns="http://example.com/b"
if-exists="true" scd="x-schema::tns">
<schemaBindings map="false">
<package name="org.example.com.b" />
</schemaBindings>
<bindings if-exists="true" scd="tns:complex-element">
<class ref="org.example.com.b.ComplexElement" />
</bindings>
<bindings if-exists="true" scd="~tns:list">
<class ref="org.example.com.b.List" />
</bindings>
<bindings if-exists="true" scd="~tns:file">
<class ref="org.example.com.b.File" />
</bindings>
<!-- and so on ... -->
</bindings>
Please follow the documentation on using episodes.
In short, instead of using the .episode file as a file, include the compiled artifact of the schema A.xsd as an episodes/episode in the plugin configuration:
<dependencies>
...
<dependency>
<groupId>com.acme.foo</groupId>
<artifactId>a-schema</artifactId>
<version>1.0</version>
</dependency>
...
</dependencies>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<configuration>
<extension>true</extension>
<episodes>
<episode>
<groupId>com.acme.foo</groupId>
<artifactId>a-schema</artifactId>
<!-- Version is not required if the artifact is
configured as dependency -->
</episode>
</episodes>
</configuration>
</plugin>
</plugins>
</build>
So you shouldn't -b ${basedir}/src/main/resources/b-episode etc.
Disclaimer: I'm the author of maven-jaxb2-plugin.
PayloadFactory mediator I’m using JSON format string. The Problem is, when I enable the following lines in /repository/conf/axis2/axis2.xml, POST request works fine, but GET request produce error message.
<!--messageFormatter contentType="application/json"
class="org.apache.axis2.json.JSONStreamFormatter"/-->
<!--messageBuilder contentType="application/json"
class="org.apache.axis2.json.JSONStreamBuilder"/-->
Here is the ESB template for paypal List all Payment Resources:
<template name="listPaymentResources" xmlns="http://ws.apache.org/ns/synapse">
<sequence class="sequence">
<payloadFactory media-type="json">
<format>
{}
</format>
</payloadFactory>
<property name="messageType" scope="axis2" value="application/json" />
<call>
<endpoint>
<http method="get" uri-template="{uri.var.paypalUrl}/v1/payments/payment"/>
</endpoint>
</call>
</sequence>
</template>
Proxy service:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="listPaymentResources"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<property xmlns:ns="uri.var.payment"
name="ppurl"
expression="//ns:paypalurl/text()"/>
<PayPal.config>
<appUri>{$ctx:ppurl}</appUri>
</PayPal.config>
<PayPal.listPaymentResources/>
<respond/>
</inSequence>
<outSequence>
<log/>
<send/>
</outSequence>
</target>
<description/>
</proxy>
The Error log:
[2013-12-11 17:25:10,971] ERROR - TargetHandler Unexpected error: Cannot get a J
SON writer
java.lang.UnsupportedOperationException: Cannot get a JSON writer
at org.apache.axis2.json.JSONStreamFormatter.getJSONWriter(JSONStreamFor
matter.java:63)
at org.apache.axis2.json.AbstractJSONMessageFormatter.getTargetAddress(A
bstractJSONMessageFormatter.java:228)
at org.apache.synapse.transport.passthru.TargetRequest.start(TargetReque
st.java:152)
at org.apache.synapse.transport.passthru.TargetHandler.requestReady(Targ
etHandler.java:136)
at org.apache.http.impl.nio.DefaultNHttpClientConnection.produceOutput(D
efaultNHttpClientConnection.java:244)
at org.apache.synapse.transport.http.conn.LoggingNHttpClientConnection.p
roduceOutput(LoggingNHttpClientConnection.java:112)
at org.apache.synapse.transport.passthru.ClientIODispatch.onOutputReady(
ClientIODispatch.java:88)
at org.apache.synapse.transport.passthru.ClientIODispatch.onOutputReady(
ClientIODispatch.java:41)
at org.apache.http.impl.nio.reactor.AbstractIODispatch.outputReady(Abstr
actIODispatch.java:148)
I have done some testing on the subject on latest release of the WSO2 ESB and i could not able to reproduce this given issue.
I've got what seems like a very simple example of an xsd and xml file where the xml file does not validate.
Copies of the two files are below.
The first xml element with id = 'fixMe' gets an error that says
Top level is not completed.
Valid xml document must have a root tag
If I remove its id attribute or use "idx" instead of "id", it's fine. But I can't figure out why. 'id' should be a valid attribute.
Any insight appreciated.
XML:
<question id="fixMe" />
<question idx="ok"/>
<question />
XSD:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="perceptive.com/mi/analysis"
xmlns="perceptive.com/mi/analysis"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="test" type="test"/>
<xs:complexType name="test">
<xs:sequence minOccurs="1" maxOccurs="10">
<xs:element name="question" type="question_type"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="question_type">
<xs:attribute name="idx" type="xs:string" use="optional"/>
<xs:attribute name="id" type="xs:string" use="optional"/>
</xs:complexType>
</xs:schema>
An XML document cannot be valid until it is well-formed.
So you should specify a root tag in XML document, e.g.:
<root>
<question id="fixMe" />
<question idx="ok"/>
<question />
</root>