Hi On 06/06/13 16:16, tomstark wrote:
Hi,I'm new to the forum and have only been using CXF for a short time but I don't see an answer to the current issue I am having regarding JSON, REST and namespaces. I'm using YANG to define the data model and then taking the resulting XSD and using the maven jaxb2 plugin to generate the POJOs and marshal/unmarshal the objects. Each YANG file specifies it's own namespace (so the user obj has it's own namespace, and contains other objects that have their own namespaces). I'm using CXF 2.5.2 (forced to use this b/c of an external dependency), Jettison and Spring 3.1. The XML for spring looks something like this: (edited to remove specifics) <bean id="users" class="com.company.api.rest.impl.UserImpl"> <property name="userService" ref="userService" /> </bean> <jaxrs:server id="rsServer" address="/"> <jaxrs:serviceBeans> <ref bean="users" /> </jaxrs:serviceBeans> <jaxrs:providers> <ref bean="jaxbProvider" /> </jaxrs:providers> </jaxrs:server> <bean id="jaxbProvider" class="org.apache.cxf.jaxrs.provider.JSONProvider"> <property name="namespaceMap"> <map> <entry key="urn:com.company:api:rest:user" value="user"/> </map> </property> </bean> The java interface looks like this: @POST @Consumes({"application/json"}) public Response create( final User user, final @Context HttpServletRequest servletRequest) throws Exception; My problem is this: As described above, the user object is a composite object. When I use JSON to send a request to create a user, I would expect that I would have to qualify the objects from different namespaces but not the members within the user object that are not part of a different namespace. For example, if user had 2 members, role and username, with role being in a separate namespace and username part of the user namespace, I would like to send JSON like this: {"user.user":{"user-role.user-role":{"name":"ROLE_ADMINISTRATOR"},"username":"admin"}} but when I do that, the username is not assigned and is null instead. To resolve the issue, I need to specify "user.username" and "user-role.name". While this may not seem like a big deal, I have cut out 90% of the user object for brevity so specifying the namespace (even though it has been shortened) is annoying in JSON and makes the packets much larger.
I agree
So my question is how to get around this issue?
I think when we have a complex data model, rather than try to get Jettison map namespaces to prefixes and vice-versa correctly it can be better to get Jettison drop namespaces on the output (ignoreNamespaces property) and then, if reading such a simplified model into namespace qualified beans is needed later on then use transform properties to qualify various elements in the inbound sequence:
http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-CustomizingJAXBXMLandJSONinputandoutput
One other bit of info. I have a GET method for user that returns JSON. The strange thing is that is doesn't have all of the members of user fully-qualified with the namespace in the response. Instead, it is more like what I would like to see my input JSON. In fact, if I take the response user object and then try to feed that back in a POST, it fails. Why can't the object be reflected like that?
I'm not sure; try Jettison 1.3.3 and alternatively, experiment with JacksonJsonProvider
Cheers, Sergey
Thanks in avance!
