Hi
On 26/08/16 09:32, J. Fiala wrote:
Hi,

I'd like to add schema validation for Json requests (as described in
https://cwiki.apache.org/confluence/display/CXF20DOC/JAX-RS+Data+Bindings#JAX-RSDataBindings-Schemavalidation).


1.) Schemavalidation using Json JacksonProvider / JAXBElementProvider:
Configuration used:

<bean id="jaxbProvider"
class="org.apache.cxf.jaxrs.provider.JAXBElementProvider">
       <property name="schemaHandler" ref="schemaHolder"/>
    </bean>

    <bean id="schemaHolder"
class="org.apache.cxf.jaxrs.utils.schemas.SchemaHandler">
       <property name="schemas" ref="theSchemas"/>
    </bean>

    <util:list id="theSchemas">
      <value>my_complex_types_and_elements.xsd</value>
    </util:list>

I'm sending an invalid JSON which does not match the defined pattern,
but the validation doesn't strike.

The only message I see in the log is:
o.apache.cxf.jaxrs.utils.ResourceUtils   : No resource
classpath:META-INF/jax-rs-catalog.xml is available

Is there anything else I have to set to activate schema validation?

Or is the JAXBElementProvider only active for XML request processing and
schema validation is not possible using JacksonProvider ?

Yes, JAXBElementProvider is for XML only.
JacksonProvider is a 3rd party provider, I think latest ones can validate with JSON Schema

2.) jettison/org.apache.cxf.jaxrs.provider.json.JSONProvider

When I try to use
jettison/org.apache.cxf.jaxrs.provider.json.JSONProvider and add the
schemaHandler:

<bean id="jsonProvider"
class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
       <property name="schemaHandler" ref="schemaHolder"/>
    </bean>

Then I get with the first attempt (model generated using wadl2java):
Caused by: com.sun.istack.internal.SAXException2: unable to marshal type
"..." as an element because it is missing an @XmlRootElement annotation

After adding
@XmlRootElement(namespace="|http://www.example.org/books|"), the
exception changes:
Payload: JAXBException occurred : cvc-elt.1: Cannot find the declaration
of element "..."

The Json request sent using the CXF client includes a namespace prefix:
{"ns2:myRequestType":{...}}

If I remove the namespace prefix, the error stays the same:
{"myRequestType":{...}}

Next I thought to add a jsonNamespaceMap (as described here
http://cxf.apache.org/docs/jax-rs-data-bindings.html):

<bean id="jsonProvider"
class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
       <property name="schemaHandler" ref="schemaHolder"/>
<property name="namespaceMap" ref="jsonNamespaceMap"/>
    </bean>

|<||util:map| |id||=||"jsonNamespaceMap"|
|map-class||=||"java.util.Hashtable"||>|
|<||entry| |key||=||"http://www.example.org/books";| |value||=||"ns2"||/>|
|</||util:map||>
But still no luck, the error stays the same, regardless if the Json
includes the prefix or not.
|

Any ideas on this?


Does the schema has an *element* named myRequestType ?

Sergey

best regards,

Johannes




--
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Reply via email to