[英]Spring - Intercept response from another bean by exception handler
我有两个@RestController
s-(A和B)和注册的ResponseEntityExceptionHandler
。 应用异常处理程序后,是否有可能(以及如何执行)从A
调用并从B
获得响应?
例:
A
A
用getPerson
调用B
B
抛出NotFound
异常 NotFound
由异常处理程序处理,转换ResponseEntity
并放置400状态 B
终于返回异常ResponseEntity
A
从B
获得400状态 A
可以得到这个400并用它做点什么 简单的@Autowired
无法正常工作。
片段:
A:
@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);
}
}
B:
@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;
}
}
转发就像重定向,但完全发生在服务器端; Servlet容器将相同的请求转发到目标URL; 该网址在浏览器中不会更改。 在春季,您可以这样做,并且可以传递attribute:
@Controller
@RequestMapping("/")
public class YourController {
@GetMapping("/forward")
public ModelAndView redirectWithUsingForwardPrefix(ModelMap model) {
model.addAttribute("attribute", "forward");
return new ModelAndView("forward:/redirectedUrl", model);
}
}
无法从异常处理程序中将控制权返回给控制器,该异常处理程序在处理其方法后将被调用。 您当前的流程如下所示: call A.addPersons
> invoke B.addPerson
> B throws exception
-> exception is propagate to A controller
并在处理控制器方法(状态为400的ResponseEntity)后另存为dispatchException以便在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!");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.