【问题标题】:Status: 415, error: "Unsupported Media Type" - for POST request OneToMany - ManyToOne relation状态:415,错误:“不支持的媒体类型” - 用于 POST 请求 OneToMany - ManyToOne 关系
【发布时间】:2019-03-15 20:54:35
【问题描述】:

我已经尝试解决这个问题两天了,我已经阅读了所有内容并尝试了很多选项,但没有任何反应,我看不出我错在哪里。 我只需要添加一个合并其他 2 个表的表条目。对于冗长的描述,我深表歉意,但我只是想详尽无遗
这是表格之间的关系

这是我的代码:
用户类

import com.fasterxml.jackson.annotation.*;
import javax.persistence.*;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "users")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private int id;

@Column(name = "FirstName")
private String firstName;

@Column(name = "LastName")
private String lastName;

@OneToMany(mappedBy = "user", fetch = FetchType.EAGER)
@JsonIgnore
private List<ProductRatings> userGroups = new ArrayList<>();

public User() {}
}

产品类别

import com.fasterxml.jackson.annotation.*;  
import org.hibernate.annotations.Fetch;  
import org.hibernate.annotations.FetchMode;  
import javax.persistence.*;  
import javax.validation.constraints.DecimalMax;  
import javax.validation.constraints.DecimalMin;  
import javax.validation.constraints.Size;  
import java.util.ArrayList;  
import java.util.List;  

@Entity  
@Table(name = "products")  
public class Product{  
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private int id;

@Column(name = "Name")
private String name;

@OneToMany(mappedBy = "product", fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
@JsonIgnore
private List<ProductRatings> productGroups = new ArrayList<>();

public Product() {}
}

ProductRating 类

import javax.persistence.*;  
@Entity  
@Table(name = "product_ratings")  
public class ProductRatings {  

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private int id;

@Column(name = "Rating")
private int rating;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "UserID")
private User user;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "ProductID")
private Product product;

public ProductRatings() {}
}  

这是我的邮递员请求
这是请求状态

IDEA 控制台中的最终代码
2019-03-15 20:44:30.150 WARN 7288 --- [nio-8080-exec-1] .c.j.MappingJackson2HttpMessageConverter:无法评估 Jackson 反序列化类型 [[simple type, class com.teamwork_2.producttag.models.ProductRatings ]]:com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法处理托管/反向引用'defaultReference':反向引用类型(java.util.List)与托管类型(com.teamwork_2.producttag.models.Product)不兼容)

2019-03-15 20:44:30.158  WARN 7288 --- [nio-8080-exec-1] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.teamwork_2. producttag.models. ProductRatings]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot handle managed/back reference 'defaultReference': back reference type (java.util.List) not compatible with managed type (com.teamwork_2. producttag.models. Product)   

2019-03-15 20:44:30.174  WARN 7288 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported]  

【问题讨论】:

  • 永远不要在休息服务上使用实体类进行数据交换。了解 3 层架构
  • 我猜你知道我现在正在学习,不可能什么都知道

标签: java spring hibernate one-to-many many-to-one


【解决方案1】:

这是由于对 REST 客户端的意外输入。 尝试添加

内容类型:application/json

接受:应用程序/json

 在 REST 客户端标头部分中。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2017-10-23
    • 2015-06-17
    • 2016-11-21
    • 2019-01-16
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 2015-07-24
    • 2018-10-13
    相关资源
    最近更新 更多