0

参考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]
4

1 回答 1

1

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

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

于 2019-04-17T19:46:48.163 回答