当前位置: 首页 > news >正文

乐清建站网架公司十大排名

乐清建站,网架公司十大排名,室内设计学校专业,产品软文代写起因 关于使用AOP去实现统一返回接口在之前的博客中我们已经实现了#xff0c;但我突然突发奇想#xff0c;SpringBoot中异常类的统一返回好像是通过RestControllerAdvice 这个注解去完成的#xff0c;那我是否也可以通过这个注解去实现统一返回接口。 正文 这个方法主要…起因 关于使用AOP去实现统一返回接口在之前的博客中我们已经实现了但我突然突发奇想SpringBoot中异常类的统一返回好像是通过RestControllerAdvice 这个注解去完成的那我是否也可以通过这个注解去实现统一返回接口。 正文 这个方法主要是通过ControllerAdvice ResponseBodyAdvice实现统一返回结果。其实本质来说和aop实现是相通的明白一个另一个就非常好理解了。 Result 的代码我就不在这边重复贴了读者可以去我直接用AOP实现的博客中拿 自定义注解 import com.study.project.common.BaseResponse; import com.study.project.common.ResultCode;import java.lang.annotation.*;import static com.study.project.common.ResultCode.SUCCESS;/*** date 2023/2/18*/ Documented Target({ElementType.METHOD, ElementType.TYPE}) Retention(RetentionPolicy.RUNTIME) public interface FunctionResult {String value() default ;//默认code为成功ResultCode code() default SUCCESS; } 自定义一个响应拦截 import com.study.project.annotation.FunctionResult; 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.RestControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;import java.lang.annotation.Annotation; import java.lang.reflect.Method;/*** author Chengming.Zhang* date 2023/2/18* ResponseBodyAdvice主要是对加了RestController(也就是ControllerResponseBody)注解的处理器将要返回的值进行增强处理。*其实也就是采用了AOP的思想对返回值进行一次修改。*/ RestControllerAdvice public class FunctionResponseBodyAdvice implements ResponseBodyAdvice {//判断当前类上是否有FunctionResultprotected boolean isFunctionResult(MethodParameter returnType) {/*** getContainingClass() 获取当前类的信息* isAnnotationPresent 判断当前类上是否存在某个注解*/Class? containingClass returnType.getContainingClass();boolean annotationPresent containingClass.isAnnotationPresent(FunctionResult.class);Annotation[] annotations containingClass.getAnnotations();return returnType.getContainingClass().isAnnotationPresent(FunctionResult.class);}Overridepublic boolean supports(MethodParameter returnType, Class converterType) {return isFunctionResult(returnType);}Overridepublic Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {Method method returnType.getMethod();Class? methodReturnType method.getReturnType();//判断是否为void的方法if (methodReturnType.equals(void.class)) {return body;}//判断当前方法是否有FunctionResult注解如果没有则全部按照成功返回如果有则根据具体指定的返回码以及返回内容返回FunctionResult result returnType.getMethod().getAnnotation(FunctionResult.class);if (result null) {return new BaseResponse(ResultCode.SUCCESS, body);}ResultCode code result.code();return new BaseResponse(result.code(), body);} } controller类 import com.study.project.annotation.FunctionResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;/*** date 2023/2/4*/ FunctionResult RestController public class TestController {RequestMapping(/test5)public int test5(){return 1;}RequestMapping(/test6)public void test6(){System.err.println(test6);} } 测试 你是不是以为代码已经结束 其实这个代码是有问题hhhhh当接口的返回类型是String的时候就会提示报错 看控制台的报错信息发现是接口的返回参数转换的时候报错了于是我们根据控制台上的报错信息进行断点排查 首先我们找到控制台中的第一行的报错类StringHttpMessageConverter.java中的addDefaultHeaders方法发这个方法其实是重新了他父类的AbstractHttpMessageConverter的方法 在AbstractHttpMessageConverter中的方法Result的参数是T但是StringHttpMessageConverter在重写的时候将其转为了String因此当ResponseBodyAdvice返回Result格式的时候就会报错所以我们就需要在ResponseBodyAdvice中需要单独处理一下String类型。 完整代码 import cn.hutool.json.JSONUtil; import com.study.project.annotation.FunctionResult; import jdk.nashorn.internal.objects.annotations.Function; 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.bind.annotation.RestControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;import java.lang.annotation.Annotation; import java.lang.reflect.Method;/*** author Chengming.Zhang* date 2023/2/18* ResponseBodyAdvice主要是对加了RestController(也就是ControllerResponseBody)注解的处理器将要返回的值进行增强处理。*其实也就是采用了AOP的思想对返回值进行一次修改。*/ RestControllerAdvice public class FunctionResponseBodyAdvice implements ResponseBodyAdvice {//判断当前类上是否有FunctionResultprotected boolean isFunctionResult(MethodParameter returnType) {/*** getContainingClass() 获取当前类的信息* isAnnotationPresent 判断当前类上是否存在某个注解*/Class? containingClass returnType.getContainingClass();boolean annotationPresent containingClass.isAnnotationPresent(FunctionResult.class);Annotation[] annotations containingClass.getAnnotations();return returnType.getContainingClass().isAnnotationPresent(FunctionResult.class);}Overridepublic boolean supports(MethodParameter returnType, Class converterType) {return isFunctionResult(returnType);}Overridepublic Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {Method method returnType.getMethod();Class? methodReturnType method.getReturnType();if (methodReturnType.equals(void.class)) {return body;}//判断当前方法是否有FunctionResult注解如果没有则全部按照成功返回如果有则根据具体指定的返回码以及返回内容返回FunctionResult result returnType.getMethod().getAnnotation(FunctionResult.class);if (result null) {if (body instanceof String) {return JSONUtil.toJsonStr(new BaseResponse(ResultCode.SUCCESS, body));}return new BaseResponse(ResultCode.SUCCESS, body);}if (body instanceof String) {return JSONUtil.toJsonStr(new BaseResponse(result.code(), body));}return new BaseResponse(result.code(), body);} }
http://www.w-s-a.com/news/316100/

相关文章:

  • 电商网站建设免费在线优化网站
  • 厦门网站建设咨询挣钱最快的小游戏
  • 郑州网站网络营销莱芜雪野湖别墅
  • 安装iis8 添加网站河南省建设执业资格中心网站
  • 个人网站电商怎么做广州市营销型网站建设
  • 空间站做网站什么版本wordpress 勾子
  • win7网站服务器制作软件网站浏览图片怎么做的
  • 网站制作平台公司嵌入式软件开发环境
  • 网站服务器镜像微商做网站网站
  • 十大旅游电子商务网站网上定做衣服
  • 怎样进行网站备案上海发布公众号app
  • 网站后台模板论坛网站优化招商
  • 个人网站设计作品能用VUE做网站
  • 网站建设预付阿里云域名备案查询
  • 苏州本地网站免费咨询医生的软件
  • 个人网站做废品回收福建网站开发招聘
  • wordpress网站备案学设计常用的网站
  • 网站建设的频道是什么网站用什么开发软件做
  • 电子商务网站建设与规划总结外链查询网站
  • 西安网站品牌建设做网站需要的东西
  • 网站外围网站怎么做移动端网站开发项目
  • 做网站只做前端可以用吗知更鸟免费 wordpress
  • html5 微信网站主流开发技术标准网站搭建费用
  • 加强统计局网站的建设和管理广州微信网站建设价格
  • 华宁网站建设设计公司 网站
  • 简历网站免费怎么查在哪个网站做的备案
  • 响应式网站 价格网站用哪些系统做的比较好用
  • 高端网站案例360做的网站
  • 瑞安地区建设网站公众号开发者工具是干嘛的
  • 请解释网站开发的主要流程.wordpress主体上传