TLDR; HttpURLConnection
does not support PATCH, but we can sometimes use X-HTTP-Method-Override
instead (this works with Spark Framework). I can Patch POJO using reflection, or deserialize to simple POJO and compare with null
values instead
I had not planned to implement PATCH in my testing web service - at all, ever…
But Mark Turner’s comment on my previous blog post made me reconsider and now I’ll try and aim for as many HTTP verbs as I can.
Mark asked:
we did a similar thing with the jersey client, but found one flaw: it couldn’t handle PATCH requests which are useful when testing. Does gson handle this?
The short answer is, Gson can be used to handle this, and I present two ways of handling it below.
When implementing Patch, because of the architecture I’m using I have to solve 4 problems:
- how to patch a POJO (Domain)
- how to parse a JSON or XML request in a Patch request suitable to allow me to patch a POJO (API)
- how to route a Patch request (HTTP REST)
- how to send a Patch request (Test)
Generic Solution
I thought if I could find a generic solution then I should try that first as I can make fast progress that way.
How to patch a POJO (Domain Object)
For a generic solution the first thing that popped in to my mind was to use reflection to do this, and since my objects are currently pretty simple that seems reasonable. Given that my application is pretty low risk since it is to use for training or practice in how to test a REST API.
I thought I’d create a generic patcher
that given a hashmap will use reflection to iterate over fields until it finds the field and then sets the value.
At the moment all my fields are String so that’s pretty simple to do.
@Test
public void canPatchAListicator(){
ListicatorList list = new ListicatorList("first title", "first desc");
Map<String,String> patches = new HashMap<String, String>();
patches.put("title", "this is the new title");
patches.put("createdDate", "1996-04-01-14-54-23");
patches.put("description", "new description");
ReflectionPatcher patcher = new ReflectionPatcher(list, ListicatorList.class);
patcher.patch(patches);
Assert.assertEquals("this is the new title", list.getTitle());
Assert.assertEquals("1996-04-01-14-54-23", list.getCreatedDate());
Assert.assertEquals("new description", list.getDescription());
Assert.assertEquals(0, patcher.getFieldsInError().size());
Assert.assertEquals(3, patcher.getFieldsPatched().size());
}
And the ReflectionPatcher
is also pretty simple:
public class ReflectionPatcher {
private final Object thing;
private final Class theClass;
private List<String> fieldsInError = new ArrayList<>();
private List<String> fieldsPatched = new ArrayList<>();
public ReflectionPatcher(Object thing, Class theClass) {
this.thing = thing;
this.theClass = theClass;
}
public void patch(Map<String, String> patches) {
for(String fieldName : patches.keySet()){
boolean hadToSetAccessible = false;
Field declaration=null;
Field field=null;
try {
declaration = theClass.getDeclaredField(fieldName);
if(!declaration.isAccessible()){
hadToSetAccessible = true;
declaration.setAccessible(true);
}
declaration.set(thing, patches.get(fieldName));
fieldsPatched.add(fieldName);
} catch (NoSuchFieldException e) {
e.printStackTrace();
fieldsInError.add(fieldName + " - did not exist" );
} catch (IllegalAccessException e) {
e.printStackTrace();
fieldsInError.add(fieldName + " - could not access");
}finally {
if(hadToSetAccessible=true && declaration!=null){
declaration.setAccessible(false);
}
}
}
}
public List<String> getFieldsInError() {
return fieldsInError;
}
public List<String> getFieldsPatched() {
return fieldsPatched;
}
}
Not the prettiest, not the most robust, but for my current needs this would work and if I stick to simple objects with String
fields no nested elements I’ve pretty much got patching of POJOs sorted.
How to parse a JSON or XML request in a Patch request
JSON
My patch requests would look something like this in JSON:
PATCH /lists/sdfjwer-siwejr-2342sn
{"title":"title4","author":"author2"}
- The GUID in the URI path
- the part JSON in the body
And converting that to a hash with Gson is simple. And is often how I use Gson for tactical work when parsing.
return gson.fromJson(body, Map.class);
- turn the
String``body
into a Map.
XML
My XML looks a little different since outer tags have to be named, not just ‘an object’
I could make it a <patch/>
element which would keep it consistent, but since this is REST, the verb PATCH
pretty much tells the server what it needs so I should be able to patch a list
with:
PATCH /lists/sdfjwer-siwejr-2342sn
<list><title>title4</title><author>author2</author></list>
Jaxb is good for serializing to an object, but doesn’t want to work with a Map so I turned to JSON-java from Sean Leary for help. Again another package I’ve used for tactical automating in the past.
This allowed me to…
Map withHead = gson.fromJson(
XML.toJSONObject(body).toString(),
Map.class);
Create some JSON from the XML and then use Gson to convert it to a Map.
Only one issue is that because of the extra ‘head’ <list>
I have a nested map and all I want are the fields and values so I quickly thought:
ArrayList outer = new ArrayList();
outer.addAll(withHead.keySet());
return (Map<String, String>) withHead.get(outer.get(0));
- Get the key of the parent and then return the submap with all its children.
A more elegant solution for this will occur to me as soon as I hit publish on this post, or I’ll learn it from the helpful comments.
I will investigate a more robust solution to this, but the reflection approach to the POJO amendment just needs a HashMap, so I’m done.
How to route a Patch request
This simply required me wiring up the Spark routing to the api method I created which used the ReflectionPatcher and the generic payload to map convertor.
patch("/lists/*", (request, response) -> {
return api.patchList(new SparkApiRequest(request),
new SparkApiResponse(response)).getBody();
});
patch("/lists", (request, response) -> {
response.status(405);
return "";
});
How to send a Patch request
I thought this was going to be easy:
con.setRequestMethod("PATCH");
Set the request method on my HttpURLConnection
but NO.
HttpURLConnection
does not support PATCH.
Fortunately, Spark supports “X-HTTP-Method-Override”
And therefore if I send a POST, with a header of:
X-HTTP-Method-Override: PATCH
Spark will treat the request as a PATCH and route it accordingly.
A ‘Better’ non-generic way
For my purposes I can speed ahead with a non-generic way, but it would probably be better for me to have a more object based approach.
So I tried an experiment…
In my current code I have:
- Domain Objects, these have methods and logic and cool stuff but they are POJO with no annotations etc.
- Payload Objects, these are purely for serializing and deserializing (or marshalling and unmarshalling)
Here is an example:
@XmlRootElement(name = "list")
public class ListicatorListPayload {
public String guid;
public String title;
public String description;
public String createdDate;
public String amendedDate;
}
So what would happen if I deserialized a partial JSON into that?
return gson.fromJson(body, ListicatorListPayload.class);
Well, because all the fields are set to null at the start, if there is nothing in the JSON string, then they stay null, so I effectively have a ‘PATCH’ object where the patches are the non-null values.
What happens with XML?
JAXBContext context = JAXBContext.newInstance(ListicatorListPayload.class);
Unmarshaller m = context.createUnmarshaller();
return (ListicatorListPayload)m.unmarshal(new StringReader(body));
Same result, a ListicatorListPayload
where only the patched fields are non-null.
I could now create a method on my ListicatorList
which takes a ListicatorListPayload
as parameter and sets the fields which are non-null, a bit like a clone operation.
Or, and I suspect I would do this, create a ListicatorListPatcher
which knows how to patch a domain object from a payload - I don’t really like the idea of my Domain Objects knowing anything about the API, but I’m happy for my API to know about the domain objects.
This seems like a more robust approach going forward, and if I introduce more complexity into my code base for object handling then I’ll probably use that approach.
I learned a surprising amount:
- X-HTTP-Method-Override: PATCH as a header can allow some web servers to treat a POST method as a PATCH
- HttpURLConnection does not like PATCH methods (hence the above)
- Reflection is still useful for quick hacks
- Gson deserializes to Map, Jaxb does not
- Gson and Jaxb will both deserialize a PATCH message to null when the value isn’t present - which is handy