0

I have encountered an issue. I need to pass an array of ids that looks like: [1,2,3,4] from my frontend with axios to my spring boot mvc controller.

I want to retrieve the array from @RequestBody in my controller.

How would I do it?

As already mentioned, I will post my code and what I have so far.

In my vue component, my axis call:

axios.put('{{ myUrl }}', {
              genreIds: this.form.selectedGenres
            })

In my mvc controller, my method:

@PutMapping("/url")
    Song myMethod(
            @PathVariable Long songId,
            @RequestBody Long[] genreIds
    ) {
        System.out.println(genreIds);
        //My other code
    }

And all I am getting is a bad request status 400 from my Server

Dusto
  • 45
  • 10
  • What did you already tried? Do you use a common content type like `application/json`? With these information nobody can answer your question... – 0x1C1B Nov 09 '22 at 12:29
  • Hello, I haven't specified any content type, so I am using the default: `application/json`. I will post my code and update my question. – Dusto Nov 09 '22 at 14:28
  • Does this answer your question? [Passing an Array or List to @Pathvariable - Spring/Java](https://stackoverflow.com/questions/9623258/passing-an-array-or-list-to-pathvariable-spring-java) – Jswq Nov 12 '22 at 16:14
  • no it doesn't, I wanted the to get the form data over the @RequestBody, but I already solved it, thanks. – Dusto Nov 16 '22 at 11:05

1 Answers1

0

Like so?

@XXXMapping(value = "/url")
public ResponseEntity controllerMethod(@RequestBody int[] arrayWithIds){
//normally use arrayWithIds in Methodbody
}
Nils
  • 24
  • 3
  • thank you for your response, I tried it out, but I am getting 'bad request status 400'. My axios request is probably wrong. – Dusto Nov 09 '22 at 14:26
  • try to add in the PutMapping Annotation `consumes = MediaType.APPLICATION_JSON_VALUE` and on the body in the axios `JSON.stringify(data_to_send)` and a third parameter in the axios like: `{headers:{'Content-Type':'application/json'}}` – Nils Nov 10 '22 at 09:15
  • It still gave me bad request, but I got it to work. I used `@RequestBody Map genreIds` – Dusto Nov 16 '22 at 11:06