简体   繁体   English

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

[英]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.我已经设置了一个“StockPrice”实体、服务和存储库,允许将数据添加到 mysql 表中。 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).我正在尝试从 api 获取要放入此表的数据,问题是我要获取的数据是 json 数据(比 Z81C3B080DAD537DE7EZE10E0987A4BF25 表中的参数少一些)。 So I am having trouble manipulating said data.所以我在处理上述数据时遇到了麻烦。

Within my Service class I have defined the following:在我的服务 class 中,我定义了以下内容:

...

@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;此 api 请求返回苹果在 2021 年 2 月 24 日的盘中股票价格; and is in json form with:并采用 json 形式:

[{'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'.即它缺少 6 个字段 - 'Ticker'、'relative_strength'、'ma30'、'ma7'、'ma365'、'id'。 The id is auto incremented so this should not be a problem. id 是自动递增的,所以这应该不是问题。

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] .显然 ObjectInput 失败了,但我不确定是否是因为 api 数据中的字段与我的表中的字段不同,或者它可能完全误解了 json 数据,因为它正在返回[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:通常,当您使用 Api 发出请求时,您需要创建一个响应 class 并在映射到实体后,因此:

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.使用响应 api 的变量创建 StockPriceResponse,之后,在服务中,您可以使用 toEntity() 方法映射并初始化 api 不返回的变量。

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"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Spring Boot Web UI 调用外部 API - Calling External API from Spring Boot Web UI 从Spring Boot使用multipart / form-data调用外部api - Calling external api from spring boot with multipart/form-data 使用MySql数据库的Spring Boot / Web - Spring Boot/Web with MySql database 从其他外部 spring 启动应用程序调用 api - Call an api from other external spring boot app jQuery和JSON以从Java Web应用程序中的数据库检索数据? - jQuery and JSON to retrieve data from database in Java web app? 前端(HTML)无法从Microsoft Azure中的mysql数据库中获取Spring Boot应用程序中的数据 - Front-end (HTML) is not fetching data in spring boot app from mysql database in Microsoft Azure 如何通过 Id 从数据库中获取数据 Spring 引导 Java REST ZDB97423871483AC14 - How to get Data By Id From Database Spring Boot Java REST API 从Java Spring Boot映射mysql数据库中的tinyint(3) - Mapping tinyint(3) in a mysql database from Java Spring Boot Spring Boot无法从MySQL数据库获取和显示数据 - Spring Boot can't fetch and display Data from MySQL Database 调用外部 api 并在 spring 引导中的数据库中捕获相应的 json 数据的良好做法是什么 - What is the good practice to call external api and capture respective json data in data base in spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM