代码之家  ›  专栏  ›  技术社区  ›  ByeBye

Spring-通过异常处理程序截获来自另一个bean的响应

  •  0
  • ByeBye  · 技术社区  · 7 年前

    @RestController s-(A和B)和注册 ResponseEntityExceptionHandler A 并得到 B 在应用异常处理程序之后?

    1. 用户rest调用
    2. A B类 getPerson
    3. B类 引发异常 NotFound
    4. 找不到 由异常处理程序处理,转换 ResponseEntity
    5. B类 最终返回异常 反应性
    6. A 从中获取状态 B类
    7. A 你能拿这400块做点什么吗

    @Autowired 不起作用。

    代码段:

    答:

    @RestController
    @RequestMapping("/v1")
    public class A {
    
        private final B b;
    
        @Autowired
        public A(B b) {
            this.b = b;
        }
    
        @PostMapping(
            value = "persons",
            consumes = "application/json",
            produces = "application/json")
        public ResponseEntity<List<StatusResponse<Person>>> addPersons(final List<Person> persons) {
            final List<StatusResponse<Person>> multiResponse = new ArrayList<>();
            for(final Person p: persons) {
                final ResponseEntity<Person> response = b.addPerson(person);
                multiResponse.add(new StatusResponse<>(
                    response.getStatusCode(), response.getMessage(), response.getBody()
                ));
            }
            return ResponseEntity.status(HttpStatus.MULTI_STATUS).body(multiResponse);
        }
    
    }
    

    @RestController
    @RequestMapping("/v1")
    public class B {
    
        @PostMapping(
            value = "person",
            consumes = "application/json",
            produces = "application/json")
        public ResponseEntity<Person> addPerson(final Person person) {
            accessService.checkAccess();
            return ResponseEntity.status(201).body(
                logicService.addPerson(person)
            );
        }
    
    }
    

    处理程序

    @ControllerAdvice
    public final class MyExceptionHandler extends ResponseEntityExceptionHandler {
    
        @ExceptionHandler(MyException.class)
        protected ResponseEntity<Object> handleApiException(final MyException exception, final WebRequest webRequest) {
            //logic
            return afterLogic;
        }
    
    }
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   ByeBye    7 年前

    你的电流是这样的 call A.addPersons invoke B.addPerson -&燃气轮机; B throws exception -&燃气轮机; exception is propagate to A controller

    我不确定在控制器中出现此异常时要执行什么操作,但解决方案可能如下所示:

    @RestController
    @RequestMapping("/resources")
    public class AController {
    
        private BService service;
    
        @Autowired
        public AController(BService service) {
            this.service = service;
        }
    
        @RequestMapping("/test")
        public ResponseEntity<String> test() {
            ResponseEntity<String> result = service.test();
            if (result.hasBody()) {
                //doSomething
            }
    
            return result; //or list like you did
        }
    }
    
    @Service
    public class BService {
    
        public ResponseEntity<String> test() {
            try {
                return ResponseEntity.status(201).body(getResponse()); //this always throws exception. It's just for purpose of example
            } catch (CustomException ex) {
                return ResponseEntity.status(400).build();
            }
    
        }
    
        private String getResponse() {
            throw new CustomException("Not OK!");
        }
    }
    
        2
  •  0
  •   Danial Jalalnouri    7 年前

    forward类似于重定向,但完全发生在服务器端;Servlet容器将相同的请求转发到目标URL;URL在浏览器中不会更改。 在spring中,可以这样做,并且可以传递属性:

    @Controller
    @RequestMapping("/")
    public class YourController {
    
        @GetMapping("/forward")
        public ModelAndView redirectWithUsingForwardPrefix(ModelMap model) {
            model.addAttribute("attribute", "forward");
            return new ModelAndView("forward:/redirectedUrl", model);
        }
    }