Reputation: 53
I am new to SpringBoot and I am using SpringBoot with JDBC for making an API. The INSERT is not working properly, I had made ID Unique and AutoIncremeneted it. Before I was inserting the ID and it was causing no problems.I am using MySql as the database.
The JSON I am using in POST is:
{
"name":"Name",
"email":"namegmail.com",
"dob":"2000-04-09",
"age":21
}
I am getting this Error.
{
"timestamp": "2021-06-20T13:10:16.925+00:00",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'application/octet-stream' not supported",
"path": "/api/v1/NewStudent"
}
Controller
@RestController
@RequestMapping(path = "api/v1/")
public class StudentController {
private StudentService studentService;
@Autowired
public StudentController(StudentService service){
this.studentService=service;
}
@GetMapping("/home")
public String getHome(){
return "Hey Welcome to Home";
}
@PostMapping("/NewStudent")
public void registerNewStudent(@RequestBody StudentClass studentClass){
studentService.addNewStudent(studentClass);
}
@GetMapping("/student/{id}")
public StudentClass getStudentSearch(@PathVariable(value = "id") Long userId){
return studentService.getStudentSelect(userId);
}
@GetMapping("/AllStudents")
public List<StudentClass> getAllStudents(){
return studentService.getAllStudents();
}
}
StudentDao
public class StudentDaoClass implements StudentDao{
@Autowired
private JdbcTemplate jdbctemplate;
private List<StudentClass> selectAll;
@Override
public int insert(StudentClass student) {
String query="INSERT INTO Student(std_name,std_email,std_dob,std_age)" +
"VALUES(?,?,?,?)";
int res=this.jdbctemplate.update(query,student.getName(),student.getEmail(),student.getDob(),student.getId());
return res;
}
@Override
public StudentClass selectStudent(Long id) {
String query="SELECT * FROM Student WHERE std_id=?";
RowMapper<StudentClass> rowMapper=new RowMapperClass();
StudentClass studentClass=this.jdbctemplate.queryForObject(query,rowMapper,id);
return studentClass;
}
@Override
public List<StudentClass> selectAllStudents() {
String query="SELECT * FROM Student";
selectAll=this.jdbctemplate.query(query,new RowMapperClass());
return selectAll;
}
public JdbcTemplate getJdbctemplate() {
return jdbctemplate;
}
public void setJdbctemplate(JdbcTemplate jdbctemplate) {
this.jdbctemplate = jdbctemplate;
}
}
StudentClass
public class StudentClass {
@Id
private Long id;
private String name;
private LocalDate dob;
private Integer age;
private String email;
public StudentClass() {
}
public StudentClass(String name, String email,LocalDate dob ,Integer age) {
this.name = name;
this.dob = dob;
this.age = age;
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDate getDob() {
return dob;
}
public void setDob(LocalDate dob) {
this.dob = dob;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "StudentClass{" +
"id=" + id +
", name='" + name + '\'' +
", dob=" + dob +
", age=" + age +
", email='" + email + '\'' +
'}';
}
}
Upvotes: 0
Views: 221
Reputation: 891
I think your rest call is not even reaching the jdbc part as there is problem in your rest request. I believe you are not setting the content-type properly. Try making below changes and see.
When sending the request please set 'Content-Type'
header property to 'application/json'
and enhance your below handler method what is consumes.
@PostMapping(path="/NewStudent", consumes="application/json")
public void registerNewStudent(@RequestBody StudentClass studentClass){
studentService.addNewStudent(studentClass);
}
Try debugging your application. Your rest call should reach your above handler method.
Upvotes: 1