简体   繁体   中英

Calling json data from external api to be entered into mysql database- Java Spring Boot Web app

I have set up a 'StockPrice' entity, service and repository that allows allows data to be added to a mysql table. The table has the following column format: col1 ... col2

I am trying to get data from an api to be put in this table, the problem is the data I am trying to get is json data (with a few less paramaters than in the mysql table). So I am having trouble manipulating said data.

Within my Service class I have defined the following:

...

@Service
public class StockPriceService {
  ...
  @PostConstruct
  public void populateStockPriceFromAPI() {

    
    ResponseEntity<List<StockPrice>> response = restTemplate.exchange(
      "https://cloud.iexapis.com/stable/stock/AAPL/chart/date/20210224?token=" + apiKey +"&chartIEXOnly=true",
      HttpMethod.GET,
      null,
      new ParameterizedTypeReference<List<StockPrice>>(){});
    List<StockPrice> result = response.getBody();

    
    
              // Save the list into a database
              if(Objects.nonNull(result)) result.stream().filter(Objects::nonNull).forEach(element -> stockPriceRepository.saveAndFlush(element));

      
    }
}

This api request returns the intraday stock prices of apple on 24/02/2021; and is in json form with:

[{'date': '2021-02-24', 'minute': '09:30', 'label': '09:30 AM', 'high': 125.34, 'low': 124.52, 'open': 124.71, 'close': 125.21, 'average': 125.034, 'volume': 22151, 'notional': 2769618.735, 'numberOfTrades': 225}, {'date': '2021-02-24', 'minute': '09:31', 'label': '09:31 AM', 'high': 125.3, 'low': 124.23, 'open': 125.175, 'close': 124.695, 'average': 125.043, 'volume': 20496, 'notional': 2562891.48, 'numberOfTrades': 110},...]

ie it is missing 6 of the fields- 'Ticker', 'relative_strength', 'ma30', 'ma7','ma365', 'id'. The id is auto incremented so this should not be a problem.

When I run I get the following errors:


java.lang.ClassCastException: class java.lang.StackTraceElement cannot be cast to class java.lang.String (java.lang.StackTraceElement and java.lang.String are in module java.base of loader 'bootstrap')
    at java.base/java.io.ObjectInputStream.readTypeString(ObjectInputStream.java:1792) ~[na:na]
    at java.base/java.io.ObjectStreamClass.readNonProxy(ObjectStreamClass.java:835) ~[na:na]
    at java.base/java.io.ObjectInputStream.readClassDescriptor(ObjectInputStream.java:991) ~[na:na]
    at java.base/java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:2017) ~[na:na]
    at java.base/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1895) ~[na:na]
    at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2202) ~[na:na]
    at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1712) ~[na:na]
    at java.base/java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2540) ~[na:na]
    at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2434) ~[na:na]
    at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2235) ~[na:na]
    at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1712) ~[na:na]
    at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:519) ~[na:na]
    at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:477) ~[na:na]
    at org.apache.catalina.session.StandardSession.doReadObject(StandardSession.java:1587) ~[tomcat-embed-core-9.0.37.jar:9.0.37]

Clearly the ObjectInput is failing but I am not sure if it is because the fields in the api data are different from that of my table, or perhaps it is completely misinterpreting the json data, because it is returning [na:na] . If someone could offer me some advice on how to fix this, it'd be much appreciated:)

Generally when you do a request with the Api, you need yo create a Response class and after mapping into the Entity, so:

Create a StockPriceResponse with the variable that response the api, after of this, in the service you can mapping with a method toEntity() and inizialize the variable that the api don't return.

Did I understand your problem?

Example:

public void toEntity(PreRR entity, PreRResponse obj) {
    
    entity.setDescription(obj.getDescription());
    entity.setExecutionDate(obj.getExecutionDate());
    entity.setElement(obj.getElement());
    entity.setCoccole(obj.getCoccole());
    //Variable that is not recived by api
    entity.setMario("PROVA MARIO"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM