Home > Mobile >  Spring unable to map single value from POST
Spring unable to map single value from POST

Time:10-01

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;
}

CodePudding user response:

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 } })

CodePudding user response:

Just make a change in syntax for passing the requestParam by using like params: { testparam: 1 }

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