简体   繁体   English

在 restTemplate 之后设置弹簧过滤器

[英]set spring filter after restTemplate

I have a few endpoints without http error codes.我有几个没有 http 错误代码的端点。 What I receive is a 200OK with a message error on the response.我收到的是一个 200OK 的响应消息错误。 What I want to do is, after every single restTemplate GET, set some custom logic to check if the response is an error or not.我想做的是,在每次 restTemplate GET 之后,设置一些自定义逻辑来检查响应是否为错误。 What I have tried so far is implementing a filter class which is working with my endpoints到目前为止我尝试过的是实现一个与我的端点一起工作的过滤器类

@Component
class ErrorFilter implements Filter {

   @Override
   void doFilter(ServletRequest servletRequest, ServletResponse 
servletResponse, FilterChain filterChain) {
       System.out.println("Filter called")
       filterChain.doFilter(servletRequest, servletResponse)
   }
}

when I implement an endpoint, this is working nice and I can add logic after every request.当我实现一个端点时,这很好用,我可以在每个请求之后添加逻辑。 but it is not working when I call an external endpoint using restTemplate like this:但是当我像这样使用 restTemplate 调用外部端点时它不起作用:

restTemplatie.exchange(url, HttpMethod.GET, request, String.class)

So, what I want to do is, after the above code, implement a logic similar to this:所以,我想要做的是,在上面的代码之后,实现一个类似这样的逻辑:

if(response.body == customErrorMessage){
    thrown Exception
}

And I want this working in every single restTemplate call.我希望这在每个 restTemplate 调用中都能正常工作。 Can I achieve this somehow with spring boot?我可以用弹簧靴以某种方式实现这一目标吗?

maybe with something similar to a restTemplate interceptor but the iterceptor is working BEFORE the call and I need it after (because I want to access to the response)也许使用类似于 restTemplate 拦截器的东西,但该拦截器在调用之前正在工作,之后我需要它(因为我想访问响应)

You can use Interceptor for handling response.您可以使用 Interceptor 来处理响应。 in your case you can do the following:在您的情况下,您可以执行以下操作:

public class ResponseInterceptor implements ClientHttpRequestInterceptor {

final static Logger log = LoggerFactory.getLogger(LoggingRequestInterceptor.class);

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
    ClientHttpResponse response = execution.execute(request, body);
    //add your code
    if(response.getBody() == customErrorMessage){ // TODO 
      thrown new Exception("customErrorMessage");
    }
    return response;
}

To get the response body you need to read it as:要获得响应正文,您需要将其阅读为:

StringBuilder inputStringBuilder = new StringBuilder();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getBody(), "UTF-8"));
        String line = bufferedReader.readLine();
        while (line != null) {
            inputStringBuilder.append(line);
            inputStringBuilder.append('\n');
            line = bufferedReader.readLine();
        }

and then check inputStringBuilder.toString() (or you can check by line )然后检查inputStringBuilder.toString() (或者您可以按line检查)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM