Springboot 抛出 Jackson 异常

Springboot throwing giving Jackson exceptions

在测试期间,我遇到了 issue.I 发布了带有控制器 class 和模型输入的休息 API。 在调用 API 时,使用了数组 [{"a":1,"b":2}] 而不是单个字符串。触发了以下错误:

{

"timestamp": "2018-12-19T12:33:36.729+0000",
"status": 400,
"error": "Bad Request",
"message": "JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token\n at [Source: (PushbackInputStream); line: 3, column: 14] (through reference chain: com.xy.df.model.inputReq[\"req\"])",
"path": "x/y/z"

}

我们没有在应用程序中显式地在 POM 中导入 JACKSON 依赖项。我注意到在父 pom jackson 中使用的版本是:2.9.5

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>

1.Is 它易受 RCE 攻击?如何在 Spring-boot 中解决这个问题? 2. 我如何 supress/override 异常消息,以便客户端永远不知道下面使用了哪些库?

JsonMappingException: out of START_ARRAY token 异常由 Jackson 对象映射器抛出,因为它期待一个 Object {} 而它发现一个 Array [{}] 作为响应。

这可以通过在 geForObject("url",Object[].class) 的参数中将 Object 替换为 Object[] 来解决。 参考文献:

  1. Ref.1
  2. Ref.2
  3. Ref.3

我已经解决了问题。在继续之前,需要了解几个非常有用的注释 - @ExceptionHandler - 此处理程序可帮助您定义要为其捕获异常的错误 class @controller 建议 - 它迎合了一种横切方法。任何 class 提到的控制器建议,它可用于您的微服务下的所有控制器。

@ControllerAdvice
public class ExceptionController {

    @Autowired
    SomeGenericResponse someGenericResponse ; /* data model of common response */

    @ExceptionHandler(value = <My case Jackson Class>.class)
    public ResponseEntity<SomeGenericResponse> CustomException(HttpServletRequest req, HttpServletResponse res,Exception ex) {


        someGenericResponse.setMessage("Your Message");
        someGenericResponse.setStatus("false");

        return new ResponseEntity<SomeGenericResponse> someGenericResponse ,HttpStatus.BAD_REQUEST);
    }
}