Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jackson在处理字符串时存在长度限制,是否可以添加配置项,供业务控制其所能解析的字符传长度? #4239

Open
yanghao605 opened this issue Feb 26, 2024 · 3 comments

Comments

@yanghao605
Copy link
Contributor

当前遇到一个问题,是服务端返回的响应消息中,字符串长度过长,导致jackson解析失败

Caused by: com.fasterxml.jackson.core.exc.StreamConstraintsException: String length (20051112) exceeds the maximum length (20000000)
	at com.fasterxml...core.StreamReadConstraints.validateStringLength:324
	at com.fasterxml...util.ReadConstrainedTextBuffer.validateStringLength:27
	at com.fasterxml...util.TextBuffer.finishCurrentSegment:939
	at com.fasterxml...json.UTF8StreamJsonParser._finishString2:2584
	at com.fasterxml...json.UTF8StreamJsonParser._finishAndReturnString:2560
	at com.fasterxml...json.UTF8StreamJsonParser.getText:335
	at com.fasterxml...jsonorg.JSONObjectDeserializer.deserialize:51
	at com.fasterxml...jsonorg.JSONObjectDeserializer.deserialize:48
	at com.fasterxml...jsonorg.JSONObjectDeserializer.deserialize:14
	at com.fasterxml...deser.DefaultDeserializationContext.readRootValue:323
	at com.fasterxml...databind.ObjectMapper._readMapAndClose:4825mas
	at com.fasterxml...databind.ObjectMapper.readValue:3825
	at org.apache...produce.ProduceJsonProcessor.doDecodeResponse:64
	at org.apache...produce.ProduceProcessor.decodeResponse:76
	at org.apache...http.DefaultHttpClientFilter.extractResponse:105
	at org.apache...http.DefaultHttpClientFilter.afterReceiveResponse:139
	at org.apache...http.RestClientInvocation.lambda$processResponseBody$7:251

20000000是jackson提供的默认值;从chassis源码中来看,并没有提供配置该长度的配置项,是否可以加一个

@liubao68
Copy link
Contributor

老版本可以通过RestObjectMapperFactory处理。 配置项应该可以在新版本增加。

@yanghao605
Copy link
Contributor Author

OK

@liubao68
Copy link
Contributor

liubao68 commented Mar 7, 2024

如果需要,可以按照下面的方式定制:

  @Test
  public void test() throws Exception {
    RestObjectMapperFactory.getRestObjectMapper().getFactory()
        .setStreamReadConstraints(StreamReadConstraints.builder().maxStringLength(20).build());
    RestObjectMapperFactory.getRestObjectMapper().readValue("\"dddddddddddddddddddddddddddddddddd\"", String.class);
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants