Springboot 利用Aspect 解决NULL 返还为""
2017-05-15 00:00
176 查看
摘要: 主要是利用Aspect的机制来解决java 中null 字段 为"" 的问题,前端更容易去处理数据
废话就不多说了直接上代码:
如果你不需要正式环境下有这个,那么最好注销或者使用e.getMessage();
废话就不多说了直接上代码:
/** * Controller层AOP,记录请求和返回结果日志扫描controller日志 * * @author reasahi * @version 1.0.0 */ @Aspect @Component public class WebLogAspect { /** * 日志 */ private static final Logger logger = LoggerFactory.getLogger(WebLogAspect.class); @Resource private MappingJackson2HttpMessageConverter converter; /** * 同步计算每次请求时间 */ ThreadLocal<Long> startTime = new ThreadLocal<>(); // 要扫描多个Controller 使用这种方式进行处理,如果你的包里面分了多个Controller;如果只有一个删除一个execution留一个就行了 @Pointcut("execution(public * com.exclmpl.controller.*.*(..)) || execution(public * com.exclmpl.webController.*.*(..))") public void webLog() { } @Before("webLog()") public void doBefore(JoinPoint joinPoint) throws Throwable { startTime.set(System.currentTimeMillis()); // 接收到请求,记录请求内容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder .getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // 记录下请求内容 logger.info("请求连接 : " + request.getRequestURL().toString()); logger.info("请求方式 : " + request.getMethod()); logger.info("请求IP: " + IpHost.getIpAddr(request)); logger.info("请求类: " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); // logger.info("请求参数 : " + params); logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs())); } @AfterReturning(returning = "ret", pointcut = "webLog()") public void doAfterReturning(Object ret) throws Throwable { // 处理完请求,返回内容 logger.info("请求返还内容 : " + ret); logger.info("耗时 : " + (System.currentTimeMillis() - startTime.get()) + "毫秒"); } @Around(value = "webLog()") @ResponseBody public void formatResult2JSON(ProceedingJoinPoint pjp) throws Throwable { HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); HttpOutputMessage outputMessage = new ServletServerHttpResponse(response); //移除所有字段为null的字段,其实就是使用了toString 之后移除 所有的null字段,但是这样处理的后果就是所有返还字段内不能带有null这样小写的字段,否则将会全部被移替换掉 String result=pjp.proceed().toString().replaceAll("null", "\"\""); converter.write(JSONObject.fromObject(result), MediaType.APPLICATION_JSON_UTF8, outputMessage); shutdownResponse(response); } @AfterThrowing(pointcut = "webLog()", throwing = "error") public void handleForException(JoinPoint jp, Throwable error) throws Throwable { HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); HttpOutputMessage outputMessage = new ServletServerHttpResponse(response); //这里可能会报错,原因是因为你在 try ctah模块里面用
e.printStackTrace();
如果你不需要正式环境下有这个,那么最好注销或者使用e.getMessage();
converter.write(JSONObject.fromObject(error.getMessage()), MediaType.APPLICATION_JSON_UTF8, outputMessage); shutdownResponse(response); } private void shutdownResponse(HttpServletResponse response) throws IOException { response.getOutputStream().close(); } }
相关文章推荐
- SpringBoot 文档根元素 "beans" 必须匹配 DOCTYPE 根 "null"
- 解决内核编译时产生 "mkimage" command not found - U-Boot images will not be built 错误
- Springboot ServletContextListener @Autowired null 解决办法
- springMvc配置tomcat时出现"No Spring WebApplicationInitializer types detected on classpath" 解决方法
- springboot整合shiro认证时"username"更改为"account"或自定义的ShiroConfig配置
- Spring Boot @PathVariable出现点号"."时导致路径参数截断获取不全的解决办法
- springboot 利用configureMessageConverters add FastJsonHttpMessageConverter 实现返回JSON值 null to ""
- 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法
- 解决SpringBoot框架下利用jdbcTemplate插入mysql数据库中文乱码问题
- Springboot ServletContextListener @Autowired null 解决办法
- 1.1 使用IntelliJ IDEA搭建Spring-Boot的"Hello World"项目
- Spring Boot+Thymeleaf出现org.xml.sax.SAXParseException: 元素类型 "meta" 必须由匹配的结束标记 "</meta>" 终止。
- 利用Spring AOP (aspect) 自定义注解解决日志和签名校验
- windows 7 "unmountable boot volume" 解决方法
- Spring Boot + Bootstrap 出现"Failed to decode downloaded font"和"OTS parsing error: Failed to convert WOFF 2.0 font to SFNT"
- Spring-Boot "Whitelabel Error Page" 解决方法总结
- springboot 使用thymeleaf 模板引擎时报错org.xml.sax.SAXParseException: 元素类型 "link" 必须由匹配的结束标记 "</link>" 终止
- springboot解决传递json字符串时获取某个参数为null
- 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法
- 关于SpringBoot自定义注解(解决post接收String参数 null(前台传递json格式))