StackOverflowError:com.google.gson.stream.JsonWriter.value

时间:2019-04-16 14:50:54

标签: json http-post catalina catalina.out flowable

参考this other question,似乎我不了解的整个方法都存在问题。实际上,现在我不再有问题中指定的错误,但是我有一个新错误:

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无法与Object配合良好。这样对吗?在这种情况下,由于我的方法仅使用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]

1 个答案:

答案 0 :(得分:1)

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

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