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;
}
|
所以大家可以根据自己的需要,定义结果的入参和出参结果做一些特殊处理…..
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://www.jianshu.com/p/5d3f701d4fc3
相关文章
猜你喜欢
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-26 81
-
2025-06-04 66
-
2025-05-29 89
-
2025-06-04 97
-
2025-05-27 39
热门评论

