Ineed to get the id to delete on the database but I cant get the id parameter this way
@RequestMapping(value = {"/delete/search/","/delete/search"}, method = RequestMethod.DELETE)
@ResponseBody
public Integer deleteUser(@RequestBody Integer id_search) {
return id_search;
}
I get this error message
"message": "JSON parse error: Can not deserialize instance of java.lang.Integer out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@1b77938; line: 1, column
integer. The integer type is used for integral numbers. JSON does not have distinct types for integers and floating-point values.
The @RequestBody annotation allows us to retrieve the request's body. We can then return it as a String or deserialize it into a Plain Old Java Object (POJO). Spring has built-in mechanisms for deserializing JSON and XML objects into POJOs, which makes this task a lot easier as well.
@RequestBody: Annotation is used to get request body in the incoming request. Note: First we need to establish the spring application in our project. Step 2: Click on Generate which will download the starter project. Step 3: Extract the zip file.
If you don't add @RequestBody it will insert null values (should use), no need to use @ResponseBody since it's part of @RestController.
Perhaps you are trying to send a request with JSON text in its body from a Postman client or something similar like this:
{
"id_search":2
}
This cannot be deserialized by Jackson since this is not an Integer (it seems to be, but it isn't). An Integer object from java.lang Integer is a little more complex.
For your Postman request to work, simply put (without curly braces { }
):
2
This statement is wrong
@RequestBody Integer id_search
This means Spring is expecting a body of type Integer
class. But what you are passing as the body doesn't match Integer class.
You have many options to fix this.
Remove @RequestBody
and just declare as
public Integer deleteUser(Integer id_search) {...}
With this you need to call this endpoint as
http://localhost:8080/seviceRS/delete/search?id_search=2
You can pass it as PathVariable
like this
http://localhost:8080/seviceRS/delete/search/2
For this to work change your controller method like this
@RequestMapping(value =
{"/delete/search/{id_search}"}, method =
RequestMethod.DELETE)
@ResponseBody
public Integer deleteUser(Integer id_search) {...}
If you want to send as body, then you should create a class which matches the json that you are sending.
For example. Create a class like
class Demo {
private int id_search;
//Getters & Setters
}
With this approach your controller method looks like
@RequestMapping(value = {"/delete/search/}, method =
RequestMethod.DELETE)
@ResponseBody
public Integer deleteUser(Demo demo) {
demo.getId_Search();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With