Thanks very much for the clear explanation, using the namespaceMap fixed it for us!
Sent from a mobile device > On 21 Jan 2014, at 11:20, Sergey Beryozkin <[email protected]> wrote: > > Hi Mandy > > Please see comments inline >> On 20/01/14 21:49, Mandy Warren wrote: >> >> Hi, >> >> We have a Rest service which returns a JSON or XML response if all goes >> well and a JSON / XML "Errors" object if a problem occurs. The "Errors" >> object has been created using xjc ie. by generating a JAXB class from a >> Schema. >> >> Our JAXB provider looks like this: >> JAXBElementProvider jaxbElementProvider = new JAXBElementProvider >> (); >> >> In our JSON provider we have specified no namespaces should be used in the >> response:- >> JSONProvider jsonProvider = new >> org.apache.cxf.jaxrs.provider.json.JSONProvider(); > it is set to false by default >> jsonProvider.setDropRootElement(false); > the supportUnwrapped is used when reading only >> jsonProvider.setSupportUnwrapped(true); > Ignoring namespaces is only effective on the write side >> jsonProvider.setIgnoreNamespaces(true); >> jsonProvider.setSchemaHandler(schemaHandler); >> jsonProvider.setValidateOutput(true); >> >> >> In our integration tests we have code as follows:- >> >> assertEquals(400, response.getStatus()); >> Errors errors = response.readEntity(Errors.class); >> assertEquals("ABC_001", errors.getError().get(0).getCode()); >> >> ie. we are asserting we get the correct HTTP status code back and that we >> get an Errors object containing an Error with a specific sub error code. >> >> This works fine when using XML as the mime type but when using JSON, our >> code fails at the line: Errors errors = response.readEntity(Errors.class) >> with the following exception: >> >> javax.ws.rs.MessageProcessingException: javax.ws.rs.BadRequestException: >> com.sun.istack.SAXParseException2; columnNumber: 0; unexpected element >> (uri:"", local:"ns2.Errors"). Expected elements are < >> {http://www.xx.com/schema/framework/ErrorV01}Errors> >> at org.apache.cxf.jaxrs.impl.ResponseImpl.doReadEntity >> (ResponseImpl.java:335) >> at org.apache.cxf.jaxrs.impl.ResponseImpl.readEntity >> (ResponseImpl.java:286) >> at org.apache.cxf.jaxrs.impl.ResponseImpl.readEntity >> (ResponseImpl.java:276) >> >> Is there anyway to get the response.readEntity() method to ignore >> namespaces? > It depends on what exactly you'd to do with the JSON payload. > Do you plan the server to return a namespace-qualified JSON ? This is rarely > used in practice, AFAIK, at least when we have say a browser based client, it > can indeed be handy when working with CXF clients as the namespace info can > be used to unmarshall. > > In the trace above, what happens is that the server-based JSONProvider adds a > 'ns2' prefix by default and the client one, finding no namespace map (the one > which would tell it to interpret 'ns2' as a prefix for > http://www.xx.com/schema/framework/ErrorV01) passes it as is to JAXB and we > have a failure. > > The easiest way to fix it is to have a client side JSONProvider set up with a > namespaceMap property, which will have a > ns1:http://www.xx.com/schema/framework/ErrorV01 pair added to it. > > The other option is actually configure the server side to ignore the > namespaces, - this will simplify reading the response for non-CXF clients, > while on the client side you can setup JSONProvider with an > inTransformElements map containing a > '*:{http://www.xx.com/schema/framework/ErrorV01}*' pair, see > > http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-CustomizingJAXBXMLandJSONinputandoutput > > HTH, Sergey > >> Many thanks >> >> Mandy > > > -- > Sergey Beryozkin > > Talend Community Coders > http://coders.talend.com/ > > Blog: http://sberyozkin.blogspot.com
