简体   繁体   English

在邮递员中发布外键的数据

[英]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.

相关问题 Spring 使用 Postman 引导,在使用外键进行 POST 时返回 null - Spring boot using Postman returning null when making a POST with foreign key 如何传递数据到外键,同时传递数据,我使用邮递员获取空值? - How to pass data into a foreign key , while passing data i am getting null using postman? Axios 使用 int 外键发布 - Axios Post with foreign key of int SpringBoot REST - 使用 HTTP POST JSON 数据插入带有外键的行 - SpringBoot REST - Insert Row With Foreign Key using HTTP POST JSON Data Spring Data Rest:在一对多关系中调用post后,外键更新为null - Spring Data Rest : Foreign key is update with null after post call in one to many relationship 使用 POST 请求更新 Spring Boot 中的外键 - Update foreign key in Spring Boot with a POST request Mongo Spring Data“外国”关键 - Mongo Spring Data “foreign” key 休眠:使用外键插入数据 - Hibernate: insert data with foreign key 在postman提交了一个post请求,只映射了4个实体(包括主键) - Submitted a post request in postman and only 4 entities mapped(including the primary key) 在邮递员中发送有效载荷时,是否有办法从另一个表的主键插入外键值的值? - Is there a way to insert the value of the foreign key value from the primary key of another table when sending a payload in postman?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM