4

I have one REST Controller where I have written this code

@PostMapping(value = "/otp")
public void otp(@RequestBody Integer mobile) {

    System.out.println(" Mobile = "+mobile);
}

And I am calling this method from Postman with the following inputs

URL : localhost:8080/otp

Body :

{
    "mobile":123456
}

But I am getting the following exception

org.springframework.http.converter.HttpMessageNotReadableException: 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

If I am taking String as a parameter like this

@PostMapping(value = "/otp")
public void otp(@RequestBody String mobile) {

    System.out.println(" Mobile = "+mobile);
}

And passing the inputs as

{
    "mobile":123456
}

Now it is printing in the console as follows

 Mobile = {
    "mobile":"123456"
}

But I want only this value 123456. How to achieve my requirement?

NOTE: I don't want to create any additional POJO class or even I don't want to send the data using query/path parameter.

Altaf Ansari
  • 726
  • 2
  • 7
  • 18
  • I would say: Instead of `{ "mobile" : 123456 }` simply send `123456` as the HTTP body. Alternatively create a class `Mobile` with an `Integer` field and use that as the method's parameter. – Seelenvirtuose Nov 21 '18 at 12:18

6 Answers6

7

If you want to send your body like:

{
    "mobile":123456
}

You will create another object to receive the value.

But if you only want to accept the integer value without any other object, you will not put json object in request body, but only the integer itself.

Body:

12345

Jaiwo99
  • 9,687
  • 3
  • 36
  • 53
1

Create a pojo class like below.

public class Mobile{
   private Integer mobile;
  //getter and setter
}

And then

public void otp(@RequestBody Mobile mobile)

to print value use

mobile.getMobile();
Alien
  • 15,141
  • 6
  • 37
  • 57
  • I don't want to send the data in the query parameter. I want to send through JSON body only. – Altaf Ansari Nov 21 '18 at 12:15
  • Although such a class is one way to go, OP wants to have an _integer_, not a _string_! – Seelenvirtuose Nov 21 '18 at 12:19
  • thanks for correction..in question one place he mentioned string too. – Alien Nov 21 '18 at 12:21
  • I don't want to create an extra POJO class for one attribute/value. If we have a requirement in 100 classes then we unnecessary we need to create 100 POJO classes. And even I don't want to send the data as query or path parameter. Except these, if there is any solution then tell me. – Altaf Ansari Nov 21 '18 at 12:25
1

You can use a class as request body:

class Request {
    public Integer mobile;
}

and specify the parameter like this:

public void otp(@RequestBody Request mobile) {
...
Henry
  • 42,982
  • 7
  • 68
  • 84
  • I don't want to create an extra class for one attribute/value. If we have a requirement in 100 classes then we unnecessary we need to create 100 POJO classes. – Altaf Ansari Nov 21 '18 at 12:21
  • @Altaf It is somewhat unusual in a real world situation to have 100 POJO classes of a single field. See AokoQin's answer which suggests having one common class, or explain your situation more clearly. – rougou Jun 12 '19 at 04:33
1

Converting process with json and @RequestBody is automatically and need you provide a class which contains proper field.If you insist to send data by request body,you could use String to receive json data as String.For example:

public void test(@RequestBody String request){
        log.info(request);
    }

In this way the request body you received is a String.You need some other tool to help you convert it.Like org.json,you could get more info from here HttpServletRequest get JSON POST data

But the easiest way is creating a new class to receive the data or changing @RequestBody to @RequestParam or @Pathvariable.

If you still want to use json as the request body,maybe you could create a common class A which contain lots of fields like name,phone number,email...Then after you send a request which only contains mobile,you just need to A.getMobile().In this way, even you get 100 request,you still need one POJO(but not recommend)

AokoQin
  • 159
  • 4
0

Just send the number in JSON. 12345 use @RequestBody to receive the int number. it will work. I use postman to send it in RAW JSON.

BTW, I am a beginner learning Spring Boot.

-1

if you have org.json.JSONObject

 @PostMapping(value = "/otp")
 public void otp(@RequestBody String mobile) {
   JSONObject obj = new JSONObejct(mobile);
   System.out.print(obj.getInt("mobile"));
}
Mr code.
  • 307
  • 2
  • 17