0

My request looks like in postman

[


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

    }

]

I am trying to send this as my post request, the issue i am facing is that only the second object is passed to the stored procedure in MySQL, Please Help me to send array of objects to spring boot server using POST Request.

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");

    }
10
  • Try to change List<EmployeeSkillMatrix> emp_skill_matrix to EmployeeSkillMatrix[] emp_skill_matrix. I guess spring needs to know what type of Object he needs to map Commented Aug 19, 2019 at 13:32
  • I don't know for sure but I suspect you need to save inside the loop at the end. Commented Aug 19, 2019 at 13:33
  • Indeed. You need to add save.execute(); at end of you for loop Commented Aug 19, 2019 at 13:35
  • 2
    Maybe you should think about reading the StoredProcedureQuery interface. Maybe you should think about using StoredProcedureQuery::executeor StoredProcedureQuery::executeUpdate. Maybe you should use errString. When I read you code, I think the main problem is, it look like you don't understand how stored procedure and Java work together. Commented Aug 19, 2019 at 13:39
  • i got Error calling CallableStatement.getMoreResults @Valijon if i add save.execute() Commented Aug 19, 2019 at 13:43

1 Answer 1

3

You would need to create a DTO object encapsulating the JSON input array:

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

Change the JSON input to:

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

And finally the controller such as:

@RequestMapping(path = "/save", method = RequestMethod.POST)
@ResponseBody
public YourCustomResponse upload(@RequestBody EmployeeSkillMatrixInDto inDto) {
    yourService.saveEmployeeSkillMatrix(inDto.getEmployees());
    //... rest of your response handling ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.