0
{
    "response": [
        {
            "productId": "TP_PD_000011",
            "productName": "Toppr Get 10% Off on all Packages",
            "point": 100,
            "imageURL": "https://image.timespoints.iimg.in/product/images/toppr.card.jpg",
            "categories": [
                "Learning###Education"
            ],
            "stock": true,
            "partnerId": 397,
            "category": null,
            "clientId": null,
            "termsConditions": "<ul><li><span style=\"color: rgb(0, 0, 0);\">Get flat 10% off on all Toppr Packages.</span></li><li><span style=\"color: rgb(0, 0, 0);\">To avail the offer : </span><a href=\"https://short.klippd.in/ahik\" rel=\"noopener noreferrer\" target=\"_blank\">Click Here! </a></li><li><span style=\"color: rgb(0, 0, 0);\">Offer valid for all users. </span></li><li><span style=\"color: rgb(0, 0, 0);\">Offer valid on all products. </span></li><li><span style=\"color: rgb(0, 0, 0);\">Offer is valid till </span>31st March'22</li><li><span style=\"color: rgb(0, 0, 0);\">Offer is valid once per user.</span></li><li><span style=\"color: rgb(0, 0, 0);\">Valid on Credit card/ Debit card/ Net banking. </span></li><li>Merchant/TimesPoints reserves the right to withdraw, modify, cancel, change the offer at any point of time without prior notice and at their sole discretion.</li><li>In case of any issues with the usage of code please reach us&nbsp;at&nbsp;feedback at timespoints dot com.</li></ul>",
            "productDescription": "<p><a href=\"https://short.klippd.in/ahik\" rel=\"noopener noreferrer\" target=\"_blank\">Toppr</a> is India's leading after-school learning app with over 12 million students learning on the platform. On a mission to make learning personalised, Toppr caters to the individual learning styles of candidates and provides the widest K12 syllabus.</p><p><br></p><p>Toppr covers 22 boards like CBSE, ICSE, major state boards; 17 Subjects like Maths, Science, Social Science and 60+ competitive exams like JEE, NEET, BITSAT and more.</p>",
            "orderSequence": 59,
            "linkBasedOffer": false,
            "exclusive": false,
            "productApplicability": "ONLINE",
            "partnerName": "Toppr",
            "expiryDate": "31-03-2022",
            "active": false
        }
    ],
    "message": "Success",
    "comments": "",
    "status": "SUCCESS",
    "responseCode": "TP_100"
}

i am triyng to call external api using rest template and above is the api response if i able to get this response as object or string then how can i get any one value like

productId=
productName=

my controller class looks like this

   @GetMapping("/callingAPI")
   public Object getForEntity() {
        String url = "base url";
        RestTemplate restTemplate = new RestTemplate();      

        ResponseEntity<Response> response = restTemplate.getForEntity(url, Response.class);
                    
        
        System.out.println(response);
        
        String.valueOf(ResponseEntity.status(response.getStatusCode()).headers(response.getHeaders()));   
    
        System.out.println(response.getStatusCode());

        Response Json = response.getBody();
        System.out.println(Json);

        System.out.println("ProductId"+ "=" + Json.getProductId());
        System.out.println("point"+ "=" + Json.getPoint());
        System.out.println("ClientId"+ "=" + Json.getClientId());
        System.out.println("ImageURL"+ "=" + Json.getImageURL());
        return  Json;   

   }

i have created response model class with all getter and setters and also table created in my db

@Column(name="productId")
private String productId;

@Column(name="productName")
private String productName;

@Column(name="point")
private Integer point;

@Column(name="imageURL")
private String imageURL;

@ElementCollection
@Column(name="categories")
private List<String> categories = new ArrayList<String>();

@Column(name="stock")
private Boolean stock;

@Column(name="partnerId")
private Integer partnerId;

@Column(name="category")
private String category;

@Column(name="clientId")
private String clientId;

@Column(name="termsConditions")
private String termsConditions;

@Column(name="productDescription")
private String productDescription;

@Column(name="orderSequence")
private Integer orderSequence;

@Column(name="linkBasedOffer")
private Boolean linkBasedOffer;

@Column(name="exclusive")
private Boolean exclusive;

@Column(name="productApplicability")
private String productApplicability;

@Column(name="partnerName")
private String partnerName;

@Column(name="expiryDate")
private String expiryDate;

@Column(name="active")
private Boolean active;

**i am getting result as below and in console also as null for println statement **

{
"productId": null,
"productName": null,
"point": null,
"imageURL": null,
"categories": [],
"stock": null,
"partnerId": null,
"category": null,
"clientId": null,
"termsConditions": null,
"productDescription": null,
"orderSequence": null,
"linkBasedOffer": null,
"exclusive": null,
"productApplicability": null,
"partnerName": null,
"expiryDate": null,
"active": null

}

1 Answers1

0

Response.java

public class Response{
    public String productId;
    public String productName;
    public int point;
    public String imageURL;
    public ArrayList<String> categories;
    public boolean stock;
    public int partnerId;
    public Object category;
    public Object clientId;
    public String termsConditions;
    public String productDescription;
    public int orderSequence;
    public boolean linkBasedOffer;

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public int getPoint() {
        return point;
    }

    public void setPoint(int point) {
        this.point = point;
    }

    public String getImageURL() {
        return imageURL;
    }

    public void setImageURL(String imageURL) {
        this.imageURL = imageURL;
    }

    public ArrayList<String> getCategories() {
        return categories;
    }

    public void setCategories(ArrayList<String> categories) {
        this.categories = categories;
    }

    public boolean isStock() {
        return stock;
    }

    public void setStock(boolean stock) {
        this.stock = stock;
    }

    public int getPartnerId() {
        return partnerId;
    }

    public void setPartnerId(int partnerId) {
        this.partnerId = partnerId;
    }

    public Object getCategory() {
        return category;
    }

    public void setCategory(Object category) {
        this.category = category;
    }

    public Object getClientId() {
        return clientId;
    }

    public void setClientId(Object clientId) {
        this.clientId = clientId;
    }

    public String getTermsConditions() {
        return termsConditions;
    }

    public void setTermsConditions(String termsConditions) {
        this.termsConditions = termsConditions;
    }

    public String getProductDescription() {
        return productDescription;
    }

    public void setProductDescription(String productDescription) {
        this.productDescription = productDescription;
    }

    public int getOrderSequence() {
        return orderSequence;
    }

    public void setOrderSequence(int orderSequence) {
        this.orderSequence = orderSequence;
    }

    public boolean isLinkBasedOffer() {
        return linkBasedOffer;
    }

    public void setLinkBasedOffer(boolean linkBasedOffer) {
        this.linkBasedOffer = linkBasedOffer;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    public void setExclusive(boolean exclusive) {
        this.exclusive = exclusive;
    }

    public String getProductApplicability() {
        return productApplicability;
    }

    public void setProductApplicability(String productApplicability) {
        this.productApplicability = productApplicability;
    }

    public String getPartnerName() {
        return partnerName;
    }

    public void setPartnerName(String partnerName) {
        this.partnerName = partnerName;
    }

    public String getExpiryDate() {
        return expiryDate;
    }

    public void setExpiryDate(String expiryDate) {
        this.expiryDate = expiryDate;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public boolean exclusive;
    public String productApplicability;
    public String partnerName;
    public String expiryDate;
    public boolean active;
}

Root.java

class Root{
    public ArrayList<Response> response;
    public String message;
    public String comments;
    public String status;
    public String responseCode;
  
    public ArrayList<Response> getResponse() {
        return response;
    }

    public void setResponse(ArrayList<Response> response) {
        this.response = response;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getResponseCode() {
        return responseCode;
    }

    public void setResponseCode(String responseCode) {
        this.responseCode = responseCode;
    }



   
}

Controller

@GetMapping("/callingAPI")
public Object getForEntity() {
     String url = "base url";
     RestTemplate restTemplate = new RestTemplate();      
 
     ResponseEntity<Root> response = restTemplate.getForEntity(url, Root.class);
                 
     
     System.out.println(response);
     
     String.valueOf(ResponseEntity.status(response.getStatusCode()).headers(response.getHeaders()));   
 
     System.out.println(response.getStatusCode());
 
     Root Json = response.getBody();
     System.out.println(Json);
 
     System.out.println("ProductId"+ "=" + Json.getResponse().get(0).getProductId());
     System.out.println("point"+ "=" + Json.getResponse().get(0).getPoint());
     System.out.println("ClientId"+ "=" + Json.getResponse().get(0).getClientId());
     System.out.println("ImageURL"+ "=" +Json.getResponse().get(0).getImageURL());
     return  Json;   
 
}

Check Pojo class . The structure of your response class and json response doesn't match.

kelvin
  • 1,480
  • 1
  • 14
  • 28