Search code examples
javaspringspring-bootrestresttemplate

How can i update the value in the list of ceiling using resttemplate?


1.there is the json list for the controller and the data that what i've been used

[
  {
    "id" : 1,
    "cardname" : "visa gold",
    "cardnumber": "1254***548**54",
    "status" : true,
    "opposed" : false,
    "issueDate" : "21-06-2021",
    "cardbalance" : 200000,
    "validitydate" : "25-06-2021",
    "experationdate" : "30-06-2023",
    "onlinepayment" : true,
    "contactless" : true,
    "withdrawal" : false,
    "ceiling" : [{
      "maxValue" : 10000,
      "minValue" : 500,
      "value" : 100000,
      "currency" : "MAD",
      "type" : "paiment"
    }]

2.and there is my restcontroller code

    @PutMapping("/update/{id}")
    public Ceiling update(@RequestParam Integer id, Card card){
        ParameterizedTypeReference<List<Card>> responseType = new ParameterizedTypeReference<List<Card>>() {};
        ResponseEntity<List<Card>> resp = restTemplate.exchange(GET_CARD, HttpMethod.GET, resp, responseType);
        List<Card> list = resp.getBody();
        return ;
    }

the problem is the put doesn't work the value in the list ceiling , they give me the error : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required Integer parameter 'id' is not present]


Solution

  • The error message,

    Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required Integer parameter 'id' is not present]

    which says the id request parameter is not present in the URL.

    If you are passing the id as an request parameter, then the URL should be like /update?id=12345 and controller method should defined like follows,

    @PutMapping("/update")
    public Card update(@RequestParam(value="id") Integer id, Card card){
    
        RestTemplate restTemplate = new RestTemplate();
        HttpEntity<Card> request = new HttpEntity<>(card);
        ResponseEntity<Card> response = restTemplate.exchange(GET_CARD, HttpMethod.GET, request, Card.class);
        return response.getBody(); 
    }
    

    NOTE : you have not mentioned any information about the relationship between the Card and Ceiling entities. So just assuming that the GET_CARD endpoint is updating the card which we are passing through REST call and returning the updated Card with the response, I have modified the method body as above.

    And if you need to send the id as a path variable, then the URL should be like update/{id} and controller method should be like,

    @PutMapping("/update/{id}")
    public Card update(@PathVariable("id") Integer id, Card card){
        
        RestTemplate restTemplate = new RestTemplate();
        HttpEntity<Card> request = new HttpEntity<>(card);
        ResponseEntity<Card> response = restTemplate.exchange(GET_CARD, HttpMethod.GET, request, Card.class);
        return response.getBody(); 
    }