skip to Main Content

I am using Spring 5.3.9, jackson-core and jackson-databind- 2.17.1 version. From UI, I am sending json array to the controller. I have tried multiple parameters types in the controller and none of them are working. Getting bad request.

Json Array

@Controller
@RequestMapping("/secure")
 public class PrsPyamentUtilizationSave {

@PostMapping(value = "/prsPyamentUtilSave.html", produces = MediaType.TEXT_HTML_VALUE)
@ResponseBody
public String prsPaymentUtilValidation(@RequestBody Map<String, Object> [] list) {
    
     System.out.println("paymentUtils: " + paymentUtils);
    

     return "OK";
}


}

if I change it to String it is working but output doesn’t seems like a json.

public String prsPaymentUtilValidation(@RequestBody String paymentUtils) 
  {
    
     System.out.println("jsonArray: " + paymentUtils);
return "OK";
 }


jsonArray: list%5B0%5D%5BReportId%5D=1234&list%5B0%5D%5BProviderId%5D=204437&list%5B0%5D%5BProgramTypeId%5D=1&list%5B0%5D%5BFcpUnits%5D=6&list%5B1%5D%5BReportId%5D=1234&list%5B1%5D%5BProviderId%5D=99999&list%5B1%5D%5BProgramTypeId%5D=1&list%5B1%5D%5BFcpUnits%5D=7

Changed to RestController as suggested.

    @RestController
   @RequestMapping("/secure")
public class PrsPyamentUtilizationSave {

    @PostMapping(value = "/prsPyamentUtilSave.html")    
    public String prsPyamentUtilSave(@RequestBody List<PaymentDTO> paymenDTOs) {        
         System.out.println("jsonArray: " + paymenDTOs); 
         return "OK";
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    Updated the Ajax call by removing the key "list" and adding the "JSON.stringify" to the array object.

    $.post(/prsPyamentUtilSave.html', JSON.stringify(list), function (data) { result = data; });
    

    Also used RestController as suggested by Lane. Thanks Lane!!

    @RestController
    
    @RequestMapping("/secure") public class PrsPyamentUtilizationSave {
    @PostMapping(value = "/prsPyamentUtilSave.html")
      public String prsPyamentUtilSave(@RequestBody List<PaymentDTO> paymenDTOs) {               
      System.out.println("jsonArray: " + paymenDTOs);
       return "OK"; 
     } 
    }
    

  2. Yes, a String will always work because it’s just that, a String. Your Map won’t work because your data isn’t structured as a Map. Consider making a DTO for this.

    import com.fasterxml.jackson.annotation.JsonProperty;
    import lombok.Getter;
    import lombok.Setter;
    
    @Getter
    @Setter
    public class PaymentDTO {
    
        @JsonProperty("ReportId")
        private String reportId;
    
        @JsonProperty("ProviderId")
        private long providerId;
    
        @JsonProperty("ProgramTypeId")
        private long programTypeId;
    
        @JsonProperty("FcpUnits")
        private String fcpUnits;
    }
    

    Then you just need to make your controller take a List of these.

    public String prsPaymentUtilValidation(@RequestBody List<PaymentDTO> paymenDTOs) {
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search