Skip to content
Advertisement

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

"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);
}

Advertisement

Answer

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);
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement