如何在Spring Boot Post请求中发送对象数组

时间:2019-08-19 13:29:11

标签: java spring spring-boot

我的请求看起来像邮递员

[


    {
            "skill_name":"cms",
            "skill_desc":"php",
            "is_cerificate_completed":true,
            "emp_skill_id":-1,
            "emp_id":3,
            "status":"SMP",
            "reason_for_reject":"",
            "active":true,
            "submitted":true,
            "user_id":3

        },


{
        "skill_name":"wordpress",
        "skill_desc":"php",
        "is_cerificate_completed":true,
        "emp_skill_id":-1,
        "emp_id":3,
        "status":"SMP",
        "reason_for_reject":"",
        "active":true,
        "submitted":true,
        "user_id":3

    }

]

我正尝试将其作为发帖请求发送,我面临的问题是只有第二个对象传递给MySQL中的存储过程,请帮助我使用POST Request将对象数组发送到Spring Boot服务器。

public void saveEmployeeSkillMatrix(List<EmployeeSkillMatrix> emp_skill_matrix) {
        StoredProcedureQuery save = em.createStoredProcedureQuery("sp_iu_skill_matrix");

        save.registerStoredProcedureParameter("p_emp_skill_id", Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_emp_id", Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_skill_name", String.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_skill_desc", String.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_is_certificate_completed", Boolean.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_status", String.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_reason_for_reject", String.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_active", Boolean.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_submitted", Boolean.class, ParameterMode.IN)
        .registerStoredProcedureParameter("p_usr_id", Integer.class, ParameterMode.IN)
        .registerStoredProcedureParameter("error_msg", String.class, ParameterMode.INOUT);

        for(EmployeeSkillMatrix esm : emp_skill_matrix)
        {
            save.setParameter("p_emp_skill_id", esm.getEmp_skill_id());
            save.setParameter("p_emp_id", esm.getEmp_id());
            save.setParameter("p_skill_name", esm.getSkill_name());
            save.setParameter("p_skill_desc", esm.getSkill_desc());
            save.setParameter("p_is_certificate_completed", esm.isCertificate_completed());
            save.setParameter("p_status", esm.getStatus());
            save.setParameter("p_reason_for_reject", esm.getReason_for_reject());
            save.setParameter("p_active", esm.isActive());
            save.setParameter("p_submitted", esm.isSubmitted());
            save.setParameter("p_usr_id", esm.getUser_id());
            save.setParameter("error_msg", new String("error_msg"));
        }

        String errString = (String) save.getOutputParameterValue("error_msg");

    }

1 个答案:

答案 0 :(得分:1)

您需要创建一个封装了JSON输入数组的DTO对象:

public class EmployeeSkillMatrixInDto {
    private List<EmployeeSkillMatrix> employees;
    public List<EmployeeSkillMatrix> getEmployees() { return employees; }
    public void setEmployees(List<EmployeeSkillMatrix> {
        this.employees = employees; 
    }
}

将JSON输入更改为:

 { "employees": [ ... your employees array here ... ] }

最后是控制器,例如:

@RequestMapping(path = "/save", method = RequestMethod.POST)
@ResponseBody
public YourCustomResponse upload(@RequestBody EmployeeSkillMatrixInDto inDto) {
    yourService.saveEmployeeSkillMatrix(inDto.getEmployees());
    //... rest of your response handling ...
}