2

I have a RESTful service that has a single path parameter and a parameter that is unmarshalled from the request body. The request body parameter is XML for which I have an XSD. I've been trying to validate the XML payload against the XSD but to no avail. I've tried the following, as described here:

<jaxrs:server address="/"
    serviceClass="my.endpoint.class">
    <jaxrs:schemaLocations>
        <jaxrs:schemaLocation>classpath:schema/myschema.xsd</jaxrs:schemaLocation>
    </jaxrs:schemaLocations>
</jaxrs:server>

The schemas are being found (there are no errors at least) but what I expect to be an invalid payload is not throwing an exception. Parameters that don't match the XSD contents are coming through as null. It may not be relevant but my auto-generated payload class has three attributes, some of which are required.

I've had a brief go at creating a MessageBodyReader, as described here but I think I'm having scope issues and my schema object is not available when readFrom is called.

Any help or suggestions would be gratefully appreciated!

Community
  • 1
  • 1
David
  • 497
  • 4
  • 10

1 Answers1

2

Turns out the servlet I was using wasn't accepting the jaxrs configuration shown above. I changed from using this:

<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

to this:

<servlet-class>
    org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<init-param>
    <param-name>config-location</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>

Snippet of my applicationContext.xml

<jaxrs:server address="/">
    <jaxrs:schemaLocations>
        <jaxrs:schemaLocation>classpath:schema/myschema1.xsd</jaxrs:schemaLocation>
        <jaxrs:schemaLocation>schema/myschema2.xsd</jaxrs:schemaLocation>
        <jaxrs:schemaLocation>schema/myschema3.xsd</jaxrs:schemaLocation>
    </jaxrs:schemaLocations>

    <jaxrs:serviceBeans>
        <bean class="my.package.endPoint1" />
        <bean class="my.package.endPoint2" /> 
    </jaxrs:serviceBeans>

    <jaxrs:features>
        <cxf:logging />
    </jaxrs:features>
</jaxrs:server>

Schema references are from the resources directory, adjacent to WEB-INF.

David
  • 497
  • 4
  • 10
  • the schemas that you have mentioned in the schema locations tag, are they interrelated i mean are you using some sort of import or include or they are totally different and are validating different requests ? – Sikorski Jun 01 '12 at 06:08
  • Hello David, I am trying to do the same thing.However I am not using servlets. How should I proceed. – Abhishek Ranjan Oct 15 '15 at 10:05