SpringMVC的Body参数拦截的问题

时间:2022-08-30 19:13:21

springmvc对出参和入参有非常友好的拓展支持,方便你对数据的输入和输出有更大的执行权,我们如何通过springmvc定义的结果做一系列处理呢?

入参

requestbodyadvice : 针对所有以@requestbody的参数做处理

参考案例 : jsonviewrequestbodyadvice

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class jsonviewrequestbodyadvice extends requestbodyadviceadapter {
 
  /**
   * 这里是一个前置拦截匹配操作,其实就是告诉你满足为true的才会执行下面的beforebodyread方法,这里可以定义自己业务相关的拦截匹配
   * @param methodparameter
   * @param targettype
   * @param convertertype
   * @return
   */
  @override
  public boolean supports(methodparameter methodparameter, type targettype,
      class<? extends httpmessageconverter<?>> convertertype) {
        
    return (abstractjackson2httpmessageconverter.class.isassignablefrom(convertertype) &&
        methodparameter.getparameterannotation(jsonview.class) != null);
  }
 
    // 这里就是具体的前置操作了... 下面的例子就是查找这个入参方法是否有@jsonview修饰
  @override
  public httpinputmessage beforebodyread(httpinputmessage inputmessage, methodparameter methodparameter,
      type targettype, class<? extends httpmessageconverter<?>> selectedconvertertype) throws ioexception {
 
    jsonview annotation = methodparameter.getparameterannotation(jsonview.class);
    class<?>[] classes = annotation.value();
    if (classes.length != 1) {
      throw new illegalargumentexception(
          "@jsonview only supported for request body advice with exactly 1 class argument: " + methodparameter);
    }
    return new mappingjacksoninputmessage(inputmessage.getbody(), inputmessage.getheaders(), classes[0]);
  }
}

出参

responsebodyadvice: 针对所有以@responsebody的参数做处理

参考案例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@controlleradvice
public class logresponsebodyadvice implements responsebodyadvice {
 
  /**
   *
   * @param returntype
   * @param convertertype
   * @return
   */
  @override
  public boolean supports(methodparameter returntype, class convertertype) {
    return true;
  }
 
  @override
  public object beforebodywrite(object body, methodparameter returntype, mediatype selectedcontenttype, class selectedconvertertype, serverhttprequest request, serverhttpresponse response) {
    // 做任何事情 body 就是返回的结果对象,没有处理之前
    return body;
  }
}

注意事项

自定义的处理对象类上必须得加上@controlleradvice注解!

为什么?

源码中requestmappinghandleradapter类在执行initcontrolleradvicecache()做初始化的时候会执行一个

?
1
2
list<controlleradvicebean> beans = controlleradvicebean.findannotatedbeans(getapplicationcontext());
annotationawareordercomparator.sort(beans);

而controlleradvicebean.findannotatedbeans方法会查找类上有controlleradvice注解的类才会加入到处理当中..

?
1
2
3
4
5
6
7
8
9
public static list<controlleradvicebean> findannotatedbeans(applicationcontext applicationcontext) {
    list<controlleradvicebean> beans = new arraylist<controlleradvicebean>();
    for (string name : beanfactoryutils.beannamesfortypeincludingancestors(applicationcontext, object.class)) {
      if (applicationcontext.findannotationonbean(name, controlleradvice.class) != null) {
        beans.add(new controlleradvicebean(name, applicationcontext));
      }
    }
    return beans;
  }

所以大家可以根据自己的需要,定义结果的入参和出参结果做一些特殊处理.....

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.jianshu.com/p/5d3f701d4fc3