首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >已解决[org.springframework.http.converter.HttpMessageNotReadableException: JSON解析错误:非法字符((CTRL-CHAR,代码31))

已解决[org.springframework.http.converter.HttpMessageNotReadableException: JSON解析错误:非法字符((CTRL-CHAR,代码31))
EN

Stack Overflow用户
提问于 2021-01-06 21:46:16
回答 1查看 1.2K关注 0票数 0

这是我的应用程序日志中的完整错误消息

代码语言:javascript
运行
复制
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens; nested exception is com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL -CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens

我正在发送一个POST请求到我的web应用程序使用postman与以下标题的输入是一个带有json的gzip。在邮递员上,它返回400个错误请求

代码语言:javascript
运行
复制
 Content-Type: application/json
 Content-Encoding: gzip

这是我的POST方法。我有一个自定义的gzip拦截器类。

代码语言:javascript
运行
复制
@RequestMapping(
   method = RequestMethod.POST,
   value = "/post"
)
public WekaPredictResp prediction(HttpServletRequest request, HttpServletResponse response, @RequestBody CustomRequest gzipInputs) throws Exception {
   postMethod(gzipInputs);
}
代码语言:javascript
运行
复制
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.HttpEntity;
import org.springframework.web.client.RestTemplate;

public static CustomResponse postMethod(CustomRequest gzipInputs) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<Map<String, Object>> entity = new HttpEntity<>(gzipInputs.getInputs(), headers);

    RestTemplate gzipRestTemplate = new RestTemplateBuilder()
       .requestFactory(HttpComponentsClientHttpRequestFactory.class)
       .additionalInterceptors(new GzipInterceptor())
       .build();

    ResponseEntity<Map> responseEntity = gzipRestTemplate.postForEntity(externalUrl, entity, Map.class);

    Map<String, Object> outputs = (Map<String, Object>) responseEntity.getBody();

    CustomResponse response = new CustomResponse();
    response.setOutputs(outputs);

    return outputs;

}
代码语言:javascript
运行
复制
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

public class GzipHttpRequestInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
        HttpHeaders httpHeaders = httpRequest.getHeaders();
        httpHeaders.add(HttpHeaders.CONTENT_ENCODING, "gzip");
        httpHeaders.add(HttpHeaders.ACCEPT_ENCODING, "gzip");
        httpHeaders.set(HttpHeaders.CONTENT_TYPE, "gzip"); // Override for compatibility
        return clientHttpRequestExecution.execute(httpRequest, compress(bytes));
    }

}
EN

回答 1

Stack Overflow用户

发布于 2021-01-07 15:27:50

通过包含一个跟踪gzip输入并解压缩的过滤器,可以找到示例解决方案here

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65596954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档