英文:
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
}
这是我的模型类。有一个来自另一个表AssessmentProperties的外键。现在,当我用Postman发布数据时,我的数据如下所示:
"section_type":"hard",
"weightage":2,
"time":2,
"no_of_questions":3,
"foreign_key":{
"assessment_id":1
}
但是我的输入应该是这样的:
"section_type":"hard",
"weightage":2,
"time":2,
"no_of_questions":3,
"assessment_id":1
有谁能告诉我我应该怎么做呢?
顺便说一下,这是用于Post方法的控制器
英文:
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
"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
@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);
}
答案1
得分: 1
不要使用SectionProperties作为@RequestBody
参数,而是创建一个自定义的DTO(DataTransferObject)类,其JSON格式如下:
{
"section_type": "hard",
"weightage": 2,
"time": 2,
"no_of_questions": 3,
"assessment_id": 1
}
在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
}
然后您的方法应该如下所示,注意您需要更改逻辑以将DTO对象转换为实体对象,反之亦然:
@RequestMapping(value="/SectionProperty", method=RequestMethod.POST)
public ResponseEntity<Long> createOrUpdateOptions(@RequestBody SectionPropertiesDTO model) {
// TODO: 更改createOrUpdateSections方法以从DTO转换为实体对象;
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:
"section_type":"hard",
"weightage":2,
"time":2,
"no_of_questions":3,
"assessment_id":1
And like this in 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:
@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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论