Home > Back-end >  Get SpringCloud gateway response response values of the code
Get SpringCloud gateway response response values of the code

Time:03-12

In the console print gibberish, coding format changed in a time, as long as byte String and then turns gibberish, returns to the potman shows get no response, bosses look

 package com. 

The import com. Alibaba. Fastjson. JSONObject;
The import com.google.com mon. Base. Charsets;
The import lombok. Extern. Slf4j. Slf4j;
The import org.apache.com mons. Lang. StringUtils;
The import org. Reactivestreams. Publisher;
The import org. Slf4j. LoggerFactory;
The import org. Springframework. Cloud. Gateway. Filter. GatewayFilter;
The import org. Springframework. Cloud. Gateway. Filter. GatewayFilterChain;
The import org. Springframework. Cloud. Gateway. Filter. GlobalFilter;
The import org. Springframework. Core. Ordered;
The import org. Springframework. Core. IO. Buffer. The DataBuffer;
The import org. Springframework. Core. IO. Buffer. DataBufferFactory;
The import org. Springframework. Core. IO. Buffer. DataBufferUtils;
The import org. Springframework. Core. IO. Buffer. DefaultDataBufferFactory;
The import org. Springframework. HTTP. HttpStatus;
The import org. Springframework. HTTP. Server. Reactive. ServerHttpRequest;
The import org. Springframework. HTTP. Server. Reactive. ServerHttpResponse;
The import org. Springframework. HTTP. Server. Reactive. ServerHttpResponseDecorator;
The import org.springframework.stereotype.Com ponent;
Import org. Springframework. Web. Server. ServerWebExchange;
The import reactor. Core. Publisher. Flux;
The import reactor. Core. Publisher. Mono;

Import the Java. IO. UnsupportedEncodingException;
Import the Java. Nio. Charset. Charset;
Import the Java. Nio. Charset. StandardCharsets;
Import the Java. Util. Concurrent. Atomic. AtomicReference;
Import the Java. Util. Logging. The Logger;

The import static com.sun.org.glassfish.external.statistics.impl.StatisticImpl.START_TIME;
The import static org. Springframework. Cloud. Gateway. Support. ServerWebExchangeUtils. ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR;

@ Slf4j
@ Component
Public class DataRespEncryptFilter implements GlobalFilter, Ordered {

@ Override
Public Mono The filter (ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request=exchange. GetRequest ();
//String IP=IpUtil. GetRemoteHost (request);
//execution after the completion of the call time buried point
Exchange. The getAttributes (). The put (START_TIME, System. CurrentTimeMillis ());
//original response
ServerHttpResponse originalResponse=exchange. The method getResponse ();
DataBufferFactory bufferFactory=originalResponse. BufferFactory ();
//initialize a default responseBody
AtomicReference ResponseBody=new AtomicReference<> (" no - responseBody ");
//repackaging the response class
ServerHttpResponseDecorator decoratedResponse=new ServerHttpResponseDecorator (originalResponse) {
@ Override
Public Mono WriteWith (Publisher<? Extends DataBuffer> Body) {
Flux<? Extends DataBuffer> FluxBody=Flux. The from (body);
Return super. WriteWith (fluxBody. Buffer (). The map (dataBuffers - & gt; {
DataBufferFactory DataBufferFactory=new DefaultDataBufferFactory ();
DataBuffer join=dataBufferFactory. Join (dataBuffers);
Byte [] the content=new byte [join readableByteCount ()];
The join. Read (content);
//release memory
DataBufferUtils. Release (join);
String STR=new String (the content, the Charset. Class.forname (" utf-8 "));
OriginalResponse. GetHeaders (.) setContentLength (STR. GetBytes (.) length);
System. The out. Println (STR);
Return bufferFactory. Wrap (STR) getBytes ());
}));
}
};

Return chain. The filter (exchange. Mutate (). The response (decoratedResponse). The build ())
Then (Mono. FromRunnable (() - & gt; {
//print the response log
LogResponse (exchange, responseBody. The get ());

Long startTime=exchange. The getAttribute (START_TIME);
If (startTime!=null) {
Long executeTime=(System. CurrentTimeMillis () - startTime);
//influxDB buried point
//metricService. PointRequestLatency (IP, request getURI () getPath (), executeTime);
}
}));
}
/* *
* print the response message
*
* @ param exchange
*/
Public void logResponse (ServerWebExchange exchange, String response) {
ServerHttpRequest request=exchange. GetRequest ();
Response message log. The info (" URL: {}, Method: {}, headers: {}, the response: {} ", request. GetURI () getPath (), request. GetMethod (), exchange. The Method getResponse () getHeaders (), response);
}

@ Override
Public int getOrder () {
The return - 10;
}


}



The console print:


Postman interface:


The above utf-8 tried US_ASCII, ISO_8859_1, UTF_16BE, UTF_16LE, UTF_16
But will return to have a problem: direct access to the content of direct



Leaders help to see why the code

CodePudding user response:

Junction post, do a gzip encoding decoding on ok


nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related