将 WebTestClient.BodyContentSpec 转换为 JSON 对象

convert WebTestClient.BodyContentSpec to JSON object

我有测试用例

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@AutoConfigureWebTestClient
public class PaymentsITest {

    @Autowired
    private WebTestClient client1;


    @Test
    public void getPayment() {
        client1.get().uri("/v1/payments/123" )
                .accept(MediaType.APPLICATION_JSON)
                .exchange()
                .expectBody()
                .jsonPath("$.group_header.identification").exists()
                .jsonPath("$.group_header.date_time").exists()
                .jsonPath("$.response").exists()
        ;
    }
}

我需要检查 response 对象中的属性。 有没有办法将方法 expectBody() 返回的 WebTestClient.BodyContentSpec 转换为 JSON 对象或 JSON 字符串?

找到了 使用 expectBody().returnResult().getResponseBody() 它将 return 作为 byte[] 的响应,然后将 byte[] 转换为字符串

    String s=new String(client1.get().uri("/v1/payments/123" )
            .accept(MediaType.APPLICATION_JSON)
            .exchange()
            .expectBody().returnResult().getResponseBody());

    System.out.println(s);