我已经创建了一个简单的Spring REST服务(POST,PUT)。但是,当我从邮递员致电此服务时,值存储在 null 中。
Pojo学生课程
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类
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]
我不知道我的错误在哪里,请找出我的错误并提出建议。请帮我。我完全困惑我的错误在哪里。
答案 0 :(得分:0)
在学生下方添加@RequestBody
注释,如下所示。
public ResponseEntity<Student> createStudent(@RequestBody Student student)
像这样用@JsonProperty标记字段
@JsonProperty String sId;
@JsonProperty String name;
等
答案 1 :(得分:0)
我正面临着同样的问题。我由于错误的JSON请求而得到null值的原因。 我犯了一个错误: { “ name”:“ 111”, “ id”:“ 11”“ } 删除suplus分号后,问题已解决。
答案 2 :(得分:0)
正如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)