简体   繁体   English

如何通过名单 <CustomObject> 在使用Spring的发布方法中?

[英]How to pass List<CustomObject> in post method using Spring?

I have folowing JSON, 我有以下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'}"
   ]
}

My controller is, 我的控制器是

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

My Number Block Entity 我的号码块实体

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

My Number Block POJO 我的号码块POJO

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class NumberBlockData {
    List<NumberBlock> numberBlockList;
}

as per the samples on the internet @RequestBody NumberBlockData numberBlockData will get the values correctly but mine returning folowing error, 根据互联网上的示例@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])]

I need to get the NumberBlock Arraylist to save to the Database. 我需要获取NumberBlock Arraylist才能保存到数据库。 Any Suggestions? 有什么建议么?

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

Your JSON has the contents of numberBlockList like : 您的JSON具有numberBlockList的内容,例如:

  "{ 'date' : '2019-07-11T10:28:09.461Z', 'numberCombination' : '10-24-28-36-38-51', 'pickType' : 'RANDOM', 'cost' : '7.00'}",

Note that this line starts and ends with " quotes - ie. it's a single string, not a json set of values. That's why you're getting the exception - Spring is trying to use that string to construct a NumberBlock. 请注意,该行的开头和结尾都用引号引起来-即,它是单个字符串,而不是json值集。这就是为什么您遇到异常的原因-Spring尝试使用该字符串构造NumberBlock。

So easiest solution is to remove those quotes, and simply have : 因此,最简单的解决方案是删除这些引号,而只需使用:

  { 'date' : '2019-07-11T10:28:09.461Z', 'numberCombination' : '10-24-28-36-38-51', 'pickType' : 'RANDOM', 'cost' : '7.00'},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM