我使用 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; 
} 


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!