英文:
RestTemplate exchange, values are not mapped for a field with underscore
问题
以下是翻译好的部分:
消费REST服务,这是服务的响应:
{
"message": "200",
"result": "SUCCESS",
"Test_Id": "23324"
}
用于消费该服务的代码:
ResponseEntity<InfoDto> result = null;
final String uri = "https://app.ed.im/api";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
result = restTemplate.exchange(uri, HttpMethod.GET, entity, InfoDto.class);
这是DTO:
public class InfoDto implements Serializable {
private String message;
private String result;
private String Test_Id;
}
执行此操作后,我已经收到了message和result的值,但未映射Test_Id的值。
英文:
To consume a rest service, this is the service response
{
"message": "200",
"result": "SUCCESS",
"Test_Id": "23324"
}
Code to consume the service.
ResponseEntity<InfoDto> result = null;
final String uri ="https://app.ed.im/api";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
result = restTemplate.exchange(uri, HttpMethod.GET, entity, InfoDto.class);
This is the dto
public class InfoDto implements Serializable {
private String message;
private String result;
private String Test_Id;
}
Once this is executed
I had received the values of message and result, but Test_Id value is not mapped.
答案1
得分: 2
如果参数名称的代码约定有问题,添加精确匹配的JsonProperty
@JsonProperty("Test_Id")
private String Test_Id; // 最好改为testId
可能存在下划线转驼峰的转换
英文:
If there is an issue with parameter name's code convention, add JsonProperty with exact match
@JsonProperty("Test_Id")
private String Test_Id; // prefer rename to testId
There's probably an underscore to camel-case conversion
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论