javascript - 保存对象列表返回 Cannot deserialize 错误

标签 javascript java spring

我使用 Spring Boot 和 thymeleaf 我尝试保存对象列表。

我的对象。

public class GECPD {
    public Integer id;
    public String name;
    public List<GEPD> geL;
    public Integer giId;     
    public Integer sGEId;
}

在我的 Controller 中,我返回一个对象列表

  Set<GECPD> gECL = new HashSet<>();
  ...
  model.addAttribute("gECL", new ArrayList<>(gECL));

数据显示正确

<input name="id[0]" type="hidden" value="136">
<select name="sGEId[0]" class="form-control">
 <option value="246">01</option>
    <option value="391">00</option>
</select>

我的 Controller

  @PutMapping(value = "/{id}/ge")
  public ResponseEntity updateGE(@PathVariable("id") Integer id, @RequestBody List<GECPD> dto) {
        ....
        return new ResponseEntity(dto,HttpStatus.OK);
  }

函数 saveGroundElement(){

var form = transForm.serialize('#gEForm');
var url = "/rest/spi/"  + $("#spi").val() + "/ge";
form = JSON.stringify(form);
jQuery.ajax({
    type: "put",
    url: url,
    contentType: "application/json",
    data: form,
    success: function (data, status, jqXHR) {
    },
    error: function (jqXHR, status) {

        checkError(jqXHR);
    }
});

}

数据发送

{"id":["136"],"sGEId":["246"]}

我得到这个结果

Erreur: { "timestamp" : "2018-06-29T15:36:58.427+0000", "status" : 400, "error" : "Bad Request", "message" : "JSON parse error: Cannot deserialize instance of java.util.ArrayList out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.ArrayList out of START_OBJECT token\n at [Source: (PushbackInputStream); line: 1, column: 1]", "path" : "/rest/spi/1/ge" }

有什么想法吗?

最佳答案

你的前端没问题...你需要修改你的服务器端

@PutMapping(value = "/{id}/ge")
  public ResponseEntity updateGE(@PathVariable("id") Integer id, @RequestBody GECPDList dto) {
        ....
        return new ResponseEntity(dto,HttpStatus.OK);
  }

在 GECPDS 中

public GECPDList class{
    private List<GECPD> gecpds;
}

关于javascript - 保存对象列表返回 Cannot deserialize 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51105759/

相关文章:

java - 包 org.apache.axis.client 不存在 Play Framework

java - Spring:重写/模拟依赖的依赖

javascript - 如何在地形底部裁剪实体或几何体

javascript - Kibana卡在加载屏幕上[更新-Nginx仅提供部分JS文件]

java - 解析xml kotlin android

java - 即使已填充, fragment 中的 RecyclerView 仍保持空白

java - 使用 Spring 和 JsonTypeInfo 注释将 JSON 反序列化为多态对象模型

java - Spring mvc 4中如何防止CSRF攻击

javascript - javascript 中的 "?"(问号)是什么意思?

javascript - 将 2D Canvas (图像 + 文本)应用到 3D 表面并进行 360 度旋转/动画 - 可以使用 HTML5/CSS3/Javascript 或 Flash 来实现吗?