gpt4 book ai didi

json - 如何克服 "Conflicting setter definitions for property "?

转载 作者:行者123 更新时间:2023-12-05 01:19:45 33 4
gpt4 key购买 nike

我使用 com.fasterxml.jacksonio.swagger 库。在我的 REST 端点中,我使用 org.javamoney.moneta.Money 类型进行 GET 查询。部署 war 时,我得到以下异常 1 ;

我已经关注了这个 reference并编写了以下代码[2];并在@ApplicationPath 注册它。但仍然遇到同样的问题。任何指南真的很有帮助吗?

@ApplicationPath("/rest")
public class RestApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> set = new HashSet<Class<?>>();
set.add(com.test.JsonMoneyProvider.class);

[2]

import javax.money.CurrencyUnit;
import javax.money.Monetary;
import javax.money.MonetaryAmountFactory;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.annotation.XmlTransient;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;

@Provider
public class JsonMoneyProvider extends JacksonJsonProvider {
public JsonMoneyProvider() {
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(MonetaryAmountFactory.class, MixIn.class);
setMapper(mapper);
}

public static interface MixIn {
@JsonIgnore
@XmlTransient
MonetaryAmountFactory setCurrency(CurrencyUnit currency);

@JsonIgnore
@XmlTransient
default MonetaryAmountFactory setCurrency(String currencyCode) {
return setCurrency(Monetary.getCurrency(currencyCode));
}
}
}

1

Caused by: java.lang.IllegalArgumentException: Conflicting setter definitions for property "currency": javax.money.MonetaryAmountFactory#setCurrency(1 params) vs javax.money.MonetaryAmountFactory#setCurrency(1 params)
at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getSetter(POJOPropertyBuilder.java:293)
at io.swagger.jackson.ModelResolver.resolve(ModelResolver.java:246)
at io.swagger.jackson.ModelResolver.resolve(ModelResolver.java:127)
at io.swagger.converter.ModelConverterContextImpl.resolve(ModelConverterContextImpl.java:99)
at io.swagger.jackson.ModelResolver.resolveProperty(ModelResolver.java:106)
a

最佳答案

只需在反序列化 setter 方法上使用此注释来指示要使用的 Jackson:@com.fasterxml.jackson.annotation.JsonSetter

关于json - 如何克服 "Conflicting setter definitions for property "?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37452689/

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