Creating or replacing an Middleware object in Kubernetes using the Kubernetes Java Client Library doesn't work (400/404)

With a lot of trial and error I finally found the cause of the issue. For the Traefik CRD the object that is being send to the Kubernetes API server needs to be a Gson object. A String based Json doesn't seem to be accepted. For those looking to do the same thing I did, here is a mockup solution:

//CreateMiddleware.java
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(jsonTraefikMiddleware, JsonObject.class);
customObjectsApi.createNamespacedCustomObject(kubeConfGroup, kubeConfVersion, namespace, kubeConfPlural, jsonObject, null, null, null);

When updating and existing object, you will need to add the current resource version to the json. Below example:

//ExistingMiddleware.java
Object obj = customObjectsApi.getNamespacedCustomObjectWithHttpInfo(kubeConfGroup, kubeConfVersion, namespace, kubeConfPlural, kubeMiddlewareName).getData();
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(obj);
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
String resourceVersion = jsonObject.getAsJsonObject("metadata").get("resourceVersion").toString();
String jsonTraefikMiddleware = String.format(jsonTraefikMiddlewareStringMultiline, kubeMiddlewareName, namespace, jsonIpsStringMultiline, resourceVersion);
customObjectsApi.replaceNamespacedCustomObject(kubeConfGroup, kubeConfVersion, namespace, kubeConfPlural, kubeMiddlewareName, gson.fromJson(jsonTraefikMiddleware, obj.getClass()), null, null);

Don't forget that the examples are of "works on my machine" and "from the top of my head".