转换为JSON时出现java堆栈溢出问题
这是我的代码片段。我必须使用ForkJoin进行并行调用,但我的抛出堆栈溢出,甚至没有到达服务调用
请求:
@NoArgsConstructor
@Getter
@Setter
public class Request{
@JsonProperty("id")
private String id;
public Request id(String id){
this.id=id;
return this;
}
public static Request getRequest(AnotherRequest anotherReq){
return new Request().id(anotherReq.identity);
}
public String getJson() throws Exception {
return new ObjectMapper().writeValueasString(this);
}
}
MyCallable:
@AllargsConstructor
MyCallable implements Callable<Response> {
private Service service;
private Request request;
public Response call () throws Exception{
return service.callWebservice(this.request.getJson());
}
}
主要方法:
@Autowired
private Service service;
List<MyCallable> jobs = new ArrayList<MyCallable>()
anotherRequestSS.forEach(anotherRequest->{
jobs.add(new MyCallable(Request.getRequest(anotherRequest),service);
}
ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
pool.invokeAll(jobs);
这段代码进入无限循环,这意味着getJson
被称为无限次,导致堆栈溢出。它甚至没有达到invokeAll()
的程度。这可能是什么原因
列表大小anotherRequestSS
为2
# 1 楼答案
问题是fasterxml将方法“getJson()”解释为一个属性,它必须包含在生成的JSON中
把你的课改写成
还有你的MyCallable