DeptRest.java 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. package cn.hhj.controller;
  2. import cn.hhj.service.IDeptService;
  3. import cn.hhj.vo.Dept;
  4. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.*;
  7. @RestController
  8. public class DeptRest {
  9. @Autowired
  10. private IDeptService deptService;
  11. @GetMapping("/dept/get/{id}")
  12. @HystrixCommand(fallbackMethod = "getFallback")// 指定方法跑出异常时执行的方法
  13. public Object get(@PathVariable("id") long id) {
  14. Dept vo=this.deptService.get(id);
  15. if (vo == null) { // 数据不存在,假设让它抛出个错误
  16. throw new RuntimeException("部门信息不存在!") ;
  17. }
  18. return vo;
  19. }
  20. public Object getFallback(@PathVariable("id") long id) { // 此时方法的参数 与get()一致
  21. Dept vo = new Dept() ;
  22. vo.setDeptno(999999L);
  23. vo.setDname("【ERROR】Microcloud-Dept-Hystrix"); // 错误的提示
  24. vo.setLoc("DEPT-Provider");
  25. return vo ;
  26. }
  27. }