提交在Postman中用于外键的数据

huangapple go评论105阅读模式
英文:

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:

&quot;section_type&quot;:&quot;hard&quot;,
&quot;weightage&quot;:2,
&quot;time&quot;:2,
&quot;no_of_questions&quot;:3,
 &quot;assessment_id&quot;: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=&quot;/SectionProperty&quot;,method=RequestMethod.POST)
public ResponseEntity&lt;Long&gt; createOrUpdateoptions(@RequestBody SectionPropertiesDTO model)
{
    // TODO: change the createOrUpdateSections to convert from DTO into entity;
    SectionProperties updated=properties.createOrUpdateSections(model);
    return new ResponseEntity&lt;Long&gt;(updated.getSection_id(),new HttpHeaders(),HttpStatus.OK);
}

huangapple
  • 本文由 发表于 2020年9月14日 16:34:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63880809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定