man发送json数据,给spring boot jpa项目中的类型计算jackson反序列化失败

mccptt67  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(224)

课程.java

package com.example.jpa_training.JPAD.model;

@Entity
@Table(name = "COURSE")
public class Course implements Serializable{

    public Course() {}

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String name;
    private String description;

    @ManyToOne(targetEntity=Department.class)
    @JsonIgnore
    private Department department;

    @ManyToMany(mappedBy="courses", targetEntity=Student.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JsonIgnore
    private Set<Student> students = new HashSet<Student>();

    @ManyToOne(cascade = CascadeType.MERGE)
    @JoinColumn(name="professor_id")
    @JsonManagedReference
    private Professor professor;

    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 Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Professor getProfessor() {
        return professor;
    }

    public void setProfessor(Professor professor) {
        this.professor = professor;
    }

    public Set<Student> getStudents() {
        return students;
    }

    public void addStudent(Student student) {
        this.students.add(student);

    }

    public void removeStudent(Student student) {
        this.students.remove(student);
    }

    @OneToMany(mappedBy = "course", fetch = FetchType.LAZY)
    private Set<Review> reviews;
}

评论.java

@Entity
public class Review implements Serializable{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long reviewId;

    @ManyToOne
    private Course course;

    private String reviewDescription;

    private double courseRating;

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public String getReviewDescription() {
        return reviewDescription;
    }

    public void setReviewDescription(String reviewDescription) {
        this.reviewDescription = reviewDescription;
    }

    public double getCourseRating() {
        return courseRating;
    }

    public void setCourseRating(double courseRating) {
        this.courseRating = courseRating;
    }
}

Postman 输入

{
    "course": {
        "id": 4,
        "name": "Data Analysis",
        "description": "Just take it",
        "professor": {
            "name": "Kapil Dev",
            "qualification": "M.Tech",
            "department": {
                "deptId": 1,
                "deptName": "Big Data",
                "buildingName": "DS-04"
            }
        }
    },
    "reviewDescription": "Good course, nice teaching",
    "courseRating": 0.0
}

错误日志
未能评估类型simple type,class com.example.jpa\u training.jpad.model.review:com.fasterxml.jackson.databind.jsonmappingexception:2020-12-30 11:45:00.869 warn 11152---[nio-8080-exec-2].c.j.mappingjackson2httpessageconverter:未能评估类型simple type,class com.example.jpa\u training.jpad.model.review:com.fasterxml.jackson.databind.jsonmappingexception:2020-12-30 11:45:00.869 warn 11152---[nio-8080-exec-2].w.s.m.s.defaulthandlerexception resolver:resolved[org.springframework.web.httpmediatypenotsupportedexception:content type'application/json;字符集=utf-8'不支持]
尝试过的解决方案
使用@jsonbackreference和@jsonmanagedreference,使用@jsonidentityinfo和@jsonignore,但是错误是一样的,我可以从java保存和检索数据,但是当我通过postman或使用curl命令发送数据时,我得到了上面的错误,我尝试了很多方法,但都无法修复它

vaqhlq81

vaqhlq811#

我不建议将实体直接暴露给控制器。在您的案例中,实体应该只包含jpa注解。您可以将dto(数据传输对象)公开给控制器,然后将dtoMap到相应的实体。
复习到

public class ReviewDto {
    private String reviewDescription;

    private double courseRating;

    private CourseDto course;

    // getters, setters, etc
}

走到

public class CourseDto {
    private Long id;

    private String name;

    private String description;

    // professorDto, getters, setters, etc
}

一个示例演示了如何使用控制器类

@RestController
public class DemoController {
    private final ReviewDtoMapper reviewDtoMapper;
    private final ReviewService reviewService;

    public DemoController(ReviewDtoMapper reviewDtoMapper,
                          ReviewService reviewService) {
        this.reviewDtoMapper = reviewDtoMapper;
        this.reviewService = reviewService;
    }

    @PostMapping(value = "demo")
    public ResponseEntity<String> postReview(@RequestBody ReviewDto reviewDto) {
        final Review review = reviewDtoMapper.mapFrom(reviewDto);

        reviewService.save(review);

        return ResponseEntity.ok("");
    }
}

要从reviewdtoMap到review实体的类,反之亦然。

@Component
public class ReviewDtoMapper {

    public ReviewDto mapTo(final Review entity) {

        ReviewDto reviewDto = new ReviewDto();
        reviewDto.setReviewDescription(entity.getReviewDescription());
        // set all the properties you want
        return reviewDto;
    }

    public Review mapFrom(ReviewDto dto) {

        Review review = new Review();
        review.setReviewDescription(dto.getReviewDescription());
        // set all the properties you want
        return review;
    }
}

当然,你必须根据自己的需要进行调整。
如果你喜欢这种方式做事情,我建议你检查mapstruct,它会自动为你制作Map器。

相关问题