Tuesday, 27 August 2013

Using namespaces with JAXB and JSON under Jersey 2.2 JAX-RS

Using namespaces with JAXB and JSON under Jersey 2.2 JAX-RS

I am using xjc to compile an XML schema with namespaces and ANY elements.
I will be including elements from external namespaces as needed and need
to handle the situation where a new version of an element definition is
included, or perhaps a element from another namespace with the same name.
Obviously, the XML version of this is working perfectly and I get the
correct QNAME for all elements on the client side, however, for the JSON I
get an unqualified QNAME with only the element name present. This means I
cannot distinguish between and element called "bob" from one namespace,
versus a different element called "bob" from a second namespace.
I have read all the material I can find, and believe this is what I need
to do on the server side, however, i do not know what I need on the client
side:
@Provider
final static class JsonMoxyConfigurationContextResolver implements
ContextResolver<MoxyJsonConfig> {
@Override
public MoxyJsonConfig getContext(Class<?> objectType) {
final MoxyJsonConfig configuration = new MoxyJsonConfig();
Map<String, String> namespacePrefixMapper = new HashMap<String,
String>(2);
namespacePrefixMapper.put("http://schema.jaxbmoxy.examples.jersey.glassfish.org/2013/08/customer",
"c");
namespacePrefixMapper.put("http://schema.jaxbmoxy.examples.jersey.glassfish.org/2013/08/other",
"o");
configuration.setNamespacePrefixMapper(namespacePrefixMapper);
configuration.setNamespaceSeparator('.');
return configuration;
}
}
The "c" schema is the base schema I have defined, and the "o" schema holds
the element definitions I will incorporate through the ANY. For the
example below I am importing o.stats and o.email from the external
namespace. Below is the JSON being generated on the server side:
{"c.customer": [
{
"id": "3",
"personal-info": {
"name": "Bobby Boogie",
"o.stats": [{
"age": "45",
"height": "160 cm",
"weight": "70 kg"
}]
},
"contact-info": {
"address": {
"city": "My Town",
"street": "123 Any Street",
"country": "CA"
},
"phone-number": [
{
"type": "work",
"value": "613-555-1111"
},
{
"type": "cell",
"value": "613-555-2222"
}
]
}
},
{
"id": "2",
"personal-info": {
"name": "Fred Finkleman",
"o.stats": [{
"age": "55",
"height": "182 cm",
"weight": "86 kg"
}]
},
"contact-info": {
"address": {
"city": "Fredsville",
"street": "1 Happy Street",
"country": "US"
},
"phone-number": [
{
"type": "work",
"value": "613-555-1111"
},
{
"type": "cell",
"value": "613-555-2222"
}
],
"o.email": ["fred@email.com"]
}
},
{
"id": "1",
"personal-info": {
"name": "Tom Dooley",
"o.stats": [{
"age": "45",
"height": "160 cm",
"weight": "70 kg"
}]
},
"contact-info": {
"address": {
"city": "My Town",
"street": "123 Any Street",
"country": "CA"
},
"phone-number": [
{
"type": "work",
"value": "613-555-1111"
},
{
"type": "cell",
"value": "613-555-2222"
}
]
}
}
]}
What configuration code do I need on the client side so i can get this to
function correctly? At the moment I have the following:
@Override
protected void configureClient(ClientConfig clientConfig) {
clientConfig.register(new MoxyXmlFeature());
new
ResourceConfig().property(MarshallerProperties.JSON_NAMESPACE_SEPARATOR,
".");
}
Thank you!

No comments:

Post a Comment