我有以下JSON,
{
"numberBlockList":[
"{ 'date' : '2019-07-11T10:28:09.461Z', 'numberCombination' : '10-24-28-36-38-51', 'pickType' : 'RANDOM', 'cost' : '7.00'}",
"{ 'date' : '2019-07-11T10:28:09.471Z', 'numberCombination' : '4-7-15-27-28-40', 'pickType' : 'RANDOM', 'cost' : '7.00'}"
]
}
我的控制器是
@RequestMapping(value = "/saveNumberBlock", method = RequestMethod.POST, consumes = "application/json")
@ResponseBody
public String saveData(Model model, @RequestBody NumberBlockData numberBlockData) {
indexData(model);
log.info("getNumberCombination : " + numberBlockData.getNumberBlockList());
return "index";
}
我的号码块实体
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "number_block")
public class NumberBlock {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "date", nullable = false)
private Date date;
@Column(name = "number_combination", nullable = false)
private String numberCombination;
@Column(name = "pick_type", nullable = false)
private String pickType;
@Column(name = "cost", nullable = false, precision = 12, scale = 2)
private BigDecimal cost;
}
我的号码块POJO
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class NumberBlockData {
List<NumberBlock> numberBlockList;
}
根据互联网上的样本 @RequestBody NumberBlockData numberBlockData ,可以正确获取值,但我将返回以下内容 错误
2019-07-11 16:13:47.648 WARN 8340 --- [nio-9696-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.arbiter.numberblock.modal.NumberBlock` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{ 'date' : '2019-07-11T10:43:47.560Z', 'numberCombination' : '3-14-22-35-45-52', 'pickType' : 'RANDOM', 'cost' : '3.50'}'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.arbiter.numberblock.modal.NumberBlock` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{ 'date' : '2019-07-11T10:43:47.560Z', 'numberCombination' : '3-14-22-35-45-52', 'pickType' : 'RANDOM', 'cost' : '3.50'}')
at [Source: (PushbackInputStream); line: 1, column: 21] (through reference chain: com.arbiter.numberblock.modal.NumberBlockData["numberBlockList"]->java.util.ArrayList[0])]
我需要获取 NumberBlock 数组列表以保存到数据库。有什么建议吗?
答案 0 :(得分:1)
{
"numberBlockList":[
{ "date": "2019-07-11T10:28:09.461Z", "numberCombination": "10-24-28-36-38-51", "pickType": "RANDOM", "cost" : 7.00},
{ "date": "2019-07-11T10:28:09.471Z", "numberCombination" : "4-7-15-27-28-40", "pickType": "RANDOM", "cost": 7.00}
]
}
答案 1 :(得分:0)
您的JSON具有numberBlockList的内容,如:
"{ 'date' : '2019-07-11T10:28:09.461Z', 'numberCombination' : '10-24-28-36-38-51', 'pickType' : 'RANDOM', 'cost' : '7.00'}",
请注意,该行的开头和结尾都用引号引起来-即,它是单个字符串,而不是json值集。这就是为什么会出现异常的原因-Spring尝试使用该字符串构造NumberBlock。 / p>
最简单的解决方案是删除那些引号,而只需添加:
{ 'date' : '2019-07-11T10:28:09.461Z', 'numberCombination' : '10-24-28-36-38-51', 'pickType' : 'RANDOM', 'cost' : '7.00'},