Проблема переполнения стека при конвертации в JSON - PullRequest
0 голосов
/ 29 октября 2018

Вот мой фрагмент кода. Я должен делать параллельные вызовы, используя 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 Ответ

0 голосов
/ 29 октября 2018

Проблема в том, что fastxml интерпретирует метод "getJson ()" как свойство, которое он должен включить в сгенерированный JSON.

Перепишите ваш класс как

@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 asJson() 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.asJson());
  }
}
...