我想发布ArrayList,但返回空值

时间:2019-03-26 04:57:11

标签: spring-boot

邮递员:

{
    "childDTO":[{
            "age":"80",
        "gender":"kavi",
        "occupation":"main",
        "type":"mainlife"
    },  
    {       "age":"80",
        "gender":"kavi",
        "occupation":"main",
        "type":"mainlife"
    }
    ]
}

控制器.....

@PostMapping("/child")
public List<CustomerDTO> childDTO(@RequestBody CustomerDTO cus){

return calculationService.childDTO(cus.getAge(),cus.getGender(),cus.getOccupation(),cus.getType());
}

服务...

public List<CustomerDTO> childDTO(String age, String gender, String occupation, String type);

服务说明...

@Override

public List<CustomerDTO> childDTO(String age, String gender, String occupation, String type) {

    List<CustomerDTO> typeChild = new ArrayList<>();
    if (type==children) {
        for (CustomerDTO customer1 : typeChild) {
            customer1.setAge(age);
            customer1.setGender(gender);
            customer1.setOccupation(occupation);
            customer1.setType(type);
            customer1.setBenifits(benifitDTO(beni.getRiders(), beni.getSumAssuarance()));
            System.out.println("list:-"+customer1);
            typeChild.add(customer1);

        }
    }
    System.out.println("list:-"+typeChild);
    return typeChild;
}

2 个答案:

答案 0 :(得分:1)

您可能没有发布变量children,可能是某个类字段,但是您无法将Java字符串与==进行比较。 Java字符串必须与.equals进行比较:

...
if (type.equals(children)) {
...

下次使用调试器,查看代码中发生了什么。

答案 1 :(得分:0)

您的请求和控制器的RequestBody结构不匹配。 您需要使用列表。

@PostMapping("/child")
public List<CustomerDTO> childDTO(@RequestBody List<CustomerDTO> cusList){ // here list needs to be used
    if(cusList != null && !cusList.isEmpty()) {
        CustomerDTO cus = cusList.get(0); // it will get first element from the list, if you want to process all the elements then you need to iterate the list
        return calculationService.childDTO(cus.getAge(),cus.getGender(),cus.getOccupation(),cus.getType());
    }
}

现在将采用其他逻辑,或者您可以根据需要进行更改。