Unfortunately, the solution I've come up with does not solve exactly the given problem, but I've found a great workaround. If you are struggling to map input XML into some sort of class instance, you can try working it around by mapping the response into JsonNode, which gives you a Json-like tree, which is in turn is not an exact representation of source xml, because it doesn't contain attributes of tags, but it does contain values stored within the tags!
What you can do is:
- Accept response as String. So in my code:
...
.bodyToMono(OperationStateResponseBody.class)
...
becomes:
...
.bodyToMono(String.class)
...
- Then, you input the body received as
Stringinto XmlMapper instance like this:
String rawResponse = ...; // your response body as String with XML content
XmlMapper xmlMapper = new XmlMapper(); // you can also use any sort of bean injection if you want
JsonNode jsonNode = xmlMapper.readTree(rawResponse);
- Access element values for source XML by calling
jsonNode.get("elementName"):
private boolean isSuccessful(JsonNode operationStateResponseBody) {
return operationStateResponseBody.get("Result") != null
&& operationStateResponseBody.get("Result").get("Code").asInt() == 0
&& operationStateResponseBody.get("State").get("Code").asInt() == 100;
}
Obviously, there is a lot of drawbacks to that solution, the obvious one is that all the values are of Object type, and you would have to cast them manually or by calling .asXxx() sort of methods, whereas by creating a mapped class you will have all of those features out of the box, providing that you matched the input XML format to the class correctly.