StackOverflowError: null at com.google.gson.stream.JsonWrite



提到另一个问题,似乎我对整个方法有问题,我不明白。实际上,现在我不再有问题中指定的错误,但是我有一个新的错误:

2019-04-16 16:32:16.843 ERROR 7452 --- [nio-9090-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.StackOverflowError] with root cause
java.lang.StackOverflowError: null
at java.base/java.nio.CharBuffer.<init>(CharBuffer.java:281) ~[na:na]
at java.base/java.nio.HeapCharBuffer.<init>(HeapCharBuffer.java:75) ~[na:na]
at java.base/java.nio.CharBuffer.wrap(CharBuffer.java:393) ~[na:na]
at java.base/sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:280) ~[na:na]
at java.base/sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125) ~[na:na]
at java.base/sun.nio.cs.StreamEncoder.write(StreamEncoder.java:135) ~[na:na]
at java.base/java.io.OutputStreamWriter.write(OutputStreamWriter.java:229) ~[na:na]
at java.base/java.io.Writer.write(Writer.java:249) ~[na:na]
at com.google.gson.stream.JsonWriter.string(JsonWriter.java:566) ~[gson-2.8.5.jar:na]
at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:402) ~[gson-2.8.5.jar:na]
at com.google.gson.stream.JsonWriter.value(JsonWriter.java:417) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.TypeAdapters$16.write(TypeAdapters.java:406) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.TypeAdapters$16.write(TypeAdapters.java:390) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) ~[gson-2.8.5.jar:na]
[...]

在整个堆栈跟踪中,我有这些行重复,并且没有关于引发此错误的行的信息。我正在使用可流动并通过 POST 请求传递数据。我使用的方法是这样的:

@PostMapping(path = PathConstants.START_ACTION)
public ResponseEntity<BaseResponse<ProcessInstance>> start(@PathVariable String processDefinitionId,
@RequestBody(required = false) Map<String, Object> params) throws FileNotFoundException {
LOGGER.info("starting process definition with id {}", processDefinitionId);
Map<String, Object> variables;
ResponseEntity<BaseResponse<ProcessInstance>> response = null;
PrintStream o = new PrintStream(new File("output.txt"));
System.setOut(o);
ProcessInstance pi = null;
LocalDateTime time = LocalDateTime.now();
try {
if (params != null) {           
pi = runtimeService.startProcessInstanceById(processDefinitionId, params);          
} else {
System.out.println("Empty ");
pi = runtimeService.startProcessInstanceById(processDefinitionId);
}
response = new ResponseEntity<>(new BaseResponse<>(Constants.OK, pi), HttpStatus.OK);
if(pi.isEnded()) {      
HistoricProcessInstance hpi = historyService.createHistoricProcessInstanceQuery().includeProcessVariables().
processInstanceId(pi.getId()).singleResult();
variables = hpi.getProcessVariables();
System.out.println("Variables " + variables);
if(variables.get("result").equals("OK")) {
MongoOperations.writeLogSuccess(pi, pi.getProcessDefinitionName());
} else {
MongoOperations.writeLogFailure(hpi.getDurationInMillis(), hpi.getStartTime(), pi.getProcessDefinitionName());
}
}
else {
variables =  runtimeService.getVariables(pi.getId());
}
} catch (FlowableObjectNotFoundException e) {
String pdn = repositoryService.getProcessDefinition(processDefinitionId).getName();
MongoOperations.writeLogException(time, pdn);
response = new ResponseEntity<>(new BaseResponse<>(e.getMessage(), null), HttpStatus.NOT_FOUND);
} catch (FlowableException e) { 
String pdn = repositoryService.getProcessDefinition(processDefinitionId).getName();
MongoOperations.writeLogException(time, pdn);
response = new ResponseEntity<>(new BaseResponse<>(e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR); // other exception are managed here
} catch (Exception e) {
String pdn = repositoryService.getProcessDefinition(processDefinitionId).getName();
MongoOperations.writeLogException(time, pdn);
response = new ResponseEntity<>(new BaseResponse<>(e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR); // other exception are managed here
}
return response;
}

该方法本身的逻辑是正确的,我能够正确执行所有操作。引发或未引发异常时都会发生此错误。我可以从哪里开始检查错误?
从堆栈跟踪来看,这似乎是一个JsonWriter错误,唯一完成的"Json 操作"可能是从请求正文中检索params时,但知道我该如何修复它?我什至无法理解此错误背后的原因...

编辑

所以我知道此错误的可能原因是 Gson 不能很好地与对象配合使用。对吗?在这种情况下,我该如何解决这个问题,因为我的方法只需要一个Map<String,Object>

编辑 2

好的,我已经找到了一个解决方法,但我不喜欢它,但现在是我所拥有的一切,是这样的:

@PostMapping(path = PathConstants.START_ACTION)
public ResponseEntity<BaseResponse<ProcessInstance>> start(@PathVariable String processDefinitionId,
@RequestBody(required = false) Map<?,?> params) throws FileNotFoundException {
LOGGER.info("starting process definition with id {}", processDefinitionId);
Map<String, Object> variables = (Map<String, Object>) params;

然后我将变量传递给我的方法。无论如何,抛出一个(轻微)新错误:

2019-04-16 17:08:02.638 ERROR 9524 --- [nio-9090-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.StackOverflowError] with root cause
java.lang.StackOverflowError: null
at org.apache.coyote.Response.isCommitted(Response.java:255) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.coyote.http11.Http11OutputBuffer.doWrite(Http11OutputBuffer.java:179) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.coyote.Response.doWrite(Response.java:599) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:328) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.catalina.connector.OutputBuffer.appendByteArray(OutputBuffer.java:728) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.catalina.connector.OutputBuffer.append(OutputBuffer.java:657) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:368) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:346) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:96) ~[tomcat-embed-core-9.0.14.jar:9.0.14]
at java.base/sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:233) ~[na:na]
at java.base/sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:303) ~[na:na]
at java.base/sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:281) ~[na:na]
at java.base/sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125) ~[na:na]
at java.base/sun.nio.cs.StreamEncoder.write(StreamEncoder.java:135) ~[na:na]
at java.base/java.io.OutputStreamWriter.write(OutputStreamWriter.java:229) ~[na:na]
at com.google.gson.stream.JsonWriter.string(JsonWriter.java:591) ~[gson-2.8.5.jar:na]
at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:402) ~[gson-2.8.5.jar:na]
at com.google.gson.stream.JsonWriter.value(JsonWriter.java:417) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.TypeAdapters$16.write(TypeAdapters.java:406) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.TypeAdapters$16.write(TypeAdapters.java:390) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) ~[gson-2.8.5.jar:na]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) ~[gson-2.8.5.jar:na]

您出现这些异常的原因很可能是您尝试序列化ProcessInstance。我强烈建议不要这样做并创建您自己的表示,该表示将仅包含您需要的信息。

Gson 和 Jackson 使用反射来执行序列化。尽管ProcessInstance接口具有简单的getter,但实现更加细致入微,并且可以对自身进行循环引用(这就是您看到stackoverflow的原因)。

相关内容

  • 没有找到相关文章

最新更新