[英]JSON deserialization for property starting with underscore (“_id”) not working
Posting the question as I can't find solution specific to below problem. 发布问题,因为我找不到下面问题的解决方案。
One of the 3rd part REST service we are consuming return response similar to REST服务的第三部分之一,我们使用的返回响应类似于
{ "_id": "d55eb7c0", "applicationType": "TEST", "applicationId": "uxhJ1hcT1F8bpL3xAWvTjsymNcd1RArv", "description": "Some Description"}
tried to map "_id" attribute as, to follow proper java bean naming 尝试将“ _id”属性映射为,以遵循正确的Java bean命名
@JsonProperty("_id")
private String id;
which results in following error. 这导致以下错误。
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class ] and content type [application/json;charset=utf-8]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:917)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:901)
if the field name is private String _id;
如果字段名称是
private String _id;
json response is de-serialised without any issue. json响应已反序列化,没有任何问题。 but I don't want to name field starting with '_' in our app.
但我不想在我们的应用中命名以“ _”开头的字段。 We are using Jackson-2.7.3 & Spring RestTemplate to make REST calls.
我们正在使用Jackson-2.7.3和Spring RestTemplate进行REST调用。
Implementation for consuming the endpoint. 消费端点的实现。 Its a simple GET call using Spring RestTemplate.
使用Spring RestTemplate进行简单的GET调用。
HttpEntity<String> requestEntity = new HttpEntity<>(httpHeaders); return restTemplate.exchange(authorizationProperties.getEndpoint() + "/roles", HttpMethod.GET, requestEntity,new ParameterizedTypeReference<Auth0Roles>() {});
Thanks in advance. 提前致谢。
I guess you need to apply the annotation on the accessor methods, not the field itself: 我想您需要将注释应用于访问器方法,而不是字段本身:
private String id;
@JsonProperty("_id")
public String getId() {
return this.id;
}
public void setId(String i) {
this.id = i;
}
Its not looking like the problem with JSON. 它看起来不像JSON的问题。 Most probably you are missing the HttpMessageConverter.
最有可能您缺少HttpMessageConverter。
Try Adding this dependency 尝试添加此依赖项
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.