Micronaut 控制器在验证失败时响应错误

Micronaut controller responds with error when the validation fails

我正在尝试编写一个 Micronaut 服务,该服务 returns 在数据存在验证错误时做出特定响应。


  @Post(value = "/", produces = MediaType.APPLICATION_JSON)
  public Response<Category> save(@Valid @Body Category category) {
    Category savedCategory = categoryService.save(category);
    Response<Category> response = new Response<>("Category Saved Successfully", State.SUCCESS,
        savedCategory);
    return response;
  }

  @Error(exception = ConstraintViolationException.class)
  public Response<Category> onSavedFailed(HttpRequest request, ConstraintViolationException ex) {
    Optional<Category> requestBody = request.getBody(Category.class);
    Response<Category> response = new Response<>("", State.FAILED, requestBody.get());
    response.addErrors(messageSource.violationsMessages(ex.getConstraintViolations()));
    System.out.println(response); // [1] THIS LINE IS EXECUTED
    return response;
  }

此代码似乎工作正常,因为当我使用以下测试调用代码时,标记为 [1] 的行已正确执行。 但是在捕获 HttpClientResponseException 后的测试中,我无法获得控制器返回的响应对象,我做错了什么?

@Test
  public void should_not_save_category_without_name(){
    try (RxHttpClient client = embeddedServer.getApplicationContext()
        .createBean(RxHttpClient.class, embeddedServer.getURL())) {
      Category category =  new Category("");
      Response<Category> retrieve = client.toBlocking()
          .retrieve(HttpRequest.POST("/categories", category),
              Argument.of(Response.class, Category.class));
    }catch (HttpClientResponseException exception){
      System.out.println(exception.getResponse().getStatus());
      System.out.println(exception);
    }
  }

这是测试用例中打印的内容

INTERNAL_SERVER_ERROR
io.micronaut.http.client.exceptions.HttpClientResponseException: Internal Server Error

这是 micronaut 客户端日志

10:11:51.571 [nioEventLoopGroup-1-2] DEBUG i.m.http.client.DefaultHttpClient - Sending HTTP Request: POST /categories
10:11:51.572 [nioEventLoopGroup-1-2] DEBUG i.m.http.client.DefaultHttpClient - Chosen Server: localhost(6140)
10:11:51.574 [nioEventLoopGroup-1-2] TRACE i.m.http.client.DefaultHttpClient - host: localhost:6140
10:11:51.574 [nioEventLoopGroup-1-2] TRACE i.m.http.client.DefaultHttpClient - connection: close
10:11:51.574 [nioEventLoopGroup-1-2] TRACE i.m.http.client.DefaultHttpClient - content-type: application/json
10:11:51.574 [nioEventLoopGroup-1-2] TRACE i.m.http.client.DefaultHttpClient - content-length: 2
10:11:51.574 [nioEventLoopGroup-1-2] TRACE i.m.http.client.DefaultHttpClient - Request Body
10:11:51.574 [nioEventLoopGroup-1-2] TRACE i.m.http.client.DefaultHttpClient - ----
10:11:51.574 [nioEventLoopGroup-1-2] TRACE i.m.http.client.DefaultHttpClient - {}
10:11:51.574 [nioEventLoopGroup-1-2] TRACE i.m.http.client.DefaultHttpClient - ----
Response{message='', errors=[name must not be blank], state=FAILED, data=Category{id=null, name=null}}
10:11:51.831 [nioEventLoopGroup-1-2] TRACE i.m.http.client.DefaultHttpClient - HTTP Client Response Received for Request: POST http://localhost:6140/categories
10:11:51.831 [nioEventLoopGroup-1-2] TRACE i.m.http.client.DefaultHttpClient - Status Code: 500 Internal Server Error
10:11:51.831 [nioEventLoopGroup-1-2] TRACE i.m.http.client.DefaultHttpClient - Date: Wed, 8 Jan 2020 04:41:51 GMT
10:11:51.831 [nioEventLoopGroup-1-2] TRACE i.m.http.client.DefaultHttpClient - content-type: application/json
10:11:51.831 [nioEventLoopGroup-1-2] TRACE i.m.http.client.DefaultHttpClient - content-length: 64
10:11:51.831 [nioEventLoopGroup-1-2] TRACE i.m.http.client.DefaultHttpClient - connection: close
10:11:51.832 [nioEventLoopGroup-1-2] TRACE i.m.http.client.DefaultHttpClient - Response Body
10:11:51.832 [nioEventLoopGroup-1-2] TRACE i.m.http.client.DefaultHttpClient - ----
10:11:51.832 [nioEventLoopGroup-1-2] TRACE i.m.http.client.DefaultHttpClient - {"errors":["name must not be blank"],"state":"FAILED","data":{}}
10:11:51.832 [nioEventLoopGroup-1-2] TRACE i.m.http.client.DefaultHttpClient - ----

要获取错误正文,您需要使用 3 个参数方法,其中第 3 个参数是错误类型:https://docs.micronaut.io/latest/api/io/micronaut/http/client/DefaultHttpClient.html#exchange-io.micronaut.http.HttpRequest-io.micronaut.core.type.Argument-io.micronaut.core.type.Argument-

因此,您的代码应该类似于:

HttpRequest request = HttpRequest.POST("/categories", category);
client.toBlocking().exchange(request, Argument.of(Object.class), Argument.of(JsonError));

...

exception.getResponse().getBody(Argument.of(Response.class, Category.class)) // This return an Optional

另外,为了确保一切都按预期工作并且您确实遇到了错误,您可以为 HttpClient 启用跟踪,将以下内容添加到 logback.xml:

<configuration>
    ...
    ...

    <logger name="io.micronaut.http.client" level="TRACE" />

</configuration>