I'm trying to make a POST (add) request that returns the ID of the added item. The ID is an 18 character string. Everything works fine as long as the string is 16 characters or less. When it is 18 characters long the last two characters are always corrupted since they default to zero.
I'm using Vuejs as my frontend and a SpringBoot as my backend. I'm aware of the javascript limitation of 53 bits of precision for a Long which is why I'm using String as my Type for this ID. I would expect this to happen if I was trying to return a 18 digit number but not an 18 character string. It appears that although I return a type STRING, Axios is automatically converting it to a LONG which causes it to get corrupted. How do I tell Axios not to do this? ## Heading ##Thanks.
========= Backend restcontroller endpoint =============
@PostMapping("/persons")
public ResponseEntity<String> addPerson(@Valid @RequestBody Person person) {
String newPersonId = personService.addPerson(person);
return new ResponseEntity<>(newPersonId, HttpStatus.OK);
}
==============Frontend Axios POST call ===================
axios
.post("http://localhost:xxxx/persons", newPerson)
.then(response => {
console.log("Response from post is: " + response.data);
console.log(response);
newPerson.id = response.data;
})
.catch(error => console.log(error));
========== Console OUTPUT: =============
The following line is the console.log print statement from my frontend Post call. This shows that I received the ID and last two digits as zero.
Response from post is: 622056030329638900
Notice that response.data (line3 below) ends in 900 and is not surrounded by quotes. If you read further down - response.request.response (line 10 & 11) ends in 912 which is the correct response. I think I know why they dont match (Axios converts it to a Long and it gets corrupted by the 53 bit precision limitation) but I dont understand why Axios is converting the String to a Long in the first place.
- {data: 622056030329638900, status: 200, statusText: "", headers: {…}, config: {…}, …}
- config: {url: "http://localhost:xxxx/persons", method: "post", data: "{"firstName":"daffy","middleName":"woddle","lastName":"duck","homeEmail":"daffy@gmail.com"}", headers: {…}, transformRequest: Array(1), …}
- data: 622056030329638900
- headers: {content-length: "18", content-type: "application/json;charset=UTF-8"}
- request: XMLHttpRequest
- onabort: ƒ handleAbort()
- onerror: ƒ handleError()
- onload: null
- onloadend: null
- onloadstart: null
- onprogress: null
- onreadystatechange: ƒ handleLoad()
- ontimeout: ƒ handleTimeout()
- readyState: 4
- response: "622056030329638912"
- responseText: "622056030329638912"
- responseType: ""
- responseURL: "http://localhost:xxxx/persons"
- responseXML: null
- status: 200
- statusText: ""
- timeout: 0
Expect Result: All 18 characters sent match the ones received. Actual Result: The first 16 characters match, the last two are zero.