随笔 - 276  文章 - 0  评论 - 32  阅读 - 56万
 
摘要: C++ 函数查询:http://www.cplusplus.com/reference/ http://classfoo.com/ccby/article/acZKb C++ 参考手册:C++设计模式系列:http://www.jellythink.com/archives/878 JAVA 菜鸟教 阅读全文
posted @ 2017-02-27 16:02 已停更 阅读(397) 评论(0) 推荐(1) 编辑
 
摘要: 什么是对象模型 有两个概念可以解释C++对象模型: 语言中直接支持面向对象程序设计的部分。对于各种支持的底层实现机制。 类中成员分类 数据成员分为静态和非静态,成员函数有静态非静态以及虚函数 class data members:static和nonstatic class data functio 阅读全文
posted @ 2016-08-12 15:09 已停更 阅读(11606) 评论(4) 推荐(9) 编辑
  2018年1月28日

参考:

http://blog.csdn.net/w372426096/article/details/78429132

http://blog.csdn.net/w372426096/article/details/78429141

@ExceptionHandler:统一处理某一类异常,从而能够减少代码重复率和复杂度

源码如下:

复制代码
1 @Target({ElementType.METHOD})
2 @Retention(RetentionPolicy.RUNTIME)
3 @Documented
4 public @interface ExceptionHandler {
5     Class<? extends Throwable>[] value() default {};
6 }
复制代码

注解作用对象为方法,并且在运行时有效,value()可以指定异常类。由该注解注释的方法可以具有灵活的输入参数(详细参见Spring API):

  • 异常参数:包括一般的异常或特定的异常(即自定义异常),如果注解没有指定异常类,会默认进行映射。
  • 请求或响应对象 (Servlet API or Portlet API): 你可以选择不同的类型,如ServletRequest/HttpServletRequest或PortleRequest/ActionRequest/RenderRequest
  • Session对象(Servlet API or Portlet API): HttpSession或PortletSession。
  • WebRequest或NativeWebRequest 
  • Locale
  • InputStream/Reader 
  • OutputStream/Writer 
  • Model

方法返回值可以为:

  • ModelAndView对象
  • Model对象
  • Map对象
  • View对象
  • String对象
  • 还有@ResponseBody、HttpEntity<?>或ResponseEntity<?>,以及void

@ControllerAdvice

源码如下:

复制代码
 1 @Target({ElementType.TYPE})
 2 @Retention(RetentionPolicy.RUNTIME)
 3 @Documented
 4 @Component
 5 public @interface ControllerAdvice {
 6     @AliasFor("basePackages")
 7     String[] value() default {};
 8 
 9     @AliasFor("value")
10     String[] basePackages() default {};
11 
12     Class<?>[] basePackageClasses() default {};
13 
14     Class<?>[] assignableTypes() default {};
15 
16     Class<? extends Annotation>[] annotations() default {};
17 }
复制代码

该注解作用对象为TYPE,包括类、接口和枚举等,在运行时有效,并且可以通过Spring扫描为bean组件。其可以包含由@ExceptionHandler、@InitBinder 和@ModelAttribute标注的方法,可以处理多个Controller类,这样所有控制器的异常可以在一个地方进行处理

@ResponseStatus:可以将某种异常映射为HTTP状态码

 

 


无论是普通的WEB项目,还是用SpringMVC实现的restful服务,都曾经历过下面两个问题:

这里写图片描述

 

  1. @PostMapping(path = "/selectByAcctcode")
  2. public MerAccountQueryResponse selectByAcctcode(@Valid @RequestBody MerAccountQueryRequest request,BindingResult result) {
  3.  
  4. log.info(Constants.REQUEST_MSG, JSON.toJSONString(request));
  5.  
  6. MerAccountQueryResponse response = new MerAccountQueryResponse();
  7.  
  8. try {
  9. Pageable pageable = new PageRequest(request.getPageNum(), request.getPageSize());
  10. response = merAccountService.selectByAcctcode(request, pageable);
  11. // 返回成功报文
  12. MessageUtil.createCommMsg(response);
  13. } catch (BizException e) {
  14. log.error(Constants.BUSINESS_ERROR, e);
  15. // 组织错误报文
  16. MessageUtil.errRetrunInAction(response, e);
  17. } catch (Exception ex) {
  18. log.error(Constants.EXCEPTION_ERROR, ex);
  19. // 组织错误报文
  20. MessageUtil.createErrorMsg(response,ex);
  21. }
  22. log.info(Constants.REPONSE_MSG, JSON.toJSONString(response));
  23. return response;
  24. }

 

当你有100个接口的时候,就得重复100次,如果你觉得代码量也就那么点,copy就copy吧,反正我是代码的搬运工,只是你有曾想过我可以抽取出来吗?

 

这里写图片描述

我们在写Controller的时候,如果没有出现过异常固然没问题,但一旦出现异常了,如果你处理了,那就需要你手动指定跳转到事先定义好的界面,如果你没处理,那将得到是一个非常丑陋的界面,如下:

这里写图片描述

如何避免这种问题呢???

这里写图片描述

这里写图片描述

 

  1. @Controller
  2. @RequestMapping(value = "exception")
  3. public class ExceptionHandlerController {
  4.  
  5. @ExceptionHandler({ ArithmeticException.class })
  6. public String handleArithmeticException(Exception e) {
  7. e.printStackTrace();
  8. return "error";
  9. }
  10.  
  11. @RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
  12. @ResponseBody
  13. public String testExceptionHandle(@PathVariable(value = "id") Integer id) {
  14. System.out.println(10 / id);
  15. return id.toString();
  16. }
  17. }


当访问exception/e/0的时候,会抛出ArithmeticException异常,@ExceptionHandler就会处理并响应error.jsp

这里写图片描述

 

  1. @Controller
  2. @RequestMapping(value = "exception")
  3. public class ExceptionHandlerController {
  4.  
  5. @ExceptionHandler({ ArithmeticException.class })
  6. @ResponseBody
  7. public String handleArithmeticException(Exception e) {
  8. e.printStackTrace();
  9. JSONObject jo = new JSONObject();
  10. jo.put("resCode","999999");
  11. jo.put("resMsg","系统异常");
  12. return jo.toString();
  13. }
  14.  
  15. @RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
  16. @ResponseBody
  17. public String testExceptionHandle(@PathVariable(value = "id") Integer id) {
  18. System.out.println(10 / id);
  19. return id.toString();
  20. }
  21. }

 

当然实际项目中,并不会像我这里写的这么简陋,我这里只是抛砖引玉,给你一个思路。

这里写图片描述

在实际项目中,可能碰到这种情况,我们提供的服务,调用方并不需要json报文中的消息,调用方只关注响应码,比如200,代表调用正常;404,代表请求资源不存在;502,代表系统异常。。。等等。我们又该如何去做?

这里写图片描述

  1. package com.somnus.exception;
  2.  
  3. import org.springframework.http.HttpStatus;
  4. import org.springframework.web.bind.annotation.ResponseStatus;
  5.  
  6. @ResponseStatus(value=HttpStatus.BAD_GATEWAY)
  7. public class HttpStatusException extends RuntimeException {
  8.  
  9. private static final long serialVersionUID = 1L;
  10.  
  11. public HttpStatusException() {
  12. super();
  13. }
  14.  
  15. public HttpStatusException(String message, Throwable cause) {
  16. super(message, cause);
  17. }
  18.  
  19. public HttpStatusException(String message) {
  20. super(message);
  21. }
  22.  
  23. public HttpStatusException(Throwable cause) {
  24. super(cause);
  25. }
  26.  
  27. }
  1. @Controller
  2. @RequestMapping(value = "status")
  3. public class ResponseStatusController {
  4.  
  5. @RequestMapping(value = "e/{id}", method = { RequestMethod.GET })
  6. @ResponseBody
  7. public String status(@PathVariable(value = "id") Integer id){
  8. if(id % 2 != 0){
  9. throw new HttpStatusException();
  10. }
  11. return id.toString();
  12. }
  13. }

 

 

效果如下:

这里写图片描述

另外这里不得不提一点需要注意的,不要轻易把@ResponseStatus修饰目标方法,因为无论它执行方法过程中有没有异常产生,用户都会得到异常的界面,而目标方法正常执行。

  1. package com.somnus.controller;
  2.  
  3. import org.springframework.http.HttpStatus;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import org.springframework.web.bind.annotation.ResponseStatus;
  10.  
  11. import com.somnus.exception.HttpStatusException;
  12.  
  13. @Controller
  14. @RequestMapping(value = "status")
  15. public class ResponseStatusController {
  16.  
  17. /**
  18. * ResponseStatus修饰目标方法,无论它执行方法过程中有没有异常产生,用户都会得到异常的界面。而目标方法正常执行
  19. * @param id
  20. * @return
  21. */
  22. @RequestMapping(value = "e2/{id}", method = { RequestMethod.GET })
  23. @ResponseStatus(value=HttpStatus.BAD_GATEWAY)
  24. @ResponseBody
  25. public String status2(@PathVariable(value = "id") Integer id){
  26. System.out.println(10 / id);
  27. return id.toString();
  28. }
  29.  
  30. }

 

 

可以看到哪怕是响应2了,但是响应码其实还是502

这里写图片描述


这里写图片描述

如果我们要给jdk自带的异常提供一个响应码呢,我们又不可能去改源码,这时候@ResponseStatus就得配和@ControllerAdvice一起使用了,如下:

 

  1. @Controller
  2. @RequestMapping(value = "exception")
  3. public class ExceptionHandlerController {
  4.  
  5. @ExceptionHandler({ NullPointerException.class })
  6. @ResponseStatus(value=HttpStatus.NOT_FOUND)
  7. public void handleNullPointerException(Exception e) {
  8. e.printStackTrace();
  9. }
  10.  
  11. @RequestMapping(value = "e3/{id}", method = { RequestMethod.GET })
  12. @ResponseBody
  13. public String testExceptionHandle3(@PathVariable(value = "id") Integer id) {
  14. List<String> list = 4 % id == 0 ? null : Arrays.asList(new String[]{"a","b","c","d"});
  15. return list.get(id);
  16. }
  17. }

 

当我们抛出NullPointerException异常的时候会发生什么呢

这里写图片描述

这里写图片描述

    • 当一个Controller中有多个@ExceptionHandler注解出现时,那么异常被哪个方法捕捉呢?这就存在一个优先级的问题,@ExceptionHandler的优先级是:在异常的体系结构中,哪个异常与目标方法抛出的异常血缘关系越紧密,就会被哪个捕捉到

    • @ExceptionHandler这个只会是在当前的Controller里面起作用,如果想在所有的Controller里面统一处理异常的话,可以用@ControllerAdvice来创建一个专门处理的类,我们在下一篇做介绍。

 

@ControllerAdvice,是Spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强。让我们先看看@ControllerAdvice的实现:

  1. package org.springframework.web.bind.annotation;
  2.  
  3. @Target(ElementType.TYPE)
  4. @Retention(RetentionPolicy.RUNTIME)
  5. @Documented
  6. @Component
  7. public @interface ControllerAdvice {
  8.  
  9. @AliasFor("basePackages")
  10. String[] value() default {};
  11.  
  12. @AliasFor("value")
  13. String[] basePackages() default {};
  14.  
  15. Class<?>[] basePackageClasses() default {};
  16.  
  17. Class<?>[] assignableTypes() default {};
  18.  
  19. Class<? extends Annotation>[] annotations() default {};

 

 

没什么特别之处,该注解使用@Component注解,这样的话当我们使用<context:component-scan>扫描时也能扫描到。

再一起看看官方提供的comment。

 

 

大致意思是:

  • @ControllerAdvice是一个@Component,用于定义@ExceptionHandler@InitBinder@ModelAttribute方法,适用于所有使用@RequestMapping方法。

  • Spring4之前,@ControllerAdvice在同一调度的Servlet中协助所有控制器。Spring4已经改变:@ControllerAdvice支持配置控制器的子集,而默认的行为仍然可以利用。

  • 在Spring4中, @ControllerAdvice通过annotations()basePackageClasses()basePackages()方法定制用于选择控制器子集。

不过据经验之谈,只有配合@ExceptionHandler最有用,其它两个不常用。

这里写图片描述

在SpringMVC重要注解(一)@ExceptionHandler@ResponseStatus我们提到,如果单使用@ExceptionHandler,只能在当前Controller中处理异常。但当配合@ControllerAdvice一起使用的时候,就可以摆脱那个限制了。

 

  1. package com.somnus.advice;
  2.  
  3. import org.springframework.web.bind.annotation.ControllerAdvice;
  4. import org.springframework.web.bind.annotation.ExceptionHandler;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6.  
  7. @ControllerAdvice
  8. public class ExceptionAdvice {
  9.  
  10. @ExceptionHandler({ ArrayIndexOutOfBoundsException.class })
  11. @ResponseBody
  12. public String handleArrayIndexOutOfBoundsException(Exception e) {
  13. e.printStackTrace();
  14. return "testArrayIndexOutOfBoundsException";
  15. }
  16.  
  17. }
  1. @Controller
  2. @RequestMapping(value = "exception")
  3. public class ExceptionHandlerController {
  4.  
  5. @RequestMapping(value = "e2/{id}", method = { RequestMethod.GET })
  6. @ResponseBody
  7. public String testExceptionHandle2(@PathVariable(value = "id") Integer id) {
  8. List<String> list = Arrays.asList(new String[]{"a","b","c","d"});
  9. return list.get(id-1);
  10. }
  11.  
  12. }



 

当我们访问http://localhost:8080/SpringMVC/exception/e2/5的时候会抛出ArrayIndexOutOfBoundsException异常,这时候定义在@ControllerAdvice中的@ExceptionHandler就开始发挥作用了。

如果我们想定义一个处理全局的异常

  1. package com.somnus.advice;
  2.  
  3. import javax.servlet.http.HttpServletRequest;
  4.  
  5. import org.springframework.core.annotation.AnnotationUtils;
  6. import org.springframework.web.bind.annotation.ControllerAdvice;
  7. import org.springframework.web.bind.annotation.ExceptionHandler;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import org.springframework.web.bind.annotation.ResponseStatus;
  10.  
  11. @ControllerAdvice
  12. public class ExceptionAdvice {
  13.  
  14. @ExceptionHandler({ Exception.class })
  15. @ResponseBody
  16. public String handException(HttpServletRequest request ,Exception e) throws Exception {
  17. e.printStackTrace();
  18.  
  19. return e.getMessage();
  20. }
  21.  
  22. }

 

乍一眼看上去毫无问题,但这里有一个纰漏,由于Exception是异常的父类,如果你的项目中出现过在自定义异常中使用@ResponseStatus的情况,你的初衷是碰到那个自定义异常响应对应的状态码,而这个控制器增强处理类,会首先进入,并直接返回,不会再有@ResponseStatus的事情了,这里为了解决这种纰漏,我提供了一种解决方式。

 

  1. package com.somnus.advice;
  2.  
  3. import javax.servlet.http.HttpServletRequest;
  4.  
  5. import org.springframework.core.annotation.AnnotationUtils;
  6. import org.springframework.web.bind.annotation.ControllerAdvice;
  7. import org.springframework.web.bind.annotation.ExceptionHandler;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import org.springframework.web.bind.annotation.ResponseStatus;
  10.  
  11. @ControllerAdvice
  12. public class ExceptionAdvice {
  13.  
  14.  
  15. @ExceptionHandler({ Exception.class })
  16. @ResponseBody
  17. public String handException(HttpServletRequest request ,Exception e) throws Exception {
  18. e.printStackTrace();
  19. //If the exception is annotated with @ResponseStatus rethrow it and let
  20. // the framework handle it - like the OrderNotFoundException example
  21. // at the start of this post.
  22. // AnnotationUtils is a Spring Framework utility class.
  23. if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null){
  24. throw e;
  25. }
  26. // Otherwise setup and send the user to a default error-view.
  27. /*ModelAndView mav = new ModelAndView();
  28. mav.addObject("exception", e);
  29. mav.addObject("url", request.getRequestURL());
  30. mav.setViewName(DEFAULT_ERROR_VIEW);
  31. return mav;*/
  32. return e.getMessage();
  33. }
  34.  
  35. }


如果碰到了某个自定义异常加上了@ResponseStatus,就继续抛出,这样就不会让自定义异常失去加上@ResponseStatus的初衷。

posted @ 2018-01-28 18:33 已停更 阅读(5614) 评论(1) 推荐(0) 编辑
摘要: @RestControllerSpring4之后新加入的注解,原来返回json需要@ResponseBody和@Controller配合。 即@RestController是@ResponseBody和@Controller的组合注解。 阅读全文
posted @ 2018-01-28 18:12 已停更 阅读(316) 评论(0) 推荐(0) 编辑
摘要: 元注解是指注解的注解,包括@Retention @Target @Document @Inherited四种。 1.@Retention: 定义注解的保留策略@Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含@Retentio 阅读全文
posted @ 2018-01-28 18:10 已停更 阅读(302) 评论(0) 推荐(0) 编辑
  2018年1月21日
摘要: 这篇写的很好,另外参考Java8实战 http://blog.csdn.net/sun_promise/article/details/51362838 Java8实战的一个例子 1.Optional简述 到目前为止,著名的NullPointerException是导致Java应用程序失败的最常见原 阅读全文
posted @ 2018-01-21 21:21 已停更 阅读(2410) 评论(0) 推荐(0) 编辑
摘要: Java中,null是一个关键字,用来标识一个不确定的对象。因此可以将null赋给引用类型变量,但不可以将null赋给基本类型变量 Java默认给变量赋值:在定义变量的时候,如果定义后没有给变量赋值,则Java在运行时会自动给变量赋值。赋值原则是整数类型int、byte、short、long的自动赋 阅读全文
posted @ 2018-01-21 17:44 已停更 阅读(348) 评论(0) 推荐(0) 编辑
  2018年1月15日
摘要: 教程: http://www.yiibai.com/guava/ http://ifeve.com/google-guava/ optional 注意java8同样提供optional,区分 意义: 使用Optional除了赋予null语义,增加了可读性,最大的优点在于它是一种傻瓜式的防护。 Opt 阅读全文
posted @ 2018-01-15 00:22 已停更 阅读(764) 评论(0) 推荐(0) 编辑
  2017年10月29日
摘要: http://www.cnblogs.com/cyfonly/p/6059374.html 一. 与 Thrift 的初识 也许大多数人接触 Thrift 是从序列化开始的。每次搜索 “java序列化” + “方式”、“对比” 或 “性能” 等关键字时,搜索引擎总是会返回一大堆有关各种序列化方式的使 阅读全文
posted @ 2017-10-29 18:30 已停更 阅读(540) 评论(0) 推荐(0) 编辑
摘要: 前言 设计一个缓存系统,不得不要考虑的问题就是:缓存穿透、缓存击穿与失效时的雪崩效应。 缓存穿透 缓存穿透是指查询一个一定不存在的数据,由于缓存是不命中时被动写的,并且出于容错考虑,如果从存储层查不到数据则不写入缓存,这将导致这个不存在的数据每次请求都要到存储层去查询,失去了缓存的意义。在流量大时, 阅读全文
posted @ 2017-10-29 15:33 已停更 阅读(7613) 评论(2) 推荐(5) 编辑
  2017年9月10日
摘要: 乐观锁与悲观锁 悲观锁:总是假设最坏的情况,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会阻塞直到它拿到锁。传统的关系型数据库里边就用到了很多这种锁机制,比如行锁,表锁等,读锁,写锁等,都是在做操作之前先上锁。再比如Java里面的同步原语synchron 阅读全文
posted @ 2017-09-10 20:54 已停更 阅读(1402) 评论(0) 推荐(0) 编辑
摘要: 前言 对于ThreadLocal、Volatile、synchronized、Atomic这四个关键字,我想一提及到大家肯定都想到的是解决在多线程并发环境下资源的共享问题,但是要细说每一个的特点、区别、应用场景、内部实现等,却可能模糊不清,说不出个所以然来,所以,本文就对这几个关键字做一些作用、特点 阅读全文
posted @ 2017-09-10 16:34 已停更 阅读(682) 评论(0) 推荐(0) 编辑
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示