Remember the impact of a FEIGN transfer head

Understanding of HTTP request header Accept-Encoding
Accept-Encoding indicates whether the Http response is compressed or not. When a general browser accesses a web page, it adds
Accept-Encoding: gzip, deflate to the request header by default, indicating that the content of the request is expected to be compressed. Compression, the purpose of compression is to reduce network traffic,
but this is only a protocol, and it can only be required rather than mandatory. If the server does not support compression or does not enable compression, it will not work.
If the server also supports compression or enables compression, The Content-Encoding: gzip header will be added to the response header,

Cause
Feign needs to call and pass the token, so the RequestInterceptor is customized to pass the header information,

Status
Because the project is configured to enable GZIP compression

server:
  compression:
    enabled: true

When calling through feign, the following error is reported:

Error while extracting response for type [class com.central.common.model.SysAdmin] and content type [application/json;charset=UTF-8]; nested exception is 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
 at [Source: (PushbackInputStream); line: 1, column: 2]

The general meaning is that you can see that feign reported an error when json was parsed after calling it.
Analysis
and the source code called by feign
insert image description here
There are two implementations. Here we look at the feign.SynchronousMethodHandler#invoke method. SynchronousMethodHandler supports remote calls to follow up
insert image description here
the executeAndDecode method Inside
insert image description here
Here we directly look at the content in the response. At this time, the remote call has been completed. See that our request header has the Accept-Encoding with the Accept-Encoding header: [gzip, deflate, br
]

insert image description here
Look at the function of these two heads,

content-encoding: refers to which compression method the webpage uses to transmit data to you, and
accept-encoding: indicates that you tell the server when you send the request that I can decompress the data in these formats.
The relationship between the two is that the other party's webpage will decide what format (content-encoding) to use and send it to you according to the accept-encoding you send.

The reason is clear. In the transfer of the header information, I passed all the header information and told the server that I need to compress it, which caused feign to fail to parse it.

Solution
1: Pass only what you need, or exclude the accept-encoding header

if(name != null && name.equalsIgnoreCase("authorization")) {
    
    
    requestTemplate.header(name, values);
}
//if(name != null && !name.equalsIgnoreCase("accept-encoding")) {
    
    
//    requestTemplate.header(name, values);
//}

Method 2: Directly change the yml configuration and tell feign that we need to decompress

feign:
  compression:
    request:
      enabled: true
      # 配置压缩数据大小的下限
      min-request-size: 8192
    response:
      enabled: true

Guess you like

Origin blog.csdn.net/qq_38747892/article/details/130114519