簡體   English   中英

已解決 [org.springframework.http.converter.HttpMessageNotReadableException: JSON 解析錯誤:非法字符((CTRL-CHAR,代碼 31))

[英]Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Illegal character ((CTRL-CHAR, code 31))

這是我的應用日志中的完整錯誤消息

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

我正在使用 postman 和以下 header 向我的 web 應用程序發送 POST 請求在 postman 上,它返回 400 Bad Request

 Content-Type: application/json
 Content-Encoding: gzip

這就是我上傳 gzip 文件的方式

這是我的 POST 方法。 我有一個自定義 gzip 攔截器 class。

@RequestMapping(
   method = RequestMethod.POST,
   value = "/post"
)
public WekaPredictResp prediction(HttpServletRequest request, HttpServletResponse response, @RequestBody CustomRequest gzipInputs) throws Exception {
   postMethod(gzipInputs);
}
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;

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

}

通過包含一個過濾器來跟蹤 gzip 輸入並對其進行解壓縮,可以在此處找到示例解決方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM