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



我有两个@RestController- (A 和 B( 和注册ResponseEntityExceptionHandler。是否可以(以及如何执行此操作(在应用异常处理程序后从A调用并从B获得响应?

例:

  1. 用户休息呼叫A
  2. A呼叫BgetPerson
  3. B引发异常NotFound
  4. NotFound由异常处理程序处理,转换ResponseEntity并放置 400 状态
  5. B最终返回异常ResponseEntity
  6. AB获得 400 状态
  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;
}
}

无法从异常处理程序将控制权带回控制器,该处理程序在处理其方法后被调用。您的当前流看起来像这样call A.addPersons->invoke B.addPerson->B throws exception->exception is propagate to A controller并且它被保存为 dispatchException 在处理控制器方法(不是状态为 400 的 ResponseEntity(后在 DispatcherServlet 中进一步处理 ->异常使用 MyExceptionHandler 处理。从这个地方你不能回到控制器。

我不确定您想在控制器中使用此异常做什么,但解决方案可能如下所示:

@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!");
}
}

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

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

最新更新