Issue
I am developing an application using Nuxtjs/Vuejs
. Within that, I am using Axios
to send some data to my Java Spring-boot service
.
I would like to send multiple parameters within my Axios Post
request and access them using the Java Spring-boot
. I am able to make it work for a single parameter but when I pass multiple parameters then I get the error Required request body is missing
within the Java service
.
My Axios Post
request:
export const actions = {
testdataGenerator ({ commit, state, dispatch }) {
const headers = { 'Content-Type': 'application/json' }
this.$axios.post('/generateTestData', { params: { inputTemplate: state.testDataInput, identifiersTemplate: state.identifiersData } }, { headers })
.then((response) => {
console.log(response)
})
.catch((error) => {
console.log(error)
})
}
}
Java Spring Boot service
:
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api")
public class TestDataGeneratorController {
@PostMapping(value = "/generateTestData", consumes = "application/json ", produces = "text/plain")
public String generateTestData(@RequestBody String inputTemplate, @RequestBody String identifiersTemplate) throws TestDataGeneratorException {
try {
System.out.println(inputTemplate);
System.out.println(identifiersTemplate);
return null;
} catch (Exception exception) {
throw new TestDataGeneratorException(exception.getMessage());
}
}
}
Can someone please tell me what I need to change within my Axios/Java service
to make the POST request
work?
Solution
Your are sending data with axios to the spring boot controller in the correct way, the problem stands in the multiple RequestBody
annotations in your method :
public String generateTestData(@RequestBody String inputTemplate,
@RequestBody String identifiersTemplate) throws TestDataGeneratorException
In case of multiple params contained in the body of the request one of the ways to solve is the conversion to a Map<String, Object>
and then inside the body extract the parameters :
public String generateTestData(@RequestBody Map<String, Object> map)
throws TestDataGeneratorException {
//your map will be {params={inputTemplate=..., identifiersTemplate=...}}
}
The message Required request body is missing is probably due to the multiple RequestBody
annotations and related to the presence of the second annotation that according to the message receives an empty body, but I am not sure about it.
Answered By - dariosicily
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.