如何在Spring启动时通过Rest Template请求解析GZIP压缩头?

时间:2017-04-18 09:48:43

标签: java spring spring-boot gzip resttemplate

我试图通过REST模板发送gzip压缩请求。

我的请求格式: -

url = apiUrl + mymethod + "?api_key=" + apiKey;
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            headers.add("api_key", apiKey);
            headers.add("Content-Type", "application/json");
            headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
            headers.add("Accept-Encoding", "x-gzip");

            HttpEntity<String> entity = new HttpEntity<String>("parameters",
                    headers);
            ResponseEntity<GenericResponseDto> response = null;

            try {
                response = restTemplate.exchange(url, HttpMethod.GET, entity,
                        GenericResponseDto.class);
            } catch (Exception e) {
                throw new CustomException(e, aPIAuditTrail);
            }
            log.info("Response :"+response);

发送此请求时会收到如下响应消息: - "Response(code=401, message=gzip not passed as header parameters)"

如果我将请求标题更改为

headers.add("Accept-Encoding", "gzip");

我得到例外: -

org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens
 at [Source: java.io.PushbackInputStream@1eb7b683; line: 1, column: 2]; 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
 at [Source: java.io.PushbackInputStream@1eb7b683; line: 1, column: 2]
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:229)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:213)
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:95)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:835)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:819)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:599)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:475)
    at com.finateltech.api.newbitla.service.AvailabilitiesService.onDemandsearch(AvailabilitiesService.java:222)
    at com.finateltech.api.newbitla.service.AvailabilitiesService.search(AvailabilitiesService.java:182)
    at com.finateltech.api.newbitla.service.AvailabilitiesService.process(AvailabilitiesService.java:386)
    at com.finateltech.api.bitla.processor.BitlaRequestProcessor.process(BitlaRequestProcessor.java:70)
    at com.finateltech.api.bitla.processor.BitlaRequestListener.onMessage(BitlaRequestListener.java:39)
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:757)
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:680)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$001(SimpleMessageListenerContainer.java:93)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$1.invokeListener(SimpleMessageListenerContainer.java:183)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.invokeListener(SimpleMessageListenerContainer.java:1358)
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:661)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:1102)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:1086)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1100(SimpleMessageListenerContainer.java:93)
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1203)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens
 at [Source: java.io.PushbackInputStream@1eb7b683; line: 1, column: 2]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1581)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:533)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._throwInvalidSpace(ParserMinimalBase.java:484)
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._skipWSOrEnd(UTF8StreamJsonParser.java:2875)
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:692)
    at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3776)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3721)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2810)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:226)
    ... 23 more

还尝试添加以下propeties application.prperties文件

server.compression.enabled=true
server.compression.mime-types=application/json,application/xml,application/javascript,text/html,text/xml,text/plain

没有改变同样的错误信息,如

"Response(code=401, message=gzip not passed as header parameters)"

我的Spring启动版本是1.3.5.RELEASE。

请帮我讲解如何在spring boot中发送压缩gzip请求?

0 个答案:

没有答案