[英]post data for foreign key in postman
public class SectionProperties {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long section_id;
@Column( columnDefinition = "bigint default 0")
private Long section_no;
@ManyToOne
@JoinColumn(name="assessment_id")
@OnDelete(action = OnDeleteAction.CASCADE)
private AssesmentProperties foreign_key;
private String section_type;
private int weightage;
private int time;
private int No_of_questions;
//getters and setters
}
this is my model class.There is foreign key from another table AssessmentProperties .now when iam posting the data with postman my data looks like this这是我的模型类。有来自另一个表 AssessmentProperties 的外键。现在当我用邮递员发布数据时,我的数据看起来像这样
"section_type":"hard",
"weightage":2,
"time":2,
"no_of_questions":3,
"foreign_key":{
"assessment_id":1
}
but my input should be looking like this但我的输入应该是这样的
"section_type":"hard",
"weightage":2,
"time":2,
"no_of_questions":3,
"assessment_id":1
can anyone say me what should i do for this?谁能告诉我我该怎么做? and by the way this is the controller for post method顺便说一下,这是 post 方法的控制器
@RequestMapping(value="/SectionProperty",method=RequestMethod.POST)
public ResponseEntity<Long> createOrUpdateoptions(@RequestBody SectionProperties model)
{
SectionProperties updated=properties.createOrUpdateSections(model);
return new ResponseEntity<Long>(updated.getSection_id(),new HttpHeaders(),HttpStatus.OK);
}
Instead of using SectionProperties as @RequestBody
param, make a custom DTO (DataTransferObject) class that will look like this in JSON format:不要使用 SectionProperties 作为@RequestBody
参数,而是创建一个自定义的DTO (DataTransferObject)类,其 JSON 格式如下所示:
"section_type":"hard",
"weightage":2,
"time":2,
"no_of_questions":3,
"assessment_id":1
And like this in POJO:在 POJO 中是这样的:
public class SectionPropertiesDTO {
private int assessment_id;
private String section_type;
private int weightage;
private int time;
private int no_of_questions;
//getters and setters
}
Then your method should look like this, note that you will have to change your logic to convert from DTO object into entity and vice versea:然后你的方法应该是这样的,注意你必须改变你的逻辑才能从 DTO 对象转换为实体,反之亦然:
@RequestMapping(value="/SectionProperty",method=RequestMethod.POST)
public ResponseEntity<Long> createOrUpdateoptions(@RequestBody SectionPropertiesDTO model)
{
// TODO: change the createOrUpdateSections to convert from DTO into entity;
SectionProperties updated=properties.createOrUpdateSections(model);
return new ResponseEntity<Long>(updated.getSection_id(),new HttpHeaders(),HttpStatus.OK);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.