gpt4 book ai didi

spring - 如何检查Grails Rest Client返回的内容类型

转载 作者:行者123 更新时间:2023-12-02 15:56:43 25 4
gpt4 key购买 nike

我正在使用Grails RestClient测试JSON API的完整性,我想检查请求的返回类型。

有没有更好的方法,然后像这样:

response.responseEntity.headers.getContentType().toString() == "application/json;charset=UTF-8"

最佳答案

responseEntity是spring-framework中的ResponseEntity java类,因此 groovy 无关。

考虑到这一点,看看他们自己的tests,我们可以看到他们本质上只是在做您所做的事情:

MediaType contentType = MediaType.TEXT_PLAIN;
...
HttpHeaders responseHeaders = responseEntity.getHeaders();
...
assertEquals(contentType.toString(), responseHeaders.getFirst("Content-Type"));

他们稍有不同的一件事是使用 MediaType常量;有一个 MediaType.APPLICATION_JSON的值为 application/json,并且json的默认编码已经为UTF-8,您可以将其从内容类型中删除(如果有控制权)。

关于spring - 如何检查Grails Rest Client返回的内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33433140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com