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

4
  • What did you already tried? Do you use a common content type like application/json? With these information nobody can answer your question... Commented Nov 9, 2022 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. Commented Nov 9, 2022 at 14:28
  • Does this answer your question? Passing an Array or List to @Pathvariable - Spring/Java Commented Nov 12, 2022 at 16:14
  • no it doesn't, I wanted the to get the form data over the @RequestBody, but I already solved it, thanks. Commented Nov 16, 2022 at 11:05

1 Answer 1

0

Like so?

@XXXMapping(value = "/url")
public ResponseEntity controllerMethod(@RequestBody int[] arrayWithIds){
//normally use arrayWithIds in Methodbody
}
Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your response, I tried it out, but I am getting 'bad request status 400'. My axios request is probably wrong.
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'}}
It still gave me bad request, but I got it to work. I used @RequestBody Map<String, Long[]> genreIds

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.