SoFunction
Updated on 2025-03-08

zuulGateway operation to modify the return value uniformly through filter

Sometimes when we return content to the client using spring cloud, we often need to add some extra stuff. For example, encryption, adding an additional return value, etc.

Of course, it can be processed in the method, but if there are many methods and need to be processed in a unified manner, it will be inconvenient. At this time, it can be processed in a unified manner through the filter of zuulGateway.

Just look at the code, it's very simple:

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
  * Return value output filter, used here to process return value
  *
  * @Title: ResponseFilter
  * @Description:
  * @author kokJuis
  * @date 9:52:42 AM
  */
public class ResponseFilter extends ZuulFilter
{
 @Override
 public Object run()
 {
  RequestContext context = ();
  try
  {
   // Get the return value content and process it   InputStream stream = ();
   String body = (stream, ("UTF-8"));
   String returnStr = "";
 
 //Your processing logic, encryption, adding new return values, etc... 
   // Rewrite content   (returnStr);
  }
  catch (Exception e)
  {
   ();
  }
  return null;
 }
 @Override
 public boolean shouldFilter()
 {
  RequestContext ctx = ();
  String requestURI = (("requestURI"));
  if (())
  {
 //Return false if URL request that does not need to be processed   return false;
  }
  return true;
 }
 @Override
 public int filterOrder()
 {
  return FilterConstants.SEND_RESPONSE_FILTER_ORDER - 3;
 }
 @Override
 public String filterType()
 {
  return FilterConstants.POST_TYPE;// After the request is processed, the filter will be entered }
}

Then register the filter in @Configuration

 @Bean
 public ResponseFilter responseFilter()
 {
  return new ResponseFilter();
 }

Supplementary knowledge:The unified data format that springCloud service providers should return

I won't say much nonsense, let's just read the code~

package ;
import ;
import ;
import ;
import ;
/**
 * @Author: 
 * @Date: 2019/6/27 8:55
 * @Version 1.0
 * @date 2019-06-27 08:58
 * @author wangzhilei
 */
@Data
public class BaseResult implements Serializable {
 private static final String OK = "OK";
 private static final String NotOK = "notOk" ;
 private static final String SUCCESS = "The operation is successful";
 private String result;
 private Object data;
 private String success;
 private Cursor cursor;
 private List<Error> errors;
 // TODO Return to no argument ok public static BaseResult ok(){
  return createRestult(OK, null, SUCCESS,null,null ) ;
 }
 // TODO returns ok with success public static BaseResult ok(String success){
  return createRestult(OK, null, success, null, null);
 }
 // TODO returns ok with data public static BaseResult ok(Object data){
  return createRestult(OK, data, SUCCESS, null, null);
 }
 // Return data with paging public static BaseResult ok(Object data, Cursor cursor) {
  return createRestult(OK, data, SUCCESS, cursor, null);
 }
 // TODO returns error data public static BaseResult notOk(List<> errors){
  return createRestult(NotOK, null, null, null, errors);
 }
 // TODO construction errors and correct methods public static BaseResult createRestult(String result, Object data,String success, Cursor cursor, List<> errors){
  BaseResult baseResult = new BaseResult();
  (result);
  (data);
  (success);
  (cursor);
  (errors);
  return baseResult;
 }
 @Data
 @AllArgsConstructor
 public static class Cursor {
  private int total;
  private int offset;
  private int limit;
 }
 @Data
 @AllArgsConstructor
 public static class Error{
  private String field;
  private String message;
 }
}

The above article zuulGateway's unified return value through filter is all the content I share with you. I hope it can give you a reference and I hope you support me more.