@PostMapping 和@PutMapping 获取空值

@PostMapping And @PutMapping getting null values

我创建了一个简单的 Spring REST 服务 (POST, PUT)。但是当我从邮递员那里调用这项服务时,值存储在 null.

学生波乔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 + "]";
}
}

RestControllerClass

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]

我不知道我的错误在哪里,请找到我的错误并建议我。请帮我。我完全困惑我的错误在哪里。

在 Student 学生之前添加 @RequestBody 注释,如下所示。

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

像这样用@JsonProperty 标记字段

@JsonProperty String sId;
@JsonProperty String name;

等等

我遇到了同样的问题。由于错误的 JSON 请求,我得到空值的原因。 我犯了一个错误: { "name": "111", "id":"11"" } 删除分号后问题解决

正如 Alien 指出的那样,post 方法的模型参数应该附加一个 @RequestBody 注释。

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

此外,我建议您删除 @SuppressWarnings("serial") & @Table(schema="fishpool") 注释,因为只需要 @Entity 注释(使用模型的 getter 和 setter)