处理请求body中传递的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);
}
班级
@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;
}
所有内容都很好,但是当我发送帖子或获取时,我会收到我传递的所有数据,但是SimpleDateFormat是无效的。我该如何维修? 我还尝试将对象传递给FlightClass,然后在班级的构造函数中使用转换器,但我仍然有空。
I'm developing a simple REST Controller. I'm receiving a SimpleDateFormat object in Request body. Looks like that:
2014-04-13T03:42:06-02:00
My current method now is:
@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);
}
And 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;
}
Everything is compiling fine, but when I'm sending POST or GET I receive all of the data that I've passed, but SimpleDateFormat is null. How could I repair it?
I've also tried to pass Object to FlightClass and then use a converter in the class's constructor, but I've still had null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
simpledateateFormat 推荐 offsetDateTime 以来您的输入代表ISO-- 8601带偏移
SimpleDateFormat is a legacy class and i would recommend OffsetDateTime since your input represents ISO-8601 with offset