4

I have created a simple Spring REST service (POST, PUT). But when i call this service from postman when value store in null.

Student Pojo Class

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.hateoas.ResourceSupport;

@SuppressWarnings("serial")
@Entity
@Table(schema="fishpool")
public class Student extends ResourceSupport implements Serializable {

@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Long sId;
private String name;
private String email;

public Student() {

}

public Student(Long sId, String name, String email) {
    this.sId = sId;
    this.name = name;
    this.email = email;
}

public Long getsId() {
    return sId;
}

public void setsId(Long sId) {
    this.sId = sId;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

@Override
public String toString() {
    return "Student [sId=" + sId + ", name=" + name + ", email=" + email + "]";
}
}

RestController Class

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.domain.Student;
import com.example.studentService.StudentSerivce;

@RestController
@RequestMapping(value="/api")
public class StudentController {

@Autowired
private StudentSerivce studentService;
private final static Logger log = LoggerFactory.getLogger(StudentController.class);

public StudentController(final StudentSerivce studentService) {
    this.studentService = studentService;
}

@GetMapping("/students") 
public List<Student> getAllStudent() {
    return studentService.findAllStudent();
}

@GetMapping("/student/{id}")
public Student getStudentById(@PathVariable("id") Long id) {
    return studentService.findByStudentId(id);
}

@PostMapping("/createStudent")
public ResponseEntity<Student> createStudent(Student student) {
    HttpHeaders headers =  new HttpHeaders();
    headers.add("Reader", "StudentController");
    log.info("Post Create Student : " + student);
    return new ResponseEntity<Student>(student, headers, HttpStatus.CREATED);
}

@PutMapping("/updateStudent")
public Student updateStudent(Student student) {
    log.info("Put Update Student : " + student);
    return studentService.updateStudent(student);
}

@DeleteMapping("/deleteStudent/{id}")
public void deleteStudentById(@PathVariable Long id) {
    studentService.deleteByStudentId(id);
}
}

Error.log

2018-07-06 14:31:05.405[0;39m [32m INFO[0;39m [35m20240[0;39m [2m---[0;39m [2m[nio-8080-exec-7][0;39m [36mc.example.controller.StudentController  [0;39m [2m:[0;39m Put Update Student : Student [sId=null, name=null, email=null]
[2m2018-07-06 14:31:05.705[0;39m [32mDEBUG[0;39m [35m20240[0;39m [2m---[0;39m [2m[nio-8080-exec-7][0;39m [36morg.hibernate.SQL                       [0;39m [2m:[0;39m insert into student (email, name) values (?, ?)
Hibernate: insert into student (email, name) values (?, ?)
[2m2018-07-06 14:31:44.984[0;39m [32m INFO[0;39m [35m20240[0;39m [2m---[0;39m [2m[nio-8080-exec-8][0;39m [36mc.example.controller.StudentController  [0;39m [2m:[0;39m Post Create Student : Student [sId=null, name=null, email=null]

I have no idea where is my mistake, please find the my mistake and suggest me. Please help me. I totally Confused where is my mistake.

Ng Sharma
  • 2,072
  • 8
  • 27
  • 49

3 Answers3

5

Add @RequestBody annotation before Student student like below.

public ResponseEntity<Student> createStudent(@RequestBody Student student)

mark the fields with @JsonProperty like this

@JsonProperty String sId;
@JsonProperty String name;

etc.

Alien
  • 15,141
  • 6
  • 37
  • 57
0

I am facing the same problem. the reason why I got the null value because of bad JSON request. I make a mistake: { "name": "111", "id":"11"" } after delete the suplus semicolon, the problem fixed.

Changyuan Chen
  • 308
  • 1
  • 3
  • 10
0

As Alien pointed out there should be a @RequestBody annotation attached to the model parameter of the post method.

@PostMapping("/createStudent")
public ResponseEntity<Student> createStudent(@RequestBody Student student) {
    HttpHeaders headers =  new HttpHeaders();
    headers.add("Reader", "StudentController");
    log.info("Post Create Student : " + student);
    return new ResponseEntity<Student>(student, headers, HttpStatus.CREATED);
}

Also, I would suggest that you remove the @SuppressWarnings("serial") & @Table(schema="fishpool") Annotations as only the @Entity Annotation is needed with (with the getters and setters of the Model)

tryingToLearn
  • 10,691
  • 12
  • 80
  • 114
perorororo
  • 191
  • 1
  • 14