Mapping data from database to JSON in Java Rest Web Service using MyBatis

I have to return a JSON formatted object, after fetching the data from the database. Sample database is as below:

enter image description here

I want the JSON returned value to be as:

{
"Name": "Mark",
"Address": {
            "Adr1": "123 Avenue",
            "Adr2": "Apt-121",
            "City": "Phoenix",
            "State": "AZ"
           } 
}

So far, I have been able to fetch the whole address text as a string, which is not useful to me. It looks like this:

{
"Name": "Mark",
"Address": "{Adr1:123 Avenue, Adr2:Apt-121, City:Phoenix, State:AZ}" 
}

My POJO looks like:

class Profile{
     String name;
     Address addr;
}

class Address{
     String Adr1;
     String Adr2;
     String City;
     String State;
}

I tried creating a separate class for address, but I am unable to map the individual parameters of Address(Adr1, Adr2, City, State), in order to create the JSON object I need.

Can anyone help me with mapping the correct data from database to JSON ?

Answer

you definitely need to deal with parsing the string if you are stuck with that format. I’ve used Jackson in the past with very good experiences. I’m not sure if it would deal with your string any better than another but might be worth looking at. I would try to push to at least get the JSON string in there formatted so that its values are properly quoted. thats a battle in a war you should be able to win!.

To deal with the string in a better formatted string or not I would recommend using a result handler to map to your class. this will help you at least isolate the behavior and better be able to unit test your expectations. inside of it you can handle parsing the address data and map everything to a list of objects that matches the format you want.

an example:

//model for using with mapper
public class DatabaseProfile {
    String name;
    String address;
}

//converts database profile to Profile
public class ProfileResultHandler implements ResultHandler {
  private List<AdStrategyTargetDTO> profiles = new ArrayList<>();

  public List<Profile> getProfiles() { return profiles; }

  @Override public void handleResult(ResultContext context) {
    DatabaseProfile databaseProfile = (DatabaseProfile)context.getResultObject();

    Address address = getAddress(databaseProfile.getAddress());
    Profile profile = new Profile(databaseProfile.getName(),address);

    profiles.add(profile);
  }

  private Address getAddress(String addressString){
      Address address = new Address();
      //do you string parsing here and fill in address object 
      //hopefully just using some deserializer but if all else fails 
      //this is where you would manually parse the string, I'll leave that 
      //detail up to you
      return address;
  }
}

then using this with your mapper might look something like the following

public List<Profile> getProfilesFromDatabase(){
   //the result handle just becomes a parameter of the interfaced call
   ProfileResultHandler resultHandler = new ProfileResultHandler
   profileMapper.getProfiles(resultHandler);

   return resultHandler.getProfiles();
}

Leave a Reply

Your email address will not be published. Required fields are marked *