Home > Back-end >  Why do I get a 400 error when sending multiple parameters in post? Angular Spring Boot
Why do I get a 400 error when sending multiple parameters in post? Angular Spring Boot

Time:03-15

I'm sending a localdatetime, and a number to my restcontroller from angular. This is the only one I get errors with, no matter what I do. I logged the variable values, in case they're in a wrong format. The localdatetime was in yyyy-MM-ddThh:mm format. My angular method call:

updateDogFeeding(dogid,data)
    {
      return this.http.post(environment.rooturl   "/api/profile/animals/feeddog",{dogid,data},{observe:'response', withCredentials:true,
      headers: new HttpHeaders({'Content-Type':'application/json'})})
    }

I tried sending the parameters in an array, or an array of the elements using JSON.stringify(), in HttpParams, nothing worked. I checked te error message from spring, and got this:

Could not resolve parameter [0] in public com.example.vizsga_kedvenc_felugyelo.model.Dog com.example.vizsga_kedvenc_felugyelo.controll.DogController.feedDog(java.lang.Integer,java.time.LocalDateTime): JSON parse error: Cannot deserialize value of type java.lang.Integer from Object value (token JsonToken.START_OBJECT); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type java.lang.Integer from Object value (token JsonToken.START_OBJECT) at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 1]

what could be the possible cause?

My restcontroller:

@PostMapping(value = "api/profile/animals/feeddog")
    public Dog feedDog(@RequestBody Integer dogID, @RequestBody LocalDateTime timestamp)
    {
        System.out.println("Got called"); //Isn't printed on the console
        return dogService.feedDog(dogID,timestamp);
    }

CodePudding user response:

You are using two response bodys. Should be one.

When you send the request from Angular, the request is creating a body with a JSON like this:

{
  "dogid": X,
  "data": Y
}

This is an object, but only one. So your controller method should have an object as parameter for the request body, with the same structure.

Edit: You can also use an object of type Map<> to hold the values.

  • Related