gpt4 book ai didi

spring - RestTemplate忽略@JsonIgnoreProperties(ignoreUnknown = true)批注

转载 作者:行者123 更新时间:2023-12-04 10:04:09 25 4
gpt4 key购买 nike

RestTemplategetForObject方法将忽略类上的@JsonIgnoreProperties(ignoreUnknown = true)批注。

例如。为了

@JsonIgnoreProperties(ignoreUnknown = true)
public class Stock extends AbstractSObject
{
// fields
}


RestTemplate rest = new RestTemplate();
Stock s = rest.getForObject("address", Stock.class);

我收到以下错误

Could not read JSON: Unrecognized field "SomeField" (class sandbox.Stock), not marked as ignorable

最佳答案

Spring的RestTemplate需要com.fasterxml.jackson.annotation.JsonIgnoreProperties

如果您将旧包中的org.codehaus.jackson.annotate.JsonIgnoreProperties注释用于其他目的,则可以手动设置RestTemplate以忽略未知属性:

RestTemplate rest = new RestTemplate();

ObjectMapper lax = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

MappingJacksonHttpMessageConverter c = new MappingJacksonHttpMessageConverter();
c.setObjectMapper(lax);

List<HttpMessageConverter<?>> list = new ArrayList<>();
list.add(c);

rest.setMessageConverters(list);

关于spring - RestTemplate忽略@JsonIgnoreProperties(ignoreUnknown = true)批注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22835106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com