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










share|improve this question
























  • 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

















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










share|improve this question
























  • 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















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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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














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 :)






share|improve this answer





















  • 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











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',
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
});


}
});














draft saved

draft discarded


















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

























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 :)






share|improve this answer





















  • 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















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 :)






share|improve this answer





















  • 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













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 :)






share|improve this answer












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 :)







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python