【发布时间】:2019-02-08 08:19:02
【问题描述】:
在使用注释为 Spring @ResponseStatus 的 Axon @QueryHandler 中抛出的异常时遇到问题。原始异常被 QueryHandler 吞下,Axon 特定的 AxonServerRemoteQueryHandlingException 被抛出,当 spring 响应客户端时实际给出 500
仍然可以从 Axon 异常中获取一些信息,例如原始的“Entity not found”消息,但不是异常类型,也不是原始异常包含的任何其他信息。
Q1:有什么方法可以将 Query 处理程序中抛出的异常提升为 Spring 响应为 404
Spring 异常处理程序
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class NotFoundException extends ServiceException() {
...
}
Axon 查询处理程序
@QueryHandler
public Application getApplicationById(ApplicationByIdQuery query) {
return applicationRepository.findById(query.getId())
.orElseThrow(() -> new NotFoundException(Application.class, query.getId()));
}
弹簧控制器
@Autowired
QueryGateway queryGateway;
@GetMapping(path = "/{applicationId}")
public CompletableFuture<Application> getApplication(@PathVariable String applicationId) {
return queryGateway.query(new ApplicationByIdQuery(applicationId), ResponseTypes.instanceOf(Application.class));
}
实际结果json:
{
"timestamp": "2019-02-08T08:04:03.629+0000",
"status": 500,
"error": "Internal Server Error",
"message": "An exception was thrown by the remote message handling component.",
"path": "/api/applications/dff59c46-baf1-40f5-8a21-9286d1f8e36fx"
}
Q2:我的另一个问题是为什么不直接使用常规的 JPA Query API 而是使用 Axon 的 QueryHandler。投影表是常规的 JPA 表,可以通过非常强大的 Spring JPA 进行质疑。是不是因为直接查询不能保证投影数据的一致性?我经历了很多例子,其中大多数使用直接访问(见下文),其余的都不能解决底层 QueryHandler 抛出的异常
@Autowired
ApplicationRepository applicationRepository;
public CompletableFuture<Application> getApplication(@PathVariable String applicationId) {
return CompletableFuture.supplyAsync(() -> applicationRepository.findById(applicationId)
.orElseThrow(() -> new NotFoundException(Application.class, applicationId)));
}
【问题讨论】: