How to make controller endpoint to get two different objects in java spring?

后端 未结 3 643
臣服心动
臣服心动 2021-01-03 05:50

I have a server built with java and spring.

What i am trying to do is that my controller with the same endpoint will get two different objects.

This is an ex

3条回答
  •  醉梦人生
    2021-01-03 06:38

    Seems like you want program itself to determine what type the option is.But before you do that,are you sure what is the difference between these two Object?

    First is,what is the Option1And2 actually is?If the Option1And2 contains all the field of Option1 and Option2 but it's not the subclass of those,then probably the Option1And2 could be like:

    @Data
    public class Option1And2{
        private String name;
    
        private Long id;
    }
    
    • If you have other limits like "one of them and only one of them has to be null",then you could determine it by this rule.
    • If you don't have any other limitation,then maybe you could add a new field as a flag.

    In fact those code style are not recommend.If those two functions have different responsibilities,then maybe it's better to not mix them together.You will understand what I mean when you have to refactor these code.

    If these two functions do have lots of things in common,maybe it's better for you to refactor the service logic instead of just combining two service roughly by creating a new param Option1And2.

    By the way,what are you exactly want to do?Why do you want to merge those two object into one?

提交回复
热议问题