jeudi 7 septembre 2017

Create map from unmarshaled SOAP response

This java-based product requires data fed to it be in the format of Map<String, Object>

If I make a SOAP call using JAX-WS RI I receive the response and store it in an unmarshaled object like below:

MyObject myObject = port.getMyObject(id);

I need a reliable method to translate myObject into the Map<String, Object> format.

So far I've been able to accomplish this by using reflection and recursively spidering through the object's methods (both declared and super's declared) creating keys that correspond to the chain of method names required to get to the respective values.

Each underscore in the key denotes a method that is called and is necessary in the event there's ever multiple nodes with the same name. Here's an example:

An example SOAP response (simplified) - this is unmarshaled into myObject

<Id>1234</Id>
<Location>
  <Address1></Address1>
  <Address2></Address2>
</Location>
<Login>
  <Production>
    <Username>MyUser</Username>
    <Password>MyPassword</Password>
  </Production>
</Login>

When I pass myObject into the recursive method a Map is returned that looks like this. The 'get' prefix exists on the keys because they're the names of the methods.

getId : 1234
getLocation_getAddress1: 9876 South Street
getLocation_getAddress2: Suite 3
getLogin_getProduction_getUsername: MyUser
getLogin_getProduction_getPassword: MyPassword

My question is is there a better way to do this that does not require me rolling my own solution with reflection?





Aucun commentaire:

Enregistrer un commentaire