【问题标题】:How can I read data and put into model in java?如何在 java 中读取数据并将其放入模型中?
【发布时间】:2021-08-05 13:27:23
【问题描述】:

我需要从休息中获取一些特定数据到我的模型。

我从休息中得到的数据:

[{"id":595,"name":"TransXChange20210805113332","prosystem_file_id":null,"dataset":16,"creator":113,"editor":113,"created":"2021- 08-05T09:45:21.444949Z","edited":"2021-08-05T09:45:27.542152Z","update_from_url":false,"description":"Rozkłady jazdy komunikacji miejskiej ważne od 08.08.2021","文件":"https://otwartedane.erzeszow.pl/media/resources/transxchange20210805113332.xml","链接":null,"extension":"XML","data_type":"[]","file_name": "transxchange20210805113332.xml","chart_info":null,"map_info":null,"score":4,"public":true,"licence":"other-open","connection_dict":null,"selection_string": null,"db_type":"POSTGRESQL","type":"file","dbview_cache_minutes":1,"preview_base64":null,"gpkg_display_info":null,"archive_to_csv":false},{"id":538, "name":"TransXChange20210513082611","prosystem_file_id":null,"dataset":16,"creator":113,"editor":113,"created":"2021-05-13T06:28:50.233464Z","已编辑":"2021-07-28T08:52:06.695966Z","update_from_url":false,"description":"Rozkłady jazdy komunikacji miejskiej wa日期 15.05.2021","file":"https://otwartedane.erzeszow.pl/media/resources/transxchange20210513082611.xml","link":null,"extension":"XML","data_type":" []","file_name":"transxchange20210513082611.xml","chart_info":null,"map_info":null,"score":4,"public":true,"licence":"other-open","connection_dict ":null,"selection_string":null,"db_type":"POSTGRESQL","type":"file","dbview_cache_minutes":1,"preview_base64":null,"gpkg_display_info":null,"archive_to_csv":false} ,{"id":544,"name":"TransXChange20210526143716","prosystem_file_id":null,"dataset":16,"creator":113,"editor":113,"created":"2021-05-26T12 :40:42.587492Z","edited":"2021-07-28T08:52:04.417450Z","update_from_url":false,"description":"Rozkłady jazdy komunikacji miejskiej ważne od 01.06.2021","file": "https://otwartedane.erzeszow.pl/media/resources/transxchange20210526143716.xml","link":null,"extension":"XML","data_type":"[]","file_name":"transxchange20210526143716。 xml","chart_info":null,"map_info":null,"score":4,"public":true,"licence":"其他r-open","connection_dict":null,"selection_string":null,"db_type":"POSTGRESQL","type":"file","dbview_cache_minutes":1,"preview_base64":null,"gpkg_display_info":null ,"archive_to_csv":false}]

我在一行中得到它

我的java代码:

RestTemplateBuilder builder = new RestTemplateBuilder();
        String soc = builder.build().getForObject("https://otwartedane.erzeszow.pl/v1/datasets/16/resources/", String.class);
        assert soc != null;
        System.out.println(soc);

问题是我需要将它们放入模型中

我的模特:

public class Resource {

private long id;
private String name;
private long prosystem_file_id;
private int dataset;
private int creator;
private int editor;
private LocalDateTime created;
private LocalDateTime edited;
private boolean update_from_url;
private String description;
private String file;
private String link;
private String extension;
private String data_type;
private String file_name;
private String chart_info;
private String map_info;
private int score;
private boolean isPublic;
private String licence;
private String connection_dict;
private String selection_string;
private String db_type;
private String type;
private int dbview_cache_minutes;
private String preview_base64;
private String gpkg_display_info;
private boolean archive_to_csv;

所有的 getter 和 setter 都会生成。 如何将它们放入模型中,例如列表? 问题是我得到像 [{resource},{resource},{resource}] 这样的数据。

【问题讨论】:

    标签: java spring-boot rest


    【解决方案1】:

    正如 RestTemplate documentation 所述

    您可以调用 getForObject 方法,将所需的反序列化类作为参数传递。

    类似:

    RestTemplateBuilder builder = new RestTemplateBuilder();

    Resource soc = builder.build().getForObject("https://otwartedane.erzeszow.pl/v1/datasets/16/resources/", Resource.class);
    assert soc != null;
    System.out.println(soc);
    

    【讨论】:

    • 但问题在于我得到了 [{resource},{resource},{resource}] 的响应。它就像我模型的 3 个元素数组
    • 然后创建一个包含资源列表的包装对象,例如List<Resource> soc,并将其用作构建器的响应对象。
    • List soc = builder.build().. 我应该在 .getForObject 的最后一个参数中添加什么?它需要类,我不能放在那里 List
    • 我现在不在电脑前,但我想 builder.build().getForObject("otwartedane.erzeszow.pl/v1/datasets/16/resources", Resource[].class) 应该可以完成这项工作。
    【解决方案2】:

    我找到了问题的答案。

    我需要将我的调用分配给模型数组。

    Resource[] soc =builder.build().getForObject( URL , Resource[].class );
    

    【讨论】:

      猜你喜欢
      • 2017-06-18
      • 2016-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      • 2021-06-19
      相关资源
      最近更新 更多