B_working_K_hard
B_working_K_hard

Reputation: 91

Class not mapping from Get API Request in Spring Boot, how to map to object>

I can't map any values to Profile.class. They are either 0 or null.

When I look for the values in localhost:8080 all the values from Profile object are 0 or null. I am not sure what to do here, please help.

I've tried so many different methods and nothing seems to work. I've tried an exchange, I've tried to map it to the Profile class with getForObject, but nothing has worked.

This is my Rest Controller.

package com.finance.Services;


import com.finance.Model.Profile;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

@RestController
@RequestMapping(value="/profile", headers = "Accept=application/json")
public class ProfileService {



    @RequestMapping(path="/{stockSymbol}", method=RequestMethod.GET)
    public Profile getProfile(@PathVariable("stockSymbol") String stockSymbol) {

        String url = ("https://financialmodelingprep.com/api/v3/company/profile/"+ stockSymbol +"?apikey=bc03aa8fb9a9d1a4944287b3cec3dfe1");


        RestTemplate restTemplate = new RestTemplate();



        HttpHeaders head = new HttpHeaders();
        head.setContentType(MediaType.APPLICATION_JSON);
        head.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        HttpEntity<Profile> request = new HttpEntity(head);

        ResponseEntity<Profile> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                request,
                Profile.class
        );
        if (response.getStatusCode() == HttpStatus.OK) {
            return response.getBody();
        } else {
            return null;
        }


    }

}

This is my Profile Class.

package com.finance.Model;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.Serializable;

public class Profile implements Serializable {
    private double price;
    private double beta;
    private int volAvg;
    private double mktCap;
    private String companyName;
    private String industry;

    public Profile() {
    }

    public Profile(double price, double beta, int volAvg, double mktCap, String companyName, String industry) {
        this.setPrice(price);
        this.setBeta(beta);
        this.setVolAvg(volAvg);
        this.setMktCap(mktCap);
        this.setCompanyName(companyName);
        this.setIndustry(industry);
    }

    public double getPrice() {
        return price;
    }
    @JsonProperty("price")
    public void setPrice(double price) {
        this.price = price;
    }

    public double getBeta() {
        return beta;
    }
    @JsonProperty("beta")
    public void setBeta(double beta) {
        this.beta = beta;
    }

    public int getVolAvg() {
        return volAvg;
    }
    @JsonProperty("volAvg")
    public void setVolAvg(int volAvg) {
        this.volAvg = volAvg;
    }

    public double getMktCap() {
        return mktCap;
    }
    @JsonProperty("mktCap")
    public void setMktCap(double mktCap) {
        this.mktCap = mktCap;
    }

    public String getCompanyName() {
        return companyName;
    }
    @JsonProperty("companyName")
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getIndustry() {
        return industry;
    }
    @JsonProperty("industry")
    public void setIndustry(String industry) {
        this.industry = industry;
    }

    @Override
    public String toString() {
        return "Profile{" +
                "price=" + price +
                ", beta=" + beta +
                ", volAvg=" + volAvg +
                ", mktCap=" + mktCap +
                ", companyName='" + companyName + '\'' +
                ", industry='" + industry + '\'' +
                '}';
    }
}

Upvotes: 0

Views: 1126

Answers (1)

Mukesh Keshu
Mukesh Keshu

Reputation: 467

Here is what happened. When the restTemplate.exchange is returning the response, the information is in this format.

{
  "symbol" : "TT",
  "profile" : {
    "price" : 83.36,
    "beta" : "0",
    "volAvg" : "2185585",
    "mktCap" : "1.99403786E10",
    //more keys below
}

So you cannot directly map the response to Profile class.

Here is what you should do.

  1. Add a new POJO class - APIResponseDTO.
public class APIResponseDTO {
  private String symbol;
  private Profile profile; // Your existing Profile class
  //getter and setters below
}
  1. In ProfileService class , use the new POJO class to get the result from restTemplate.exchange.
 ResponseEntity<APIResponseDTO> response = restTemplate.exchange(
        url,
        HttpMethod.GET,
        request,
        APIResponseDTO.class
        );
  1. Once response is available in APIResponseDTO object, get Profile info as-
 if (response.getStatusCode() == HttpStatus.OK) {
        return response.getBody().getProfile();
    }

Upvotes: 2

Related Questions