@ResponseBody
@RequestMapping(value = "order/pickupinfo", method = RequestMethod.POST, produces = {
"application/json;charset=UTF-8" })
public ResReslt orderList(String req, HttpServletResponse response) {
saveLog(。。。)//保存日志方法
return tt;
}
如果使用上面的方面编写编写输出日志的话,每个方法中都需要写saveLog(。。。),这样搞维护起来比较麻烦,也有可能会有遗漏的地方没有输出日志。
下面我们通过实现import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
这个接口的方式输出日志:
package com.cloud.tools.aop;
import org.slf4j.Logger;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import com.cloud.api.pojo.ResPageReslt;
import com.cloud.api.pojo.ResReslt;
import com.cloud.api.util.ObjToJson;
import com.cloud.log.ReqAndResLog;
@ControllerAdvice
public class ResponseBodyAdviceImp implements ResponseBodyAdvice{
Logger jsonLog = org.slf4j.LoggerFactory.getLogger(ResponseBodyAdviceImp.class);
@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) {
if(null !=body){
String flag = body.toString();
ReqAndResLog reqAndResLog = new ReqAndResLog();
ReqAndResLog temp = reqAndResLog.getInfo();
if(temp !=null){
//已经存在日志对象时,进行比较
if(flag.toString().equals(temp.getRes())){
//移除日志对象
reqAndResLog.remove();
StringBuffer res = new StringBuffer(60);
res.append(returnType.getMethod().getName()).append(";");
if(body instanceof ResReslt){
res.append(ObjToJson.ResResltToStr((ResReslt)body));
}else if(body instanceof ResPageReslt){
res.append(ObjToJson.ResPageResltToStr((ResPageReslt)body));
}
jsonLog.debug(res.toString());
}
}else{
//不存在日志对象,加入
reqAndResLog.setRes(flag);
reqAndResLog.setInfo(reqAndResLog);
}
}
return body;
}
}
package com.cloud.log;
public class ReqAndResLog {
public static final ThreadLocal<ReqAndResLog> REQ_AND_RES_LOG = new ThreadLocal<ReqAndResLog>();
//请求参数
private String req;
private String res;
public String getRes() {
return res;
}
public void setRes(String res) {
this.res = res;
}
public String getReq() {
return req;
}
public void setReq(String req) {
this.req = req;
}
public void setInfo(ReqAndResLog reqAndResLog){
REQ_AND_RES_LOG.set(reqAndResLog);
}
public ReqAndResLog getInfo(){
return REQ_AND_RES_LOG.get();
}
public void remove(){
REQ_AND_RES_LOG.remove();
}
}
ReqAndResLog:作用是防止同样的日志输出两边;
2.配置文件
<bean id="ResponseBodyAdviceImp" class="com.cloud.tools.aop.ResponseBodyAdviceImp" />
相关推荐
Spring MVC下彻底解决@ResponseBody转义html特殊字符问题,解决包括尖角号(),单引号('),双引号("),按位与(&),反斜杠(\)。
-- @ResponseBody() spring-mvc 3 xml 配置--> <!--处理 @ResponseBody 中文乱码问题 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> ...
主要介绍了关于@ResponseBody 默认输出的误区的解答,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
SpringMVC使用@ResponseBody.rar
主要介绍了@ResponseBody 和 @RequestBody 注解的区别的相关资料,需要的朋友可以参考下
springmvc进行Jason数据封装转化时可以即使用也是@responsebody对数据进行封装的时候需要的工具类
主要介绍了Spring MVC打印@RequestBody、@Response日志的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
springmvc+国际化i18N+springmvc验证+jetbrick-template使用+@responsebody+谷歌guava: 1)围绕springmvc做的国际化 2)围绕springmvc做的验证 3)使用的jetbrick-template模板引擎 ……
主要介绍了Springmvc 4.x利用@ResponseBody返回Json数据的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
1)spring MVC 中@ResponseBody需要的所有JAR包 2)性能还不错的模板引擎jetbrick-template-2.x 2.x的所需的所有jar包 3)日志jar包:slf4j和logback 所有 4)阿里 druid 连接池jar包 5)mysql数据库链接驱动jar包 6...
主要介绍了spring boot @ResponseBody转换JSON 时 Date 类型处理方法,主要给大家介绍Jackson和FastJson两种方式,每一种方法给大家介绍的都非常详细,需要的朋友可以参考下
NULL 博文链接:https://snowolf.iteye.com/blog/1628861
下面小编就为大家分享一篇spring+mybatis 通过@ResponseBody返回结果中文乱码的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
NULL 博文链接:https://nethub2.iteye.com/blog/2329387
具体包括三个jar: jackson-databind-2.8.2.jar jackson-core-2.8.2.jar jackson-annotations-2.8.2.jar
NULL 博文链接:https://bijian1013.iteye.com/blog/2310236
NULL 博文链接:https://bijian1013.iteye.com/blog/2310240
主要介绍了SpringMVC中解决@ResponseBody注解返回中文乱码问题, 小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本篇文章主要介绍了springMvc注解之@ResponseBody和@RequestBody详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧