course.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;
}
@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;
}
}
{
"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_training.jpad.model.review]]:com.fasterxml.Jackson.databind.jsonMappingException:2020-12-30 11:45:00.869 WARN 11152---[nio-8080-exec-2].C.J.MappingJackson2HttpMessageConverter:无法计算类型[[simple type,class com.example.jpa_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.DefaultHandlerExceptionResolver:已解决[org.springframework.web.httpmediatypenotsupportedexception:内容类型“application/json;charset=utf-8”不受支持]
尝试过的解决方案
使用@jsonBackReference和@jsonManagedReference使用@jsonIdentityInfo和@jsonIgnore但错误是相同的我可以从Java保存和检索数据,但当我通过邮差或使用curl命令发送数据时,我得到了上面的错误,我尝试了很多方法但无法修复它
我不建议将实体直接公开给您的控制器。在您的情况下,实体应该只包含JPA注释。您可以将DTO(数据传输对象)公开给您的控制器,然后将DTO映射到相应的实体。
审阅对象
public class ReviewDto {
private String reviewDescription;
private double courseRating;
private CourseDto course;
// getters, setters, etc
}
Coursedto
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("");
}
}
@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,它会自动为你制作映射器。
我试图在电影和用户之间建立多对多的关系。当我保存电影时,我会收到这个错误: 2017-12-01 16:12:43.351警告17328---[nio-8090-exec-5]。c、 j.MappingJackson2HttpMessageConverter:未能评估类型[[simple type,class com.movieseat.models.Movie]]:java的Jackson反序列
这是错误20-01-22 Wed14:17:56.475WARN MappingJackson2HttpMessageConzer无法评估Jackson反序列化类型[[简单类型,类lk.andunaechomedia.models.设备]]:com.fasterxml.jackson.databind.JsonMappingExc0019:多个返回引用属性与名称'user-运动'20-01-22周
添加到DTO对象后,我想向服务器发送一个列表 从…起 当向控制器发送对象时,它会抛出
问题内容: 我希望这个问题最终能找到一个答案,使之永远清楚。 有模特儿: JSON输入: 以及两种反序列化泛型类型的推荐方法: 要么 Jackson永远无法处理通用类型T,它认为这是JavaType的Map,但由于类型擦除而找到了对象类型构造函数参数,并抛出错误。那是杰克逊的臭虫,还是我做错了什么?TypeReference或JavaType的显式规范还有什么用? 问题答案: 您需要在构造函数上添
将有更多的类(不同的产品)扩展。 当我使用序列化它时, 这是输出: 将引发此异常: java.lang.IllegalArgumentException:意外的标记(VALUE_STRING),应为field_name:缺少包含类型id(类com.shubham.model.product的)的属性“type”,位于[源:n/a;行:-1,列:-1] 我在这里做错了什么?我观察了一下,发现了一个问
假设我有以下格式的JSON: 我试图避免自定义反序列化程序,并试图将上述JSON(称为Wrapper.java)反序列化为JavaPOJO。“type”字段指示“object”反序列化,即type=foo表示使用foo.java反序列化“object”字段。(如果type=Bar,则使用Bar.java反序列化对象字段)。Metadata/owner将始终以相同的方式对每个元数据使用简单的带Jac