【发布时间】:2019-08-05 19:44:06
【问题描述】:
在我的 Micronaut 应用程序中,我定义了一个 ExceptionHandler,它应该捕获 WorkflowException 并返回一个带有状态码 412 和 WorkflowExceptionVM 作为正文的 HttpResponse。
这是我当前的实现:
@Produces
@Singleton
@Requires(classes = [WorkflowException::class, ExceptionHandler::class])
class WorkflowExceptionHandler : ExceptionHandler<WorkflowException, HttpResponse<WorkflowExceptionVM>> {
override fun handle(request: HttpRequest<Any>, exception: WorkflowException): HttpResponse<WorkflowExceptionVM> {
return HttpResponse.status<WorkflowExceptionVM>(HttpStatus.PRECONDITION_FAILED)
.body(WorkflowExceptionVM(exception.code))
}
}
它使用正确的代码生成响应,但不包括正文。
这是我的测试:
@Test
fun `Should handle WorkflowException`() {
val request = POST(THE_URL, THE_BODY).basicAuth("user", "password")
val exception = shouldThrow<HttpClientResponseException>
{client.toBlocking().exchange(request, MyEntity::class.java) }
exception.response.code() shouldBe 412 // This works
exception.response.body() // ERROR: this is null
}
如何将正文添加到响应中?
【问题讨论】: