1

So through research on Stackoverflow, especially this topic, I've determined that I have a JSON object dictionary embedded in an otherwise working getter/setter setup. When I make a call to the API, the traditional getter/setters for the properties work well, but that's because I know what the names of the properties are. How can I get this working for unknown properties of an object called errors, and what format is this object in (Map<String,List>)?

API Response:

{
    "success":false,
    "response_code":1,
    "status_message":"One or more errors has occurred.",
    "errors":{
        "171":["Some error message."],
        "555":["Some other error message."]
    }
}

My POJO:

public class APIResponse {
    private boolean success;
    private int response_code;
    private String status_message;
    private Map<String, List> errors = new HashMap<>();
    ...
}

How can I set the getter/setters and the correct dictionary format (Looks like maybe it's Map<String,List>) so that I can start receiving this dynamic errors object?

I'm using an Invocation.Builder to parse out the API response into an object.

Response response= invocationBuilder.post(Entity.entity(APIRequest, MediaType.APPLICATION_JSON));
APIResponse formattedResponse = response.readEntity(APIResponse.class);
Shawn
  • 513
  • 9
  • 18

1 Answers1

0

My solution was a tiered one.

First, the discovery that Moxy just doesn't do maps was frightening, but rather reliving once I found that the be the issue. So I had to change my default serializer from Moxy to Jackson. This was done easily by changing the pom.xml:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jersey.version}</version>
</dependency>

Second, my attempt had it a bit wrong, instead of:

private Map<String, List> errors = new HashMap<>();

I really needed:

private HashMap<String, ArrayList<String>>;

Throwing the constructor right in line with it would always wipe out the data. Instead I just initialize the map and then created the normal getter/setters on the POJO.

Shawn
  • 513
  • 9
  • 18