@Controller
public class StudentRegistrationController {
@RequestMapping(method = RequestMethod.POST, value="/register/reg")
@ResponseBody
StudentRegistrationReply registerStudent(@RequestBody Student student) {
System.out.println("In registerStudent");
StudentRegistrationReply stdregreply = new StudentRegistrationReply();
StudentRegistration.getInstance().add(student);
//We are setting the below value just to reply a message back to the caller
stdregreply.setId(student.getId());
stdregreply.setName(student.getName());
stdregreply.setAge(student.getAge());
stdregreply.setRegistrationNumber(student.getRegistrationNumber());
stdregreply.setPayment_detailsList(student.getPayment_detailsList());
stdregreply.setRegistrationStatus("Successful");
daocontroller.setStudentRegistration(stdregreply);
return stdregreply;
}
}
trying to map the postman request to but getting null
json is like
{
"id": 300,
"name": "kukri",
"age": 26,
"registrationNumber": "54326",
"Student_payment_details":
{
"pay": 50000,
"date": "23061994",
"phcounter": "SKB"
}
}
Java classes
public class Student {
private int id;
private String name;
private int age;
private String registrationNumber;
private Student_payment_details payment_detailsList; //getter and setter
}
Student_payment_details
instead of payment_detailsList
Request url:
curl -X POST \
http://localhost:8080/register/reg \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-d '{
"id": 300,
"name": "kukri",
"age": 26,
"registrationNumber": "54326",
"payment_detailsList": {
"pay": 50000,
"date": "23061994",
"phcounter": "SKB"
}
}'
Java Dtos:
import lombok.Data;
@Data
public class Student_payment_details {
int pay;
String date;
String phcounter;
}
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
private int age;
private String registrationNumber;
private Student_payment_details payment_detailsList; //getter and setter
}
Following image shows the content of a student variable populated inside the controller
Note:
I do not know your use case but as a general suggestion, please follow 1 type of naming convention,snake_case
or camelCase
.
In java mostly used is camelCase
.
Also naming of the variable should be similar to the class type,
here variable payment_detailsList is of type Student_payment_details
which leads to confusion, if you want JSON variable name to be different then you can use as
@JsonProperty("payment_detailsList")
private Student_payment_details student_payment_details;