@PostMapping和@PutMapping获取空值

问题描述 投票:0回答:2

我创建了一个简单的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]

我不知道我的错误在哪里,请找出我的错误并建议我。请帮我。我完全糊涂我的错误在哪里。

json spring spring-mvc spring-boot spring-rest
2个回答
1
投票

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

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

用这样的@JsonProperty标记字段

@JsonProperty String sId;
@JsonProperty String name;

等等


0
投票

我面临同样的问题。我之所以因为JSON请求错误而得到null值的原因。我犯了一个错误:{“name”:“111”,“id”:“11”“}删除了分号后,修复了问题。

© www.soinside.com 2019 - 2024. All rights reserved.