【问题标题】:Handle 2 conversions with one DTO object使用一个 DTO 对象处理 2 次转换
【发布时间】:2020-01-30 07:46:34
【问题描述】:

这是我的第一个春季项目。我正在尝试从 API (openweathermap) 获取天气,将其映射到 dto,并且仅当 dto 是唯一插入数据库时​​。

最初的想法是使用两个 dto 类。一个用于 API 响应,另一个用于数据库实体(字段较少)。它工作得很好,但我听说过像 @JsonIgnoreProperties(ignoreUnknown = true) 和 @JsonProperty("name") 这样的 JSON 注释。

在对weatherSampleDto 进行一些修改后,它适用于 API 请求,但不适用于实体 ↔ dto 不再映射。

天气 API 响应:

{
  "coord": {
    "lon": 25.61,
    "lat": 49.56
  },
  "weather": [
    {
      "id": 804,
      "main": "Clouds",
      "description": "хмарно",
      "icon": "04n"
      …
    }
  ],
  "base": "model",
  "main": {
    "temp": 1.06,                   <-------------- temperature
    "feels_like": -2.27,            <-------------- feelsLike
    "temp_min": 1.06,
    "temp_max": 1.06,
    "pressure": 1016,               <-------------- pressure
    "humidity": 84,                 <-------------- humidity
    "sea_level": 1016,
    "grnd_level": 974
  },
  "wind": {
    "speed": 1.65,
    "deg": 281
  },
  "clouds": {
    "all": 100                      <-------------- clouds
  },
  "dt": 1580144397,                 <-------------- time
  "sys": {
    "country": "UA",
    "sunrise": 1580104637,
    "sunset": 1580137367
  },
  "timezone": 7200,
  "id": 691650,                     <-------------- cityId
  "name": "Ternopil",               <-------------- cityName
  "cod": 200
}

这里是修改后的WeatherSampleDto:


@JsonIgnoreProperties(ignoreUnknown = true)
public class WeatherSampleDto implements Serializable {
    @JsonIgnore
    private Long id;
    @JsonProperty("name")
    private String cityName;
    private float temperature;
    private float feelsLike;
    private int pressure;
    private int humidity;
    private int clouds;
    private int cityId;
    @JsonProperty("dt")
    private int time;

    @JsonProperty("main")
    private void unpackMain(Map<String, String> main) {
        temperature = Float.parseFloat(main.get("temp"));
        feelsLike = Float.parseFloat(main.get("feels_like"));
        pressure = Integer.parseInt(main.get("pressure"));
        humidity = Integer.parseInt(main.get("humidity"));

    }
    @JsonProperty("clouds")
    private void unpackClouds(Map<String, Integer> cloudsObj) {
        clouds = cloudsObj.get("all");
    }
    @JsonProperty("id")
    private void unpackId(Integer idObj){
        cityId = idObj;
    }
...

还有WeatherSample实体:

@Entity
public class WeatherSample {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String cityName;
    private float temperature;
    private float feelsLike;
    private int pressure;
    private int humidity;
    private int clouds;
    private int cityId;
    private int time;
...

它在每个带注释的字段上都失败了。 如com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.LinkedHashMap<java.lang.Object,java.lang.Object>`out of VALUE_NUMBER_INT token at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: weather.dto.WeatherSampleDto["clouds"])

可以用一个 dto 类解决吗?如果有怎么办?

【问题讨论】:

    标签: java spring hibernate jackson dto


    【解决方案1】:

    常见的实现是创建从 API 获取天气数据的 @Service 类,响应模型将是它自己的单独类,然后您获取所需的任何数据并填写 @Entity用它类然后存储它。

    【讨论】:

    • 是的,这正是我的做法。想也许有更好的解决方案。
    猜你喜欢
    • 2016-12-04
    • 2013-08-17
    • 2023-01-16
    • 2021-06-30
    • 2018-05-29
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 2012-04-17
    相关资源
    最近更新 更多