How to load two load two models in @RequestBody at a time in Spring REST Controller












0















I am new to spring. I want to load data in two models Trade and Skill.
Written code sample is here -
Controller Code -



    @PostMapping("/create")
public String Create(@RequestBody TradeSkill tradeskill) {
System.out.println(tradeskill);
return "record is created";
}


Parent Request Body -



class TradeSkill {
@Autowired
protected Skill skill;
@Autowired
protected Trade trade;

public TradeSkill() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "TradeSkill [skill=" + skill + ", trade=" + trade + "]";
}
}


Json is -



{
"skill" : {
"name" : "new skill"
},
"trade" : {
"trade_name" : "Trade"
}
}


Console Output is -
TradeSkill [skill=null, trade=null]



What am i doing wrong. Any help would be appreciated.










share|improve this question

























  • TradeSklll class not properly implemented. @Autowired not required. Need parameterized constructor and getter-setter. Trade and Skill classes should follow the same.

    – bittu
    Nov 15 '18 at 5:31











  • I think you should be read @Autowried before you use it :).

    – phuchoangmai
    Nov 15 '18 at 5:33
















0















I am new to spring. I want to load data in two models Trade and Skill.
Written code sample is here -
Controller Code -



    @PostMapping("/create")
public String Create(@RequestBody TradeSkill tradeskill) {
System.out.println(tradeskill);
return "record is created";
}


Parent Request Body -



class TradeSkill {
@Autowired
protected Skill skill;
@Autowired
protected Trade trade;

public TradeSkill() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "TradeSkill [skill=" + skill + ", trade=" + trade + "]";
}
}


Json is -



{
"skill" : {
"name" : "new skill"
},
"trade" : {
"trade_name" : "Trade"
}
}


Console Output is -
TradeSkill [skill=null, trade=null]



What am i doing wrong. Any help would be appreciated.










share|improve this question

























  • TradeSklll class not properly implemented. @Autowired not required. Need parameterized constructor and getter-setter. Trade and Skill classes should follow the same.

    – bittu
    Nov 15 '18 at 5:31











  • I think you should be read @Autowried before you use it :).

    – phuchoangmai
    Nov 15 '18 at 5:33














0












0








0








I am new to spring. I want to load data in two models Trade and Skill.
Written code sample is here -
Controller Code -



    @PostMapping("/create")
public String Create(@RequestBody TradeSkill tradeskill) {
System.out.println(tradeskill);
return "record is created";
}


Parent Request Body -



class TradeSkill {
@Autowired
protected Skill skill;
@Autowired
protected Trade trade;

public TradeSkill() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "TradeSkill [skill=" + skill + ", trade=" + trade + "]";
}
}


Json is -



{
"skill" : {
"name" : "new skill"
},
"trade" : {
"trade_name" : "Trade"
}
}


Console Output is -
TradeSkill [skill=null, trade=null]



What am i doing wrong. Any help would be appreciated.










share|improve this question
















I am new to spring. I want to load data in two models Trade and Skill.
Written code sample is here -
Controller Code -



    @PostMapping("/create")
public String Create(@RequestBody TradeSkill tradeskill) {
System.out.println(tradeskill);
return "record is created";
}


Parent Request Body -



class TradeSkill {
@Autowired
protected Skill skill;
@Autowired
protected Trade trade;

public TradeSkill() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "TradeSkill [skill=" + skill + ", trade=" + trade + "]";
}
}


Json is -



{
"skill" : {
"name" : "new skill"
},
"trade" : {
"trade_name" : "Trade"
}
}


Console Output is -
TradeSkill [skill=null, trade=null]



What am i doing wrong. Any help would be appreciated.







spring spring-mvc spring-boot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 5:24







Mohd. Samar

















asked Nov 15 '18 at 5:12









Mohd. SamarMohd. Samar

33




33













  • TradeSklll class not properly implemented. @Autowired not required. Need parameterized constructor and getter-setter. Trade and Skill classes should follow the same.

    – bittu
    Nov 15 '18 at 5:31











  • I think you should be read @Autowried before you use it :).

    – phuchoangmai
    Nov 15 '18 at 5:33



















  • TradeSklll class not properly implemented. @Autowired not required. Need parameterized constructor and getter-setter. Trade and Skill classes should follow the same.

    – bittu
    Nov 15 '18 at 5:31











  • I think you should be read @Autowried before you use it :).

    – phuchoangmai
    Nov 15 '18 at 5:33

















TradeSklll class not properly implemented. @Autowired not required. Need parameterized constructor and getter-setter. Trade and Skill classes should follow the same.

– bittu
Nov 15 '18 at 5:31





TradeSklll class not properly implemented. @Autowired not required. Need parameterized constructor and getter-setter. Trade and Skill classes should follow the same.

– bittu
Nov 15 '18 at 5:31













I think you should be read @Autowried before you use it :).

– phuchoangmai
Nov 15 '18 at 5:33





I think you should be read @Autowried before you use it :).

– phuchoangmai
Nov 15 '18 at 5:33












2 Answers
2






active

oldest

votes


















0














Please consider doing following:




  1. Drop @Autowired from TradeSkill, add getters and setters for trade and skill fields.


  2. Make sure that name and tradeName fields within Skill and Trade models respectively also got getters and setters.


  3. Fix your request json, replace trade_name with tradeName.







share|improve this answer































    0














    As @Bohdan Levchenko suggested in his answer, classes should look like below:



    TradeSkill.java



    public class TradeSkill {
    protected Skill skill;
    protected Trade trade;

    public Skill getSkill() {
    return skill;
    }

    public void setSkill(Skill skill) {
    this.skill = skill;
    }

    public Trade getTrade() {
    return trade;
    }

    public void setTrade(Trade trade) {
    this.trade = trade;
    }

    @Override
    public String toString() {
    return "TradeSkill [skill=" + skill + ", trade=" + trade + "]";
    }
    }


    Trade.java



    public class Trade {
    private String trade_name;

    public String getTrade_name() {
    return trade_name;
    }

    public void setTrade_name(String trade_name) {
    this.trade_name = trade_name;
    }
    }


    Skill.java



    public class Skill {
    private String name;

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }
    }


    Requested JSON should be like:



    {
    "skill" : {
    "name" : "new skill"
    },
    "trade" : {
    "trade_name" : "Trade"
    }
    }





    share|improve this answer























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


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312825%2fhow-to-load-two-load-two-models-in-requestbody-at-a-time-in-spring-rest-control%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Please consider doing following:




      1. Drop @Autowired from TradeSkill, add getters and setters for trade and skill fields.


      2. Make sure that name and tradeName fields within Skill and Trade models respectively also got getters and setters.


      3. Fix your request json, replace trade_name with tradeName.







      share|improve this answer




























        0














        Please consider doing following:




        1. Drop @Autowired from TradeSkill, add getters and setters for trade and skill fields.


        2. Make sure that name and tradeName fields within Skill and Trade models respectively also got getters and setters.


        3. Fix your request json, replace trade_name with tradeName.







        share|improve this answer


























          0












          0








          0







          Please consider doing following:




          1. Drop @Autowired from TradeSkill, add getters and setters for trade and skill fields.


          2. Make sure that name and tradeName fields within Skill and Trade models respectively also got getters and setters.


          3. Fix your request json, replace trade_name with tradeName.







          share|improve this answer













          Please consider doing following:




          1. Drop @Autowired from TradeSkill, add getters and setters for trade and skill fields.


          2. Make sure that name and tradeName fields within Skill and Trade models respectively also got getters and setters.


          3. Fix your request json, replace trade_name with tradeName.








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 5:33









          Bohdan LevchenkoBohdan Levchenko

          1,98621316




          1,98621316

























              0














              As @Bohdan Levchenko suggested in his answer, classes should look like below:



              TradeSkill.java



              public class TradeSkill {
              protected Skill skill;
              protected Trade trade;

              public Skill getSkill() {
              return skill;
              }

              public void setSkill(Skill skill) {
              this.skill = skill;
              }

              public Trade getTrade() {
              return trade;
              }

              public void setTrade(Trade trade) {
              this.trade = trade;
              }

              @Override
              public String toString() {
              return "TradeSkill [skill=" + skill + ", trade=" + trade + "]";
              }
              }


              Trade.java



              public class Trade {
              private String trade_name;

              public String getTrade_name() {
              return trade_name;
              }

              public void setTrade_name(String trade_name) {
              this.trade_name = trade_name;
              }
              }


              Skill.java



              public class Skill {
              private String name;

              public String getName() {
              return name;
              }

              public void setName(String name) {
              this.name = name;
              }
              }


              Requested JSON should be like:



              {
              "skill" : {
              "name" : "new skill"
              },
              "trade" : {
              "trade_name" : "Trade"
              }
              }





              share|improve this answer




























                0














                As @Bohdan Levchenko suggested in his answer, classes should look like below:



                TradeSkill.java



                public class TradeSkill {
                protected Skill skill;
                protected Trade trade;

                public Skill getSkill() {
                return skill;
                }

                public void setSkill(Skill skill) {
                this.skill = skill;
                }

                public Trade getTrade() {
                return trade;
                }

                public void setTrade(Trade trade) {
                this.trade = trade;
                }

                @Override
                public String toString() {
                return "TradeSkill [skill=" + skill + ", trade=" + trade + "]";
                }
                }


                Trade.java



                public class Trade {
                private String trade_name;

                public String getTrade_name() {
                return trade_name;
                }

                public void setTrade_name(String trade_name) {
                this.trade_name = trade_name;
                }
                }


                Skill.java



                public class Skill {
                private String name;

                public String getName() {
                return name;
                }

                public void setName(String name) {
                this.name = name;
                }
                }


                Requested JSON should be like:



                {
                "skill" : {
                "name" : "new skill"
                },
                "trade" : {
                "trade_name" : "Trade"
                }
                }





                share|improve this answer


























                  0












                  0








                  0







                  As @Bohdan Levchenko suggested in his answer, classes should look like below:



                  TradeSkill.java



                  public class TradeSkill {
                  protected Skill skill;
                  protected Trade trade;

                  public Skill getSkill() {
                  return skill;
                  }

                  public void setSkill(Skill skill) {
                  this.skill = skill;
                  }

                  public Trade getTrade() {
                  return trade;
                  }

                  public void setTrade(Trade trade) {
                  this.trade = trade;
                  }

                  @Override
                  public String toString() {
                  return "TradeSkill [skill=" + skill + ", trade=" + trade + "]";
                  }
                  }


                  Trade.java



                  public class Trade {
                  private String trade_name;

                  public String getTrade_name() {
                  return trade_name;
                  }

                  public void setTrade_name(String trade_name) {
                  this.trade_name = trade_name;
                  }
                  }


                  Skill.java



                  public class Skill {
                  private String name;

                  public String getName() {
                  return name;
                  }

                  public void setName(String name) {
                  this.name = name;
                  }
                  }


                  Requested JSON should be like:



                  {
                  "skill" : {
                  "name" : "new skill"
                  },
                  "trade" : {
                  "trade_name" : "Trade"
                  }
                  }





                  share|improve this answer













                  As @Bohdan Levchenko suggested in his answer, classes should look like below:



                  TradeSkill.java



                  public class TradeSkill {
                  protected Skill skill;
                  protected Trade trade;

                  public Skill getSkill() {
                  return skill;
                  }

                  public void setSkill(Skill skill) {
                  this.skill = skill;
                  }

                  public Trade getTrade() {
                  return trade;
                  }

                  public void setTrade(Trade trade) {
                  this.trade = trade;
                  }

                  @Override
                  public String toString() {
                  return "TradeSkill [skill=" + skill + ", trade=" + trade + "]";
                  }
                  }


                  Trade.java



                  public class Trade {
                  private String trade_name;

                  public String getTrade_name() {
                  return trade_name;
                  }

                  public void setTrade_name(String trade_name) {
                  this.trade_name = trade_name;
                  }
                  }


                  Skill.java



                  public class Skill {
                  private String name;

                  public String getName() {
                  return name;
                  }

                  public void setName(String name) {
                  this.name = name;
                  }
                  }


                  Requested JSON should be like:



                  {
                  "skill" : {
                  "name" : "new skill"
                  },
                  "trade" : {
                  "trade_name" : "Trade"
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 5:37









                  ArpitArpit

                  12.7k85380




                  12.7k85380






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312825%2fhow-to-load-two-load-two-models-in-requestbody-at-a-time-in-spring-rest-control%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

                      Bressuire

                      Vorschmack

                      Quarantine