简体   繁体   English

如何使用Spring REST Docs记录@ControllerAdvice处理的异常

[英]How to document @ControllerAdvice handled exception using Spring REST Docs

I have @ControllerAdvice annotated class, which is handling BadRequestException extends RuntimeException exception. 我有@ControllerAdvice注释的类,它在处理BadRequestException extends RuntimeException异常。

Now suppose that I have endpoint: 现在假设我有端点:

@PostMapping(value = "/createAccount")
public ResponseEntity<CreateAccountResponse> createAccount(@RequestBody @Valid CreateAccountRequest createAccountRequest) {...}

In case of unwanted scenario, endpoint throws BadRequestException (with HTTP Status 400) which constructs error JSON object as following: 如果发生意外情况,端点将抛出BadRequestException (HTTP状态为400),该错误构造如下的JSON对象:

{ 
  "errorCode": 123,
  "errorMessage: "Failure reason"
}

Is there any way to document case like this using Spring REST Docs ? 有什么办法可以使用Spring REST Docs记录这种情况?

This is example of my approach: 这是我的方法的示例:

@Test
public void createAccountFailExample() {

       RestDocumentationResultHandler docs = document("create-acc-fail-example",
       responseFields(
                        fieldWithPath("errorCode").type("Integer").description("Error code"),
                        fieldWithPath("errorMessage").type("String").description("Error message")
                )
        );

        org.assertj.core.api.Assertions.assertThatThrownBy(() -> this.mockMvc.perform(RestDocumentationRequestBuilders.post("/createAccount")
                        .contextPath("/account")
                        .contentType(TestUtil.APPLICATION_JSON_UTF8)
                        .content(TestUtil.convertObjectToJsonBytes(new CreateAccountRequest("nameTest", "surnameTest"))))
                        .andExpect(status().isBadRequest())
                        .andDo(docs)).hasCause(new BadRequestException(ServiceError.SOME_FAIL_REASON));
}

In this case test passes, but no documentation (.adoc) files are created. 在这种情况下,测试通过了,但是没有创建任何文档(.adoc)文件。

When I try something like this: 当我尝试这样的事情:

ResultActions resultActions = this.mockMvc.perform(RestDocumentationRequestBuilders.post("/createAccount")
                .contextPath("/account")
                .contentType(TestUtil.APPLICATION_JSON_UTF8)
                .content(TestUtil.convertObjectToJsonBytes(new CreateAccountRequest("testName", "testSurname"))))
                .andExpect(status().isBadRequest())
                .andDo(docs);

test fails because NestedServletException was thrown caused by BadRequestException , and again there is no documentation created. 测试失败,因为NestedServletException是由BadRequestException引发的,并且再次没有创建文档。

I managed to solve the problem following this answer . 我设法解决了这个问题 When exception handler is defined for MockMvc , my second approach works as expected. 当为MockMvc定义异常处理程序时,我的第二种方法可以按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring ControllerAdvice 中未处理 404 异常 - 404 Exception not handled in Spring ControllerAdvice ControllerAdvice 的异常处理程序在使用 Spring Boot 的 Rest API 获取请求中不起作用。 如何解决? - Exception Handler of ControllerAdvice Not working in Rest API Get Request using Spring Boot. How to resolve? 如何使用Spring Rest Docs记录对象列表 - How to document list of objects using Spring Rest Docs Spring 使用@ControllerAdvice 处理异常 - Spring Exception Handling using @ControllerAdvice 如何在Spring REST Docs中记录树 - how to document trees in Spring REST Docs 如何使用有条件地返回响应的Spring REST Docs来记录REST服务? - How to document REST services with Spring REST Docs that return a response conditionally? Spring引导不使用@ControllerAdvice覆盖Exception - Spring boot not overriding Exception using @ControllerAdvice Spring rest 文档无法使用 WebTestClient 记录路径参数 - Spring rest docs failing to document pathParameters using WebTestClient 如何使用 Spring @ControllerAdvice 和 problem-spring-web 处理 hibernate 异常 - How to process hibernate exception using Spring @ControllerAdvice and problem-spring-web 文档分层 JSON 有效负载 Spring REST 文档 - document hierarchical JSON payload with Spring REST Docs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM