12345678910111213141516171819202122232425262728293031 |
- package cn.hhj.controller;
- import cn.hhj.service.IDeptService;
- import cn.hhj.vo.Dept;
- import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- @RestController
- public class DeptRest {
- @Autowired
- private IDeptService deptService;
- @GetMapping("/dept/get/{id}")
- @HystrixCommand(fallbackMethod = "getFallback")// 指定方法跑出异常时执行的方法
- public Object get(@PathVariable("id") long id) {
- Dept vo=this.deptService.get(id);
- if (vo == null) { // 数据不存在,假设让它抛出个错误
- throw new RuntimeException("部门信息不存在!") ;
- }
- return vo;
- }
- public Object getFallback(@PathVariable("id") long id) { // 此时方法的参数 与get()一致
- Dept vo = new Dept() ;
- vo.setDeptno(999999L);
- vo.setDname("【ERROR】Microcloud-Dept-Hystrix"); // 错误的提示
- vo.setLoc("DEPT-Provider");
- return vo ;
- }
- }
|