How to get Custom object in java spring?
up vote
0
down vote
favorite
I'm using java spring for my server.
My question is how can I get custom object through the controller.
Example for what I mean:
I know I can do that by doing two functions:
@RequestMapping(
path = arrayOf("getObject", "getObject/"),
method = arrayOf(RequestMethod.GET))
open fun getRecord1(@RequestBody data: CustomObjectOption1): ResponseEntity<*> {
return ResponseEntity<Any>(data.name,HttpStatus.OK)
}
@RequestMapping(
path = arrayOf("getObject", "getObject/"),
method = arrayOf(RequestMethod.GET))
open fun getRecord2(@RequestBody data: CustomObjectOption2): ResponseEntity<*> {
return ResponseEntity<Any>(data.number,HttpStatus.OK)
}
but I want to do it by only one endpoint:
@RequestMapping(
path = arrayOf("getObject", "getObject/"),
method = arrayOf(RequestMethod.GET))
open fun getRecord(@RequestBody data: CustomObjectOption): ResponseEntity<*> {
if(data instance option1)
return ResponseEntity<Any>(data.name,HttpStatus.OK)
else
return ResponseEntity(data.number,HttpStatus.OK)
else
}
such that the object can be like this:
option 1:
public class CustomObject {
private String name;
private Long id;
}
or option 2:
public class CustomObject {
private List<Integer> number;
private List<Long> count;
}
Is that possible to do that in java spring?
The only solution I was thinking is to use inheritance but I would like to know if there's different way...
Thank you for the help
spring kotlin controller
add a comment |
up vote
0
down vote
favorite
I'm using java spring for my server.
My question is how can I get custom object through the controller.
Example for what I mean:
I know I can do that by doing two functions:
@RequestMapping(
path = arrayOf("getObject", "getObject/"),
method = arrayOf(RequestMethod.GET))
open fun getRecord1(@RequestBody data: CustomObjectOption1): ResponseEntity<*> {
return ResponseEntity<Any>(data.name,HttpStatus.OK)
}
@RequestMapping(
path = arrayOf("getObject", "getObject/"),
method = arrayOf(RequestMethod.GET))
open fun getRecord2(@RequestBody data: CustomObjectOption2): ResponseEntity<*> {
return ResponseEntity<Any>(data.number,HttpStatus.OK)
}
but I want to do it by only one endpoint:
@RequestMapping(
path = arrayOf("getObject", "getObject/"),
method = arrayOf(RequestMethod.GET))
open fun getRecord(@RequestBody data: CustomObjectOption): ResponseEntity<*> {
if(data instance option1)
return ResponseEntity<Any>(data.name,HttpStatus.OK)
else
return ResponseEntity(data.number,HttpStatus.OK)
else
}
such that the object can be like this:
option 1:
public class CustomObject {
private String name;
private Long id;
}
or option 2:
public class CustomObject {
private List<Integer> number;
private List<Long> count;
}
Is that possible to do that in java spring?
The only solution I was thinking is to use inheritance but I would like to know if there's different way...
Thank you for the help
spring kotlin controller
I can't understand what you're asking. What are you trying to achieve?
– JB Nizet
Nov 19 at 21:01
@JBNizet can you tell me what you don't understand? in general I'm trying to pass to the controller the same object but I want that this object could be from type A or from type B
– JJ Redikes
Nov 19 at 21:08
Just don't. There is no good reason to send two completely different objects to the same resource. Create two different controller (or controller methods), doing two different things, and accepting two different types of objects.
– JB Nizet
Nov 19 at 21:12
Also, using GET to send a request body doesn't make any sense. GET requests don't have a body.
– JB Nizet
Nov 19 at 21:13
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm using java spring for my server.
My question is how can I get custom object through the controller.
Example for what I mean:
I know I can do that by doing two functions:
@RequestMapping(
path = arrayOf("getObject", "getObject/"),
method = arrayOf(RequestMethod.GET))
open fun getRecord1(@RequestBody data: CustomObjectOption1): ResponseEntity<*> {
return ResponseEntity<Any>(data.name,HttpStatus.OK)
}
@RequestMapping(
path = arrayOf("getObject", "getObject/"),
method = arrayOf(RequestMethod.GET))
open fun getRecord2(@RequestBody data: CustomObjectOption2): ResponseEntity<*> {
return ResponseEntity<Any>(data.number,HttpStatus.OK)
}
but I want to do it by only one endpoint:
@RequestMapping(
path = arrayOf("getObject", "getObject/"),
method = arrayOf(RequestMethod.GET))
open fun getRecord(@RequestBody data: CustomObjectOption): ResponseEntity<*> {
if(data instance option1)
return ResponseEntity<Any>(data.name,HttpStatus.OK)
else
return ResponseEntity(data.number,HttpStatus.OK)
else
}
such that the object can be like this:
option 1:
public class CustomObject {
private String name;
private Long id;
}
or option 2:
public class CustomObject {
private List<Integer> number;
private List<Long> count;
}
Is that possible to do that in java spring?
The only solution I was thinking is to use inheritance but I would like to know if there's different way...
Thank you for the help
spring kotlin controller
I'm using java spring for my server.
My question is how can I get custom object through the controller.
Example for what I mean:
I know I can do that by doing two functions:
@RequestMapping(
path = arrayOf("getObject", "getObject/"),
method = arrayOf(RequestMethod.GET))
open fun getRecord1(@RequestBody data: CustomObjectOption1): ResponseEntity<*> {
return ResponseEntity<Any>(data.name,HttpStatus.OK)
}
@RequestMapping(
path = arrayOf("getObject", "getObject/"),
method = arrayOf(RequestMethod.GET))
open fun getRecord2(@RequestBody data: CustomObjectOption2): ResponseEntity<*> {
return ResponseEntity<Any>(data.number,HttpStatus.OK)
}
but I want to do it by only one endpoint:
@RequestMapping(
path = arrayOf("getObject", "getObject/"),
method = arrayOf(RequestMethod.GET))
open fun getRecord(@RequestBody data: CustomObjectOption): ResponseEntity<*> {
if(data instance option1)
return ResponseEntity<Any>(data.name,HttpStatus.OK)
else
return ResponseEntity(data.number,HttpStatus.OK)
else
}
such that the object can be like this:
option 1:
public class CustomObject {
private String name;
private Long id;
}
or option 2:
public class CustomObject {
private List<Integer> number;
private List<Long> count;
}
Is that possible to do that in java spring?
The only solution I was thinking is to use inheritance but I would like to know if there's different way...
Thank you for the help
spring kotlin controller
spring kotlin controller
edited Nov 20 at 2:00
Jayson Minard
36.2k14104170
36.2k14104170
asked Nov 19 at 20:33
JJ Redikes
185
185
I can't understand what you're asking. What are you trying to achieve?
– JB Nizet
Nov 19 at 21:01
@JBNizet can you tell me what you don't understand? in general I'm trying to pass to the controller the same object but I want that this object could be from type A or from type B
– JJ Redikes
Nov 19 at 21:08
Just don't. There is no good reason to send two completely different objects to the same resource. Create two different controller (or controller methods), doing two different things, and accepting two different types of objects.
– JB Nizet
Nov 19 at 21:12
Also, using GET to send a request body doesn't make any sense. GET requests don't have a body.
– JB Nizet
Nov 19 at 21:13
add a comment |
I can't understand what you're asking. What are you trying to achieve?
– JB Nizet
Nov 19 at 21:01
@JBNizet can you tell me what you don't understand? in general I'm trying to pass to the controller the same object but I want that this object could be from type A or from type B
– JJ Redikes
Nov 19 at 21:08
Just don't. There is no good reason to send two completely different objects to the same resource. Create two different controller (or controller methods), doing two different things, and accepting two different types of objects.
– JB Nizet
Nov 19 at 21:12
Also, using GET to send a request body doesn't make any sense. GET requests don't have a body.
– JB Nizet
Nov 19 at 21:13
I can't understand what you're asking. What are you trying to achieve?
– JB Nizet
Nov 19 at 21:01
I can't understand what you're asking. What are you trying to achieve?
– JB Nizet
Nov 19 at 21:01
@JBNizet can you tell me what you don't understand? in general I'm trying to pass to the controller the same object but I want that this object could be from type A or from type B
– JJ Redikes
Nov 19 at 21:08
@JBNizet can you tell me what you don't understand? in general I'm trying to pass to the controller the same object but I want that this object could be from type A or from type B
– JJ Redikes
Nov 19 at 21:08
Just don't. There is no good reason to send two completely different objects to the same resource. Create two different controller (or controller methods), doing two different things, and accepting two different types of objects.
– JB Nizet
Nov 19 at 21:12
Just don't. There is no good reason to send two completely different objects to the same resource. Create two different controller (or controller methods), doing two different things, and accepting two different types of objects.
– JB Nizet
Nov 19 at 21:12
Also, using GET to send a request body doesn't make any sense. GET requests don't have a body.
– JB Nizet
Nov 19 at 21:13
Also, using GET to send a request body doesn't make any sense. GET requests don't have a body.
– JB Nizet
Nov 19 at 21:13
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Just as you have written, you can do it just like that:
@RequestMapping(...)
public void method(@RequestBody YourCustomClass body)
YourCustomClass
can be either option 1 or option 2.
And that's all :)
the only problem that you can't create to different objects with the same name in java... so YourCustomClass can be only one of the option. how can I know if its option 1 or 2?
– JJ Redikes
Nov 19 at 21:06
Then do it like this:@RequestBody Object body
if(body instance of com.example.class.CustomClass){} else if (body instance of com.example.another.class.CustomClass){}
– J. Doe
Nov 19 at 21:45
its almost fine. the problem here that java recognize the 'body' as list of key and value like a Jason... what do you recommenced to fix that?
– JJ Redikes
Nov 19 at 21:55
As far as I know if you give it the proper Json it should map it to Object without any problem. Just add the annotation@RestController
for the whole controller class
– J. Doe
Nov 19 at 22:18
I know how to map to object but not on the way that you recommends. it not working this way, im sure there is different way to do it...
– JJ Redikes
Nov 20 at 15:17
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Just as you have written, you can do it just like that:
@RequestMapping(...)
public void method(@RequestBody YourCustomClass body)
YourCustomClass
can be either option 1 or option 2.
And that's all :)
the only problem that you can't create to different objects with the same name in java... so YourCustomClass can be only one of the option. how can I know if its option 1 or 2?
– JJ Redikes
Nov 19 at 21:06
Then do it like this:@RequestBody Object body
if(body instance of com.example.class.CustomClass){} else if (body instance of com.example.another.class.CustomClass){}
– J. Doe
Nov 19 at 21:45
its almost fine. the problem here that java recognize the 'body' as list of key and value like a Jason... what do you recommenced to fix that?
– JJ Redikes
Nov 19 at 21:55
As far as I know if you give it the proper Json it should map it to Object without any problem. Just add the annotation@RestController
for the whole controller class
– J. Doe
Nov 19 at 22:18
I know how to map to object but not on the way that you recommends. it not working this way, im sure there is different way to do it...
– JJ Redikes
Nov 20 at 15:17
add a comment |
up vote
0
down vote
Just as you have written, you can do it just like that:
@RequestMapping(...)
public void method(@RequestBody YourCustomClass body)
YourCustomClass
can be either option 1 or option 2.
And that's all :)
the only problem that you can't create to different objects with the same name in java... so YourCustomClass can be only one of the option. how can I know if its option 1 or 2?
– JJ Redikes
Nov 19 at 21:06
Then do it like this:@RequestBody Object body
if(body instance of com.example.class.CustomClass){} else if (body instance of com.example.another.class.CustomClass){}
– J. Doe
Nov 19 at 21:45
its almost fine. the problem here that java recognize the 'body' as list of key and value like a Jason... what do you recommenced to fix that?
– JJ Redikes
Nov 19 at 21:55
As far as I know if you give it the proper Json it should map it to Object without any problem. Just add the annotation@RestController
for the whole controller class
– J. Doe
Nov 19 at 22:18
I know how to map to object but not on the way that you recommends. it not working this way, im sure there is different way to do it...
– JJ Redikes
Nov 20 at 15:17
add a comment |
up vote
0
down vote
up vote
0
down vote
Just as you have written, you can do it just like that:
@RequestMapping(...)
public void method(@RequestBody YourCustomClass body)
YourCustomClass
can be either option 1 or option 2.
And that's all :)
Just as you have written, you can do it just like that:
@RequestMapping(...)
public void method(@RequestBody YourCustomClass body)
YourCustomClass
can be either option 1 or option 2.
And that's all :)
answered Nov 19 at 21:01
J. Doe
31
31
the only problem that you can't create to different objects with the same name in java... so YourCustomClass can be only one of the option. how can I know if its option 1 or 2?
– JJ Redikes
Nov 19 at 21:06
Then do it like this:@RequestBody Object body
if(body instance of com.example.class.CustomClass){} else if (body instance of com.example.another.class.CustomClass){}
– J. Doe
Nov 19 at 21:45
its almost fine. the problem here that java recognize the 'body' as list of key and value like a Jason... what do you recommenced to fix that?
– JJ Redikes
Nov 19 at 21:55
As far as I know if you give it the proper Json it should map it to Object without any problem. Just add the annotation@RestController
for the whole controller class
– J. Doe
Nov 19 at 22:18
I know how to map to object but not on the way that you recommends. it not working this way, im sure there is different way to do it...
– JJ Redikes
Nov 20 at 15:17
add a comment |
the only problem that you can't create to different objects with the same name in java... so YourCustomClass can be only one of the option. how can I know if its option 1 or 2?
– JJ Redikes
Nov 19 at 21:06
Then do it like this:@RequestBody Object body
if(body instance of com.example.class.CustomClass){} else if (body instance of com.example.another.class.CustomClass){}
– J. Doe
Nov 19 at 21:45
its almost fine. the problem here that java recognize the 'body' as list of key and value like a Jason... what do you recommenced to fix that?
– JJ Redikes
Nov 19 at 21:55
As far as I know if you give it the proper Json it should map it to Object without any problem. Just add the annotation@RestController
for the whole controller class
– J. Doe
Nov 19 at 22:18
I know how to map to object but not on the way that you recommends. it not working this way, im sure there is different way to do it...
– JJ Redikes
Nov 20 at 15:17
the only problem that you can't create to different objects with the same name in java... so YourCustomClass can be only one of the option. how can I know if its option 1 or 2?
– JJ Redikes
Nov 19 at 21:06
the only problem that you can't create to different objects with the same name in java... so YourCustomClass can be only one of the option. how can I know if its option 1 or 2?
– JJ Redikes
Nov 19 at 21:06
Then do it like this:
@RequestBody Object body
if(body instance of com.example.class.CustomClass){} else if (body instance of com.example.another.class.CustomClass){}
– J. Doe
Nov 19 at 21:45
Then do it like this:
@RequestBody Object body
if(body instance of com.example.class.CustomClass){} else if (body instance of com.example.another.class.CustomClass){}
– J. Doe
Nov 19 at 21:45
its almost fine. the problem here that java recognize the 'body' as list of key and value like a Jason... what do you recommenced to fix that?
– JJ Redikes
Nov 19 at 21:55
its almost fine. the problem here that java recognize the 'body' as list of key and value like a Jason... what do you recommenced to fix that?
– JJ Redikes
Nov 19 at 21:55
As far as I know if you give it the proper Json it should map it to Object without any problem. Just add the annotation
@RestController
for the whole controller class– J. Doe
Nov 19 at 22:18
As far as I know if you give it the proper Json it should map it to Object without any problem. Just add the annotation
@RestController
for the whole controller class– J. Doe
Nov 19 at 22:18
I know how to map to object but not on the way that you recommends. it not working this way, im sure there is different way to do it...
– JJ Redikes
Nov 20 at 15:17
I know how to map to object but not on the way that you recommends. it not working this way, im sure there is different way to do it...
– JJ Redikes
Nov 20 at 15:17
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53382226%2fhow-to-get-custom-object-in-java-spring%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 can't understand what you're asking. What are you trying to achieve?
– JB Nizet
Nov 19 at 21:01
@JBNizet can you tell me what you don't understand? in general I'm trying to pass to the controller the same object but I want that this object could be from type A or from type B
– JJ Redikes
Nov 19 at 21:08
Just don't. There is no good reason to send two completely different objects to the same resource. Create two different controller (or controller methods), doing two different things, and accepting two different types of objects.
– JB Nizet
Nov 19 at 21:12
Also, using GET to send a request body doesn't make any sense. GET requests don't have a body.
– JB Nizet
Nov 19 at 21:13