My goal is to update a contact in a nationbuilder nation. Nationbuilder is a SAAS for managing campaigns. a "nation" is simply a list of contacts. The endpoint for updating a contact accepts a JSON object in the request body. The object has the form
{ "person": {"first_name": "Bob", "last_name": "Smith", .....many many other fields} }
In my Java code , I have the contact's information available as a Map - a LinkedHashMap type to be exact. I need to convert this to the above JSON object format before I can pass the contact into the Nationbuilder API endpoint. I have tried several approaches and none seem to work. They all result in a 404 Not Found error. Our project uses Jackson - specifically, the javax.ws.rs.client package.
Here is the code that I began with...
LinkedHashMap payload = new LinkedHashMap();
LinkedHashMap person = new LinkedHashMap();
person.put("first_name", "Bob");
person.put("last_name", "SmithFoo");
payload.put("person", person);
LinkedHashMap response = client.post(payload, LinkedHashMap.class);
I also tried converting the payload to a string like this ...
LinkedHashMap payload = new LinkedHashMap();
LinkedHashMap person = new LinkedHashMap();
person.put("first_name", "Bob");
person.put("last_name", "SmithFoo");
payload.put("person", person);
String payloadAsString = Util.getMapper().writeValueAsString(payload);
LinkedHashMap response = client.post(payloadAsString, LinkedHashMap.class);
This also did not work. Interestingly, the value payloadAsString has the exact format I need to pass into the NationBuilder API. However, I think the reason this does not work is because the resulting value is a string rather than a JSON object.
As a third approach, I tried to then convert the string into a json object like this
// Object payloadAsObject = Util.getMapper().convertValue(payload, Object.class);
which just ended up making me realize this was a silly idea because I just ended up with the same linkedhashmap that I began with.
One other approach I tried was creating a payload class. We don't want to create a class for the person object because it would be difficult to maintain an object with 50 or so attributes given the Nationbuilder API will change so the person data needs to be in a map. However, I figured that even though the person attribute needs to be a map, maybe the request will be successful if the payload is an object like this...
public static class Payload{
public Object person;
}
Payload payload = new Payload();
LinkedHashMap person = new LinkedHashMap();
person.put("first_name", "Bob");
person.put("last_name", "SmithFoo");
payload.person = person;
LinkedHashMap response = client.post(payload, LinkedHashMap.class);
This too did not work. (404 not found)