I have the following class:
@CrossOrigin()
@PostMapping(path = "/xcpd", produces = "application/json; charset=utf-8")
protected String xcpd(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
JSONObject jsonData = null;
JSONObject jObj = new JSONObject();
JSONObject responseXCPD = null;
int user_id = -1;
int pid = -1;
try {
if ((jsonData = new JSONObject(getJSONString(request))) != null) {
if (jsonData.has("country_code") && jsonData.has("identifier")) {
// User data
user_id = Integer.valueOf(request.getParameter("user_id"));
String rolename = String.valueOf(request.getParameter("rolename"));
....
}
}
}
}
I'm using the following curl call to call this endpoint:
curl -X POST -H "Content-Type: application/json" http://localhost:8083/xcpd -d '{"user_id":"1",
"country_code":"IE",
"rolename": "doctor",
"identifier": "1.1.1.1"
}'
When I call the curl example I get the error java.lang.NumberFormatException: null
for this line user_id = Integer.valueOf(request.getParameter("user_id"));
. I suppose that means that string user_id
is empty and can't be converted in integer.
Any help please?
You are trying to parse a null value. If you want to parse the body of the request there is a simple way to do that. In you xcpd method ad a parameter annotated with @RequestBody and it will be automatically converted to a java object. getParameter is not the right way to get a field from a request body. See: https://www.baeldung.com/spring-request-response-body