我正在尝试通过Spring的HTTP POST请求发送com.google.cloud.datastore.Key
对象,但是当我收到响应并尝试使用Spring ObjectMapper
恢复密钥对象时,抛出了该异常:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.google.cloud.datastore.Key` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
注意:接口com.google.cloud.datastore.Key
间接实现Serializable
,并已由Spring正确序列化。
这是有问题的代码:
@Autowired private MockMvc mvc;
@Autowired private ObjectMapper mapper;
// ...
String responseAsString =
this.mvc
.perform(
post("/universe-history-clone")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(this.mapper.writeValueAsString(cloneData)))
.andExpect(status().is(200))
.andExpect(jsonPath("success").value(true))
.andReturn()
.getResponse()
.getContentAsString();
UniverseHistoryClone.Response cloneResponse =
this.mapper.readValue(responseAsString, UniverseHistoryClone.Response.class);
这是简化的响应POJO类:
public interface UniverseHistoryClone {
Response clone(Data data);
class Data extends ServiceData {
private Long originalAccountId;
private Long originalUniverseId;
private Long destinationAccountId;
private Long destinationUniverseId;
}
class Response {
private boolean success;
private Key newHistoryKeyId;
private Key oldHistoryKeyId;
private String errorCause;
}
}
这是HTTP响应:
MockHttpServletResponse:
Status = 200
Error message = null
Headers = {Content-Type=[application/json;charset=UTF-8]}
Content type = application/json;charset=UTF-8
Body = {"success":true,"newHistoryKeyId":{"projectId":"<hidden>","namespace":"<hidden>","nameOrId":<hidden>,"id":<hidden>,"kind":"<hidden>","ancestors":[]},"oldHistoryKeyId":{"projectId":"<hidden>","namespace":"","nameOrId":<hidden>,"id":<hidden>,"kind":"<hidden>","ancestors":[]}}
Forwarded URL = null
Redirected URL = null
Cookies = []
还有另一种方法可以根据响应字符串构建密钥吗?