Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get integer on requestbody

Tags:

java

spring

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
like image 399
Afsrd3 Avatar asked Mar 09 '18 11:03

Afsrd3


People also ask

Can we pass integer in JSON?

integer. The integer type is used for integral numbers. JSON does not have distinct types for integers and floating-point values.

Can @RequestBody be a string?

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.

How do I get body data in spring boot?

@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.

Is @RequestBody required with @RestController?

If you don't add @RequestBody it will insert null values (should use), no need to use @ResponseBody since it's part of @RestController.


2 Answers

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
like image 67
Javier Sanchez C Avatar answered Oct 19 '22 23:10

Javier Sanchez C


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.

  1. 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
    
  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) {...}
    
  3. 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();
    }
    
like image 32
pvpkiran Avatar answered Oct 19 '22 23:10

pvpkiran