【问题标题】:Save a list of object return Cannot deserialize error保存对象列表返回无法反序列化错误
【发布时间】:2018-12-08 21:16:19
【问题描述】:

我使用弹簧靴和百里香叶 我尝试保存对象列表。

我的对象。

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

在我的控制器中,我返回一个对象列表

  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>

我的控制器

  @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"]}

我得到了这个结果

错误:{“时间戳”:“2018-06-29T15:36:58.427+0000”,“状态”: 400,“错误”:“错误请求”,“消息”:“JSON 解析错误:不能 从 START_OBJECT 中反序列化 java.util.ArrayList 的实例 令牌;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:不能 从 START_OBJECT 中反序列化 java.util.ArrayList 的实例 令牌\n 在 [Source: (PushbackInputStream);行:1,列:1]", “路径”:“/rest/spi/1/ge”}

有什么想法吗?

【问题讨论】:

  • 你需要发送数组,而不是对象
  • 好的,但是怎么做

标签: javascript java spring


【解决方案1】:

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

@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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多