I'm trying to POST the following payload to my Jersey-based web service:
{
"firstname":"Jimmy",
"lastname":"Johns",
"addresses":
[
{
"street":"19 Mayberry Drive",
"city":"Mayberry",
"state":"nc",
"postalcode":"27043",
"country":"us",
"addresstype":1
}
],
"data":
{
"eyes":"blue",
"hair":"brown",
"sandwich":"roast beef"
}
}
My Jersey code:
@POST
public Response create( Person person )
{
createBo( person ); <------- stopped here in debugger
...
Stopped just as Jersey calls me, I see addresses in person flushed out with exactly what I'm looking for (what's in the JSON above). However, my data tuples aren't there. I know Jersey is calling my no-arg constructor for Address es and its setters are getting called, but I'm up in the night as far as what Jersey might or might not be trying to do with these random ("data") tuples in my JSON. (I say "random" because in a different invocation, these might be "cave":"deep, dark", "mountain":"high, wide", etc. This is part and parcel of my interface.)
To flesh out what I'm talking about, consider these POJOs as context for the above:
@XmlAccessorType( XmlAccessType.FIELD )
@XmlRootElement
public class Person implements Serializable
{
@XmlElement
private List< Address > addresses = new ArrayList< Address >();
@XmlElement
private Map< String, String > data = new HashMap< String, String >();
...
@XmlRootElement
public class Address implements Serializable
{
private String street;
private String city;
private String state;
private String country;
private String postalcode;
private Integer addresstype;
...
Note: I can't do the random tuples as I've done Address because I don't actually know beforehand what the keys will be (whereas I limit Address to street, city, etc.).
What I need is some kind of magic serializer for HashMaps in Jersey and I cannot seem to interpret the docs well enough to understand how to write one or work around this problem while still maintaining the flexibility of my interface.
I would appreciate any indication as to how to accomplish this.
Russ Bateman
P.S. Note sadly that Java.util.Map to JSON Object with Jersey / JAXB / Jackson was not helpful, though it showed great promise.
Jackson provides the facility for you. You can force it by adding the following to your
Application
class. Note this may disable automatic location of your@Path
annotated classes.Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
The following will work if you are using MOXy, and should work with any other JAXB provider. This approach converts the
java.util.Map
to anorg.w3c.dom.Element
using anXmlAdapter
.MapAdapter
An
XmlAdapter
allows you to marshal an instance of one class as an instance of another class (see: http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html).Person
You specify that a field/property should leverage the
XmlAdapter
via the@XmlJavaTypeAdapter
annotation.Address
jaxb.properties
To specify MOXy as your JAXB provider you need to include a file called
jaxb.properties
in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).Demo
Below is a standalone example you can run to prove everything works.
input.json/Output
MOXy and JAX-RS/Jersey
You can leverage MOXy in a JAX-RS environment by leveraging the
MOXyJsonProvider
class: