处理在 RequestBody 中传递的 SimpleDateFormat
Dealing with SimpleDateFormat passed in RequestBody
我正在开发一个简单的 REST 控制器。我在请求正文中收到一个 SimpleDateFormat 对象。看起来像这样:
2014-04-13T03:42:06-02:00
我现在的方法是:
@PostMapping
public ResponseEntity<Flight> addFlight(@RequestBody JSONObject object) {
Flight newFlight = new Flight(object.get("flightNumber").toString(), new
SimpleDateFormat ( object.get("departureDate").toString()));
repository.save(newFlight);
return ResponseEntity.status(HttpStatus.ACCEPTED).body(newFlight);
}
和class
@Data
@Entity
@DynamicUpdate
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
@RequiredArgsConstructor
@AllArgsConstructor
public class Flight {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private final String flightNumber;
private final SimpleDateFormat date;
}
编译一切正常,但是当我发送 POST 或 GET 时,我收到了我传递的所有数据,但 SimpleDateFormat 为空。我该如何修复它?
我还尝试将 Object 传递给 FlightClass,然后在 class 的构造函数中使用转换器,但我仍然有 null。
SimpleDateFormat is a legacy class and i would recommend OffsetDateTime 因为您的输入表示具有偏移量的 ISO-8601
A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00.
OffsetDateTime dateTime = OffsetDateTime.parse(object.get("departureDate").toString());
我正在开发一个简单的 REST 控制器。我在请求正文中收到一个 SimpleDateFormat 对象。看起来像这样:
2014-04-13T03:42:06-02:00
我现在的方法是:
@PostMapping
public ResponseEntity<Flight> addFlight(@RequestBody JSONObject object) {
Flight newFlight = new Flight(object.get("flightNumber").toString(), new
SimpleDateFormat ( object.get("departureDate").toString()));
repository.save(newFlight);
return ResponseEntity.status(HttpStatus.ACCEPTED).body(newFlight);
}
和class
@Data
@Entity
@DynamicUpdate
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
@RequiredArgsConstructor
@AllArgsConstructor
public class Flight {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private final String flightNumber;
private final SimpleDateFormat date;
}
编译一切正常,但是当我发送 POST 或 GET 时,我收到了我传递的所有数据,但 SimpleDateFormat 为空。我该如何修复它? 我还尝试将 Object 传递给 FlightClass,然后在 class 的构造函数中使用转换器,但我仍然有 null。
SimpleDateFormat is a legacy class and i would recommend OffsetDateTime 因为您的输入表示具有偏移量的 ISO-8601
A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00.
OffsetDateTime dateTime = OffsetDateTime.parse(object.get("departureDate").toString());