简体   繁体   中英

CXF - Json schema validation

I think the first question I should ask is - Is it possible to validate json payload with XSD? The example given by CXF are all using XSD, anyway.

I follow the instruction from CXF - http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-Schemavalidation

(1) At first it complains

"cvc-elt.1: Cannot find the declaration of element 'OSDCreateAppointmentRequest'"

(2) To fix that, the suggestion is to add a name space to json object, since there is no "namespace" concept for Json but XSD has that

(3) So I add name space to the Json object as follows

        <property name="inTransformElements">
        <map>
            <entry key="OSDCreateAppointmentRequest" value="{http://www.w3.org/2001/XMLSchema}OSDCreateAppointmentRequest"/> 
        </map>
    </property>

Now it complains

cvc-elt.1: Cannot find the declaration of element 'ps1:OSDCreateAppointmentRequest'

Well "ps1" looks like a namespace prefix, no idea where it comes from.

Any suggestions? Your experience on this will be highly appreciated!

W3C XML schemas (XSD) can be used only to validate XML files.

JSON is not XML. So, no XML technologies are applicable to JSON (at least, without any conversions to corresponding XML). That is, XSD cannot be used to validate JSON.

Instead, JSON has its own schemas (written in JSON, of course), see: http://json-schema.org

I know it has been sometime since the question was posted. I got this one to work. The way I got this to work was to use the following property in org.apache.cxf.jaxrs.provider.json.JSONProvider

<property name="namespaceMap" ref="jsonNamespaceMap"/>

and declare name space mapping

 <util:map id="jsonNamespaceMap">
        <entry key="<your-namespace>" value="<prefix>"/>
        </entry>

    </util:map>

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM