【问题标题】:Springboot throwing giving Jackson exceptionsSpringboot抛出给杰克逊异常
【发布时间】:2019-05-19 23:08:38
【问题描述】:

在测试期间,我遇到了这个问题。我已经发布了一个带有控制器类和模型输入的 rest 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"

}

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

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

1. RCE 是否容易受到攻击?如何在 Spring-boot 中解决这个问题? 2. 如何抑制/覆盖异常消息,使客户端永远不知道下面使用了哪些库?

【问题讨论】:

  • 看看@controlleradvice

标签: spring-boot jackson jackson-databind


【解决方案1】:

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

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

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

【讨论】:

  • 您好 Aritra - 快速响应。我不想将数组作为 request 传递。它是作为无效输入 [{"a":1,"b":2}] 的回归的一部分完成的。
  • 我的问题是围绕响应,1.杰克逊版本是否容易受到RCE的攻击?如何在 Spring-boot 中解决这个问题? 2.我如何抑制/覆盖异常消息,以便客户端永远不知道下面使用了哪些库?
【解决方案2】:

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

@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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    相关资源
    最近更新 更多