How to load two load two models in @RequestBody at a time in Spring REST Controller
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
add a comment |
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
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
add a comment |
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
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
spring spring-mvc spring-boot
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
Please consider doing following:
Drop
@Autowired
fromTradeSkill
, add getters and setters fortrade
andskill
fields.Make sure that
name
andtradeName
fields withinSkill
andTrade
models respectively also got getters and setters.Fix your request json, replace
trade_name
withtradeName
.
add a comment |
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"
}
}
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%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
Please consider doing following:
Drop
@Autowired
fromTradeSkill
, add getters and setters fortrade
andskill
fields.Make sure that
name
andtradeName
fields withinSkill
andTrade
models respectively also got getters and setters.Fix your request json, replace
trade_name
withtradeName
.
add a comment |
Please consider doing following:
Drop
@Autowired
fromTradeSkill
, add getters and setters fortrade
andskill
fields.Make sure that
name
andtradeName
fields withinSkill
andTrade
models respectively also got getters and setters.Fix your request json, replace
trade_name
withtradeName
.
add a comment |
Please consider doing following:
Drop
@Autowired
fromTradeSkill
, add getters and setters fortrade
andskill
fields.Make sure that
name
andtradeName
fields withinSkill
andTrade
models respectively also got getters and setters.Fix your request json, replace
trade_name
withtradeName
.
Please consider doing following:
Drop
@Autowired
fromTradeSkill
, add getters and setters fortrade
andskill
fields.Make sure that
name
andtradeName
fields withinSkill
andTrade
models respectively also got getters and setters.Fix your request json, replace
trade_name
withtradeName
.
answered Nov 15 '18 at 5:33
Bohdan LevchenkoBohdan Levchenko
1,98621316
1,98621316
add a comment |
add a comment |
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"
}
}
add a comment |
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"
}
}
add a comment |
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"
}
}
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"
}
}
answered Nov 15 '18 at 5:37
ArpitArpit
12.7k85380
12.7k85380
add a comment |
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%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
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
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