1

I am fairly new to JAX-RS so bear with me on this question. I'm trying to consume a REST API using JAX-RS framework. In particular I am trying to invoke a HTTP GET method on a resource where the response entity will be in JSON format. Up until now I can parse the returned JSON into a customized class with the following code snippet;

 WebTarget target = client.target(url);
 Builder builder = target.request(MediaType.APPLICATION_JSON);
 myClass obj = builder.get(myClass.class);

However, in my latest GET request the JSON return will be best abstracted as a collection of objects. I know in .NET this can be done fairly easily with

JsonConvert.DeserializeObject<List<myClass>> 

but how could I do that in JAX-RS? Thanks in advance.

EDIT:

I model my code after the solution in How to get list<String> as response from jersey2 client

WebTarget target = client.target(url);
Builder builder = target.request(MediaType.APPLICATION_JSON);
builder.header(X_OCTOPUS_APIKEY_NAME, apiKey);
Response serviceResponse = builder.get(Response.class);
List<myType> objects = serviceResponse.readEntity(new GenericType<List<myType>>() {});

However the objects returned is always null. To verify the REST API call actually return a valid JSON value I replace the last line with:

String strDate = serviceResponse.readEntity(String.class);

It is confirmed with the following JSON return:

[
  {
    "Id": "Users-267",
    "Username": "mdamon@mydomain.com",
    "DisplayName": "Damon, Matt",
    "IsActive": true,
    "IsService": false,
    "EmailAddress": "mdamon@mydomain.com",
    "IsRequestor": false,
    "Links": {
      "Self": "/api/users/Users-267",
      "Permissions": "/api/users/Users-267/permissions",
      "ApiKeys": "/api/users/Users-267/apikeys{/id}{?skip}",
      "Avatar":     "https://www.gravatar.com/avatar/94324e7c54a9a5f9d103b2a709863fc3?d=blank"
    }
  },
  {
    "Id": "Users-2101",
    "Username": "baffleck@mydomain.com",
    "DisplayName": "Affleck, Ben",
    "IsActive": true,
    "IsService": false,
    "EmailAddress": "baffleck@mydomain.com",
    "IsRequestor": false,
    "Links": {
      "Self": "/api/users/Users-2101",
      "Permissions": "/api/users/Users-2101/permissions",
      "ApiKeys": "/api/users/Users-2101/apikeys{/id}{?skip}",
      "Avatar":     "https://www.gravatar.com/avatar/11edd32712facde9a7d3dd4445a4abe9?d=blank"
    }
  },
...
]

So for reason the JSON is not being parsed at a collection of my custom type. One extra piece of information is my custom is defined as follows:

@XmlRootElement
public class myType {

    String DisplayName;
    String EmailAddress;

    public myType() {
        super();
    }

    public void setDisplayName(String displayName) {
        DisplayName = displayName;
    }

    public String getDisplayName() {
        return DisplayName;
    }

    public void setEmailAddress(String emailAddress) {
        EmailAddress = emailAddress;
    }

    public String getEmailAddress() {
        return EmailAddress;
    }
}

I only include the DisplayName and EmailAddress field of the JSON in my custom type because I don't need all the other data, in case that matters. Can anyone tell me why it is not being parsed? Thanks

user2963296
  • 71
  • 1
  • 4
  • What do you mean "the objects returned is always null?". You're saying you have a list of two nulls? – Paul Samsotha Sep 21 '18 at 18:04
  • Also, you should use Java naming convention: variable names begin with lower case. Also if you are in control of the server, I suggest using lowercase for the JSON property names also. – Paul Samsotha Sep 21 '18 at 18:07
  • to Paul Samsotha, thanks for looking, by "the objects returned is always null" I mean if I code List objects = serviceResponse.readEntity(new GenericType>() {});, the readEntity() call return a null list. But if I replace it with String strDate = serviceResponse.readEntity(String.class), the readEntity return valid JSON file above – user2963296 Sep 22 '18 at 18:59
  • It should work. But I'll reopen the question. – Paul Samsotha Sep 23 '18 at 00:34
  • 1
    I have figured out what went wrong I need to make the DisplayName and EmailAddress members of myType to be public. – user2963296 Sep 24 '18 at 19:13

0 Answers0