尝试反序列化LocalDateTime会引发奇怪的错误

时间:2017-10-04 20:33:08

标签: java angular date deserialization json-deserialization

我从frontend - Angular 4传递了一个对象数组,其中一些是date个对象,backend中的对象被映射为LocalDateTime。不幸的是,当我通过自己的反序列化器尝试deserialize时,我得到了一个错误,这对我来说非常明显,但我无法解决它。所以我的java类看起来像:

Deserializator

public class DateDeserialization extends JsonDeserializer<LocalDateTime> {
    @Override
    public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
      return LocalDateTime.parse(jsonParser.getText());
    }
}

我在model课程中将deserializator添加到字段中:

   @JsonFormat(pattern = "MM-dd-yyyy")
    @JsonDeserialize(using = DateDeserialization.class)
    private LocalDateTime insertDate;

我的java controller

@RequestMapping(value = "", method = RequestMethod.POST)
public List<Guide> save(@RequestBody List<Guide> guides) {
    guides.stream().forEach(x -> {
        guideService.save(x);
    });
    return null;
}

我得到的error404 bad requesterror message是:

JSON parse error: Text '{' could not be parsed at index 0; nested exception is com.fasterxml.jackson.databind.JsonMappingException

当然我知道这是什么意思但是如何解决呢?我还尝试了浏览器调试器在发送请求时传递给后端的内容:

0:{id: 1, description: "test1", insertDate: {…}, insertedBy: {…}, updateDate: {…}, …}
1:{id: 2, description: "test12", insertDate: {…}, insertedBy: {…}, updateDate: {…}, …}
2:{id: 3, description: "test13", insertDate: {…}, insertedBy: {…}, updateDate: {…}, …}
3:{id: 4, description: "test14", insertDate: {…}, insertedBy: {…}, updateDate: {…}, …}
4:{id: 5, description: "test15", insertDate: {…}, insertedBy: {…}, updateDate: {…}, …}

更新:

  ngOnInit() {
    this.guideService.getAll()
      .subscribe(res => {
        this.guides = res.json();
      });
  }

  save(){
    this.guideService.insertOrUpdate(this.guides).subscribe(res => {
      console.log("test went")
    });
  }

0 个答案:

没有答案