Spring unable to map single value from POST

Issue

I am currently using Vue and Spring Boot. Between these two, the communication is done using axios. However, when I try to pass a single value over to Spring Boot from Vue, my Spring keep prompting “Required request parameter “testparam”… is not present”

Here is how I am sending from Vue to Spring:

axios
    .post("url/test", {
        testparam: 1,
        })
        .then((response) > {
            console.log(response.data);
        })
        .catch((error) > {
            console.log(error.message);
        });

And here is how I am receiving it from Spring:

@PostMapping(path  "/test")
@ResponseBody
public Integer test1(@RequestParam(name  "testparam") Integer testparam) {
    System.out.println(testparam);
    return testparam;
}

Solution

You’re expecting a query param, and in your Axios call, you’re sending param in a request body. To fix it, simply send it like this:

axios.post("url/test", {}, { params: { testparam: 1 } })

Answered By – Branislav Lazic

Leave a Comment