RestTemplate的exchange方法,下划线字段的值未被映射。

huangapple go评论79阅读模式
英文:

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;
}

执行此操作后,我已经收到了messageresult的值,但未映射Test_Id的值。

英文:

To consume a rest service, this is the service response

{
   &quot;message&quot;: &quot;200&quot;,
   &quot;result&quot;: &quot;SUCCESS&quot;,
   &quot;Test_Id&quot;: &quot;23324&quot;
}

Code to consume the service.

ResponseEntity&lt;InfoDto&gt; result = null;
final String uri =&quot;https://app.ed.im/api&quot;;

RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity&lt;String&gt; entity = new HttpEntity&lt;String&gt;(&quot;parameters&quot;, 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(&quot;Test_Id&quot;)
private String Test_Id; // prefer rename to testId

There's probably an underscore to camel-case conversion

huangapple
  • 本文由 发表于 2020年7月26日 17:09:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63098207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定