0

With reference to this guide: https://spring.io/guides/gs/consuming-rest/

The guide shows how to consume a RESTful web service.

The response from the REST API query results in the following JSON:

{
   type: "success",
   value: {
      id: 10,
      quote: "Really loving Spring Boot, makes stand alone Spring apps easy."
   }
}

It creates a domain class called Quote.java to contain the data in the response:

package hello;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Quote {

    private String type;
    private Value value;

    public Quote() {
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Value getValue() {
        return value;
    }

    public void setValue(Value value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Quote{" +
                "type='" + type + '\'' +
                ", value=" + value +
                '}';
    }
}

My questions is how do I represent the following json:

{
    "size": 1,
    "limit": 25,
    "isLastPage": true,
    "values": [
        {
            "user": {
                "name": "jcitizen",
                "emailAddress": "[email protected]",
                "id": 101,
                "displayName": "Jane Citizen",
                "active": true,
                "slug": "jcitizen",
                "type": "NORMAL"
            },
            "permission": "ADMIN"
        }
    ],
    "start": 0
}

The outer objects like size and limit are straightforward but I can't figure out how to represent the values object, which looks like an array of json objects.

1 Answer 1

4

This should work.

class Output {
    private String size,
    private int limit;
    private boolean isLastPage,
    private List<Value> values;
    private int start ;
 }


class Value 
 {
     User user,
     private String permission;
 }

class User {
    private String name,
    private String emailAddress,
    private int id,
    private String displayName,
    private boolean active,
    private String slug,
    private String type
}
Sign up to request clarification or add additional context in comments.

1 Comment

How can we insert data(ex: sample json in the question) to this class?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.