java - 从休息控制器返回对象时获得无限行输出?
问题描述
我一直在从事个人项目,无法修复此错误。
我是 Spring 引导和休息控制器的新手。当我将我的 Product 实体从 RestController 返回给我的邮递员时,它会提供无限的输出。请给我一些建议。
我正在使用 mysql 数据库
package com.example.hackernews.entity;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Entity
@Table(name = "products")
public class Product {
@Id
@Column(name = "id", nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
@Column(name = "name")
String name;
@Column (name="price")
Interger price;
@JoinColumn(name = "customer_id")
@ManyToOne
Customer customer;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name ) {
this.name = name;
}
public Integer getPrice() {
return price;
}
public void setPrice(Interger price ) {
this.price = price;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer ) {
this.customer = customer;
}
}
提前致谢。
解决方案
我添加了“JsonIgnore”注释以防止您遇到错误。检查此代码,让我知道它是如何工作的。
包 com.example.hackernews.entity;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
@Table(name = "products")
public class Product {
@Id
@Column(name = "id", nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
@Column(name = "name")
String name;
@Column(name="price")
Interger price;
@JoinColumn(name = "customer_id")
@ManyToOne
@JsonIgnore
Customer customer;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPrice() {
return price;
}
public void setPrice(Interger price) {
this.price = price;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
推荐阅读
- bash - 如何使用文件中的行作为 grep 的关键字?
- r - 使用 purrr 在相同大小的向量列表中拆分向量
- python-3.x - 尝试将virtualenv与vs代码一起使用,当解释器是python3时,文件是python 2而不是3
- mongodb - MongoDB - 如何更改当前登录用户的密码?
- terraform - Terraform:检查 PowerDNS 记录时出错
- maven - Maven:如何使用发布插件部署两个工件?
- php - 使用 php 的表单中的 URL
- python - 无法在 if 语句中获取 plot.ion() 以评估模型的进度
- c# - 从派生类创建只读泛型列表
- bash - 如何将带有引号的可变数量的命令行参数解析为eval?