java.lang.StackOverflowError 使用 Gson 将 BindingResult 转换为 JSON

java.lang.StackOverflowError while converting BindingResult to JSON using Gson

环境:
Spring 引导- 1.2.3
问题

BindingResult 作为控制器方法的 @Valid 参数的下一个参数添加时,如下所示,继续获取 java.lang.WhosebugError

@RequestMapping(value = "/employees", method = RequestMethod.POST, consumes = "application/json")
public void createEmployee(HttpServletRequest request, @Valid @RequestBody Employee employee, BindingResult result){
    logger.debug("Creating Employee [" + employee.getForename() + " " + employee.getSurname() + "]");
}

如果删除 BindingResult 方法参数,它工作正常。
更新开始
发现问题,使用以下代码将 BindingResult 实例转换为 JSON 时发生 WhosebugError

//log all method arguments   
com.google.gson.Gson gson = new com.google.gson.Gson();
String json = gson.toJson(bindingResultArgFromControllerMethod);

框架代码使用 Gson 将方法参数转换为 JSON 以进行日志记录。

有办法avoid/handle这个异常吗?
更新结束

相关堆栈跟踪:

    Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler processing failed; nested exception is java.lang.WhosebugError] with root cause

java.lang.WhosebugError: null
//Repeatattive block start 
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
//Repeatattive block end 
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:155)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:97)
at com.google.gson.Gson.getAdapter(Gson.java:407)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:136)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.access0(ReflectiveTypeAdapterFactory.java:49)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.<init>(ReflectiveTypeAdapterFactory.java:106)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:105)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:161)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:97)
at com.google.gson.Gson.getAdapter(Gson.java:407)

根据 BindingResult 的 API 文档,这只是 holder 的数据用户进入浏览器。此外,BindingResult 的实现可以包含对其他辅助对象的各种引用。根据堆栈跟踪,有一个关于 BindingResult.

当前实现的循环引用

我认为您想对用户输入的数据进行 jsonify。然后你需要做的就是将 BindingResulttarget 进行 jsonify:

com.google.gson.Gson gson = new com.google.gson.Gson();
String json = gson.toJson(bindingResultArgFromControllerMethod.getTarget());

这个target是,根据method docs包装的目标对象,可能是一个bean,一个有public字段的对象,一个Map - 取决于具体的绑定策略.

希望这对您有所帮助。