【发布时间】:2021-10-11 22:24:05
【问题描述】:
我们的团队正在开发一个 API,该 API 具有一个 PATCH 端点,该端点具有一个空响应和一个空请求正文 - 必要的信息包含在路径参数中。不是我做的,只是想让测试运行。
当我尝试对其进行测试时,我得到了 415 响应,而不是预期的 204。这是我的测试主体:
WebTarget webTarget = ClientBuilder.newClient().target(baseUrl).path(path);
// have also tried this:
// WebTarget webTarget = ClientBuilder.newClient().register(new JacksonJsonProvider()).target(baseUrl).path(path);
webTarget.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
try {
final Response response = invocationBuilder.method("PATCH", Entity.json(objectMapper.writeValueAsString(new Object())));
assertEquals(204, response.getStatus(), "should return 204 empty response");
} catch (JsonProcessingException ex) {
fail();
}
有趣的是,以下两行本身也返回 415 状态:
Invocation.Builder invocationBuilder webTarget.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.method("PATCH");
我在控制台中看到以下错误:
ERROR org.apache.cxf.jaxrs.utils.JAXRSUtils - No message body reader has been found for class java.lang.Object, ContentType: application/json
【问题讨论】: