在Spring中遇到了post方法的问题。当我尝试将对象发布到数据库时,客户端应用程序已挂起,我需要终止它。带卷曲的物体正常工作。我正在获取日志:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: java.util.ArrayList[0]->dareq.Point["points"]->java.util.ArrayList[0]->dareq.Point["points"]->java.util.ArrayList[0]->dareq.Point["points"]->java.util.ArrayList[0]->dareq.Point["points"]
客户端:
积分班:
@JsonIgnoreProperties(ignoreUnknown=true)
public class Point {
private Long id;
private List<String> bssid = new ArrayList<>();
private List<Double> rssi = new ArrayList<>();
private double x;
private double y;
public Point()
{
}
public Point(Long id, List<String> bssid, List<Double> rssi, double x, double y){
this.id = id;
this.bssid = bssid;
this.rssi = rssi;
this.x = x;
this.y = y;
}
public long getid(){
return id;
}
public void setId(Long id) { this.id=id; }
public List<String> getbssid(){
return bssid;
}
public void setbssid(List<String> bssid)
{
this.bssid=bssid;
}
public List<Double> getrssi(){
return rssi;
}
public void setrssi(List<Double> rssi)
{
this.rssi=rssi;
}
public Double getx(){
return x;
}
public void setx(Double x){
this.x=x;
}
public Double gety(){
return y;
}
public void sety(Double y){
this.y=y;
}
public List<Point> getPoints()
{
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<List<Point>> pointResponse = restTemplate.exchange("http://naviserv-env-1.4jdgmueexz.us-east-2.elasticbeanstalk.com/points/", HttpMethod.GET, null,
new ParameterizedTypeReference<List<Point>>()
{});
List<Point> points = pointResponse.getBody();
return (points);
}
public Point getPoint(long id)
{
RestTemplate restTemplate = new RestTemplate();
Point point = restTemplate.getForObject("http://naviserv-env-1.4jdgmueexz.us-east-2.elasticbeanstalk.com/points/" + id, Point.class);
return point;
}
public void postPoint()
{
RestTemplate restTemplate = new RestTemplate();
restTemplate.postForObject("http://naviserv-env-1.4jdgmueexz.us-east-2.elasticbeanstalk.com/points", this, this.getClass());
}
public void deletePoint(Long id)
{
RestTemplate restTemplate = new RestTemplate();
restTemplate.delete("http://localhost:5000/points/" + id);
}
@Override
public String toString() {
return "Point{" +
"id='" + id + '\'' + ", " +
"bssid='" + bssid + '\'' + ", " +
"rssi='" + rssi + '\'' + ", " +
"x='" + x + '\'' + ", " +
"y='" + y + '\'' +
'}';
}
}
服务器端:
控制器:
@PostMapping("/points")
Points newPoints(@RequestBody Points newPoints){
return repository.save(newPoints);
}
积分等级:
@Data
@Entity
public class Points {
private @Id @GeneratedValue Long id;
@Column
@ElementCollection
private List<String> bssid;
@Column
@ElementCollection
private List<Double> rssi;
private double x;
private double y;
Points (){
};
Points(List<String> bssid, List<Double> rssi, double x, double y){
this.bssid=bssid;
this.rssi=rssi;
this.x=x;
this.y=y;
}
}
在AWS服务器上检查日志,当我尝试在客户端上发布对象时,会调用很多get方法:
78.28.26.200 - - [07/Jan/2019:00:34:31 +0000] "GET /points/ HTTP/1.1" 200 87 "-" "Java/1.8.0_172" "-"
78.28.26.200 - - [07/Jan/2019:00:34:32 +0000] "GET /points/ HTTP/1.1" 200 87 "-" "Java/1.8.0_172" "-"
78.28.26.200 - - [07/Jan/2019:00:34:32 +0000] "GET /points/ HTTP/1.1" 200 87 "-" "Java/1.8.0_172" "-"
78.28.26.200 - - [07/Jan/2019:00:34:32 +0000] "GET /points/ HTTP/1.1" 200 87 "-" "Java/1.8.0_172" "-"
78.28.26.200 - - [07/Jan/2019:00:34:32 +0000] "GET /points/ HTTP/1.1" 200 87 "-" "Java/1.8.0_172" "-"
我将非常感谢您的帮助。谢谢
答案 0 :(得分:1)
Jackson将方法public List<Point> getPoints()
解释为points
属性的获取方法,并调用该方法获取值,从而导致无限递归错误。
解决方案
将@JsonIgnore
放在public List<Point> getPoints()
上(甚至在所有非getter方法上更好),或将其重命名为public List<Point> points()
。