How to send Integer value in the form of JSON format and recieve in REST Controller?
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.
java json spring spring-boot spring-restcontroller
add a comment |
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.
java json spring spring-boot spring-restcontroller
I would say: Instead of{ "mobile" : 123456 }
simply send123456
as the HTTP body. Alternatively create a classMobile
with anInteger
field and use that as the method's parameter.
– Seelenvirtuose
Nov 21 '18 at 12:18
add a comment |
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.
java json spring spring-boot spring-restcontroller
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.
java json spring spring-boot spring-restcontroller
java json spring spring-boot spring-restcontroller
edited Nov 21 '18 at 12:29
Altaf
asked Nov 21 '18 at 12:09
AltafAltaf
3110
3110
I would say: Instead of{ "mobile" : 123456 }
simply send123456
as the HTTP body. Alternatively create a classMobile
with anInteger
field and use that as the method's parameter.
– Seelenvirtuose
Nov 21 '18 at 12:18
add a comment |
I would say: Instead of{ "mobile" : 123456 }
simply send123456
as the HTTP body. Alternatively create a classMobile
with anInteger
field and use that as the method's parameter.
– Seelenvirtuose
Nov 21 '18 at 12: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
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
add a comment |
5 Answers
5
active
oldest
votes
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) {
...
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
Nov 21 '18 at 12:21
add a comment |
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();
I don't want to send the data in the query parameter. I want to send through JSON body only.
– Altaf
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
Nov 21 '18 at 12:25
add a comment |
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
add a comment |
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)
add a comment |
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"));
}
1
can you please give me reason for down vote? i will learn:(
– Mr code.
Nov 21 '18 at 12:28
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411744%2fhow-to-send-integer-value-in-the-form-of-json-format-and-recieve-in-rest-control%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
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) {
...
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
Nov 21 '18 at 12:21
add a comment |
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) {
...
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
Nov 21 '18 at 12:21
add a comment |
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) {
...
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) {
...
answered Nov 21 '18 at 12:18
HenryHenry
34k54260
34k54260
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
Nov 21 '18 at 12:21
add a comment |
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
Nov 21 '18 at 12:21
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
Nov 21 '18 at 12:21
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
Nov 21 '18 at 12:21
add a comment |
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();
I don't want to send the data in the query parameter. I want to send through JSON body only.
– Altaf
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
Nov 21 '18 at 12:25
add a comment |
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();
I don't want to send the data in the query parameter. I want to send through JSON body only.
– Altaf
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
Nov 21 '18 at 12:25
add a comment |
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();
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();
edited Nov 21 '18 at 12:21
answered Nov 21 '18 at 12:12


AlienAlien
5,03831026
5,03831026
I don't want to send the data in the query parameter. I want to send through JSON body only.
– Altaf
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
Nov 21 '18 at 12:25
add a comment |
I don't want to send the data in the query parameter. I want to send through JSON body only.
– Altaf
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
Nov 21 '18 at 12:25
I don't want to send the data in the query parameter. I want to send through JSON body only.
– Altaf
Nov 21 '18 at 12:15
I don't want to send the data in the query parameter. I want to send through JSON body only.
– Altaf
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
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
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
Nov 21 '18 at 12:25
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
Nov 21 '18 at 12:25
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 21 '18 at 12:32
Jaiwo99Jaiwo99
6,53732343
6,53732343
add a comment |
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
edited Nov 22 '18 at 3:29
answered Nov 22 '18 at 3:16


AokoQinAokoQin
794
794
add a comment |
add a comment |
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"));
}
1
can you please give me reason for down vote? i will learn:(
– Mr code.
Nov 21 '18 at 12:28
add a comment |
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"));
}
1
can you please give me reason for down vote? i will learn:(
– Mr code.
Nov 21 '18 at 12:28
add a comment |
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"));
}
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"));
}
edited Nov 21 '18 at 12:37
answered Nov 21 '18 at 12:24
Mr code.Mr code.
13811
13811
1
can you please give me reason for down vote? i will learn:(
– Mr code.
Nov 21 '18 at 12:28
add a comment |
1
can you please give me reason for down vote? i will learn:(
– Mr code.
Nov 21 '18 at 12:28
1
1
can you please give me reason for down vote? i will learn:(
– Mr code.
Nov 21 '18 at 12:28
can you please give me reason for down vote? i will learn:(
– Mr code.
Nov 21 '18 at 12:28
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411744%2fhow-to-send-integer-value-in-the-form-of-json-format-and-recieve-in-rest-control%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I would say: Instead of
{ "mobile" : 123456 }
simply send123456
as the HTTP body. Alternatively create a classMobile
with anInteger
field and use that as the method's parameter.– Seelenvirtuose
Nov 21 '18 at 12:18