Hi
On 31/10/11 06:24, Willard, Jonathan wrote:
Hi all,

Hi, I am providing this xml to a restful servlet whose job it is to convert the 
xml to json.  Here is the servlet code, it is using the JAXB beans created from 
my schema.
It will simple return the JSON version of the xml

     @POST
     @Path("/convert")
     @Produces({"application/json"})
     @Consumes({"application/xml"})
     public JAXBElement<ModelRequest>  _POST_getModelsAsJson(
                                 ModelRequest request,
                                 @Context UriInfo uriInfo,
                                 @Context HttpServletRequest servletReq )
                                      throws WebApplicationException
     {
         try
         {
             JAXBElement<ModelRequest>  modelr = 
requestObjfactory.createModelRequest( request );
             return modelr ;
         }
         catch( Throwable e )
         {
             processException( e );
         }

         return null;
     };


Here is my xml: POST BODY:

<?xml version="1.0" encoding="UTF-8"?>

<rs:model-request throttlesize="10"
   xmlns:rs="http://www.ca.com/spectrum/restful/schema/request";
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
   xsi:schemaLocation="http://www.ca.com/spectrum/restful/schema/request 
../../../xsd/Request.xsd ">


     <rs:target-models>
       <rs:models-search>
         <rs:search-criteria-file>
           topo/config/search-devices-criteria.xml
         </rs:search-criteria-file>
       </rs:models-search>
     </rs:target-models>

     <rs:requested-attribute id="0x1006e" />
     <rs:requested-attribute id="0x10000" />
     <rs:requested-attribute id="0x10032" />
     <rs:requested-attribute id="0x12de2" />

</rs:model-request>


Here is the JSON result:

{
   "ns2.model-request" : {
     "@throttlesize" : "10",
     "ns2.target-models" : {
       "ns2.models-search" : {
         "ns2.search-criteria-file" : "\n          
topo/config/search-devices-criteria.xml\n        "
       }
     },
     "ns2.requested-attribute" : [ {
       "@id" : "0x1006e"
     }, {
       "@id" : "0x10000"
     }, {
       "@id" : "0x10032"
     }, {
       "@id" : "0x12de2"
     } ]
   }
}

Now if I take that JSON and POST it into another servlet (a servlet that 
successfully processes the equivalent xml) I am getting a NPE before it gets 
into my servlet.  I am using the following class to set the schema locations to 
work around a bug in the restful CXF code.  This bug has probably been fixed 
... this work around is now a few years old.  Earlier today it actually worked 
but I think I had the schema locations disabled.

Is NPE thrown when the schema validation is enabled ? Where is it coming from, from Jettison or elsewhere ?


public class CXFNonSpringJaxrsServlet extends
                              
org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet
{
     protected void setSchemasLocations( JAXRSServerFactoryBean bean,
                                         ServletConfig servletConfig )
     {
         List<String>  list = new ArrayList<String>();
         list.add( servletConfig.getServletContext().getRealPath(
                                    "WEB-INF/restful/schemas/Filter.xsd" ) );
         list.add( servletConfig.getServletContext().getRealPath(
                                    "WEB-INF/restful/schemas/Request.xsd" ) );
         list.add( servletConfig.getServletContext().getRealPath(
                                    "WEB-INF/restful/schemas/Response.xsd" ) );

         bean.setSchemaLocations( list );
     }
}


"jaxrs.schemaLocations" can be used to specify a space separated list of locations such as "classpath:WEB-INF/restful/schemas/Filter.xsd", etc (or may be "classpath:/WEB-INF/restful/schemas/Filter.xsd")

Here is my question - I want users to provide a POST body of either XML or 
JSON.  I would like the xml to be validated against the schema.  Will the 
schema also validate the JSON?

If jettison is used then yes, the schema should validate the incoming JSON

What is the right set up for this to work.  I have seen had a few problems 
getting it to work and I am confused.  For obvious reasons I want my clients to 
be able to use either xml or json as both input and output and I want the input 
to be validate as much as possible.


Let me know if you manage to make it work - it really should work...

Cheers, Sergey

Thanks,

Jon



Reply via email to