首页>代码>基于SpringBoot+mybatis+Jsp的日记记录系统代码分享>/diary/src/main/java/com/rabbit/diary/bean/core/CommonResponseDataAdvice.java
package com.rabbit.diary.bean.core;

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * 统一封账响应结果
 * @author Administrator
 *
 */
@RestControllerAdvice
public class CommonResponseDataAdvice implements ResponseBodyAdvice<Object> {


    @Override
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
        // 标注了@RestController,且类及方法上都没有标注@IgnoreCosmoResult的方法才进行包装
    	return methodParameter.getDeclaringClass().isAnnotationPresent(RestController.class)
                && !methodParameter.getDeclaringClass().isAnnotationPresent(com.rabbit.diary.bean.core.IgnoreCosmoResult.class)
                && !methodParameter.getMethod().isAnnotationPresent(com.rabbit.diary.bean.core.IgnoreCosmoResult.class);
    }

    @Override
    public Object beforeBodyWrite(Object o,
                                  MethodParameter methodParameter,
                                  MediaType mediaType,
                                  Class<? extends HttpMessageConverter<?>> aClass,
                                  ServerHttpRequest serverHttpRequest,
                                  ServerHttpResponse serverHttpResponse) {
    	// 已经包装过的,不再重复包装
        if (o instanceof com.rabbit.diary.bean.core.Result) {
            return o;
        }
        // 改一行代码即可:把Object返回值用Result封装
        return com.rabbit.diary.bean.core.Result.success(o);
    }
}
最近下载更多
hanweinan6  LV13 2024年10月9日
wanglinddad  LV55 2024年3月24日
zolscy  LV24 2024年3月21日
rain112  LV31 2024年2月6日
全栈小陈  LV4 2024年1月21日
酷酷的轲  LV10 2023年6月29日
微信网友_6499449700175872  LV4 2023年5月31日
980363152  LV8 2023年5月15日
最代码官方  LV168 2023年5月14日
最近浏览更多
潜心小白来到  LV3 6月10日
梁小贤  LV1 5月22日
PLVAE_514  LV2 3月10日
没有啊很开心啊  LV4 3月3日
denliv_hui  LV13 2月28日
暂无贡献等级
f22m1a2b2  LV17 2月5日
srmess  LV4 2024年12月30日
bluerstar  LV1 2024年10月23日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友