英文:
How do I have two identical json property names assigned to two separated fields
问题
{
"content": "我有以下的Java类\n\n\n [![enter image description here][1]][1]\n\n\n\n\n\n需要按以下方式将其序列化为json:\n\n如果列表(paymentTransactionReport字段)不为null,则显示其值 -\n\n{ \n \"paymentTransactionResponse\" : [\n {},\n {}\n ]\n}\n\n如果列表为null,则需要在json字段中显示paymentTransactionReportError,并将其命名为'paymentTransactionResponse',与前面的情况一样。示例 -\n\n{\n \"paymentTransactionResponse\" : {\n ----\n //来自PaymentTransactionReportError类的字段\n ----\n }\n}\n\n如何做到这一点?最好不使用自定义序列化程序。\n如果只使用两个具有相同名称的注释@JsonProperty和JsonInclude.NON_NULL,就像我所做的那样,我会遇到此错误:找不到类型为...的返回值的转换器:...似乎是由于具有相同名称的字段在序列化过程中引发了错误。"
}
英文:
I have the following Java class
I need to serialize it to json in the following way:
if the list(paymentTransactionReport field) is not null display it values -
{
"paymentTransactionResponse" : [
{},
{}
]
}
if the list is null I need to display the paymentTransactionReportError in json field with name 'paymentTransactionResponse', as in previous case. Example -
{
"paymentTransactionResponse" : {
----
//fields from PaymentTransactionReportError class
----
}
}
How can I do this?preferably without custom serializers.
If use just two annotations @JsonProperty with the same name and JsonInclude.NON_NULL as I did, I have this error: No converter found for return value of type:... Seems to be that is a error happened during serialization because of fields with the same name
答案1
得分: 1
以下是翻译好的代码部分:
一种实现方法是使用 [@JsonAnyGetter][1],请尝试以下代码:
public class TestDTO {
@JsonIgnore
List<String> paymentTransactionResponse;
@JsonIgnore
String paymentTransactionResponseError;
public List<String> getPaymentTransactionResponse() {
return paymentTransactionResponse;
}
public void setPaymentTransactionResponse(List<String> paymentTransactionResponse) {
this.paymentTransactionResponse = paymentTransactionResponse;
}
public String getPaymentTransactionResponseError() {
return paymentTransactionResponseError;
}
public void setPaymentTransactionResponseError(String paymentTransactionResponseError) {
this.paymentTransactionResponseError = paymentTransactionResponseError;
}
@JsonAnyGetter
public Map<String, Object> getData(){
Map<String, Object> map = new HashMap<String, Object>();
if(paymentTransactionResponse != null) {
map.put("paymentTransactionResponse", paymentTransactionResponse);
} else {
map.put("paymentTransactionResponse", paymentTransactionResponseError);
}
return map;
}
}
[1]: https://fasterxml.github.io/jackson-annotations/javadoc/2.9/com/fasterxml/jackson/annotation/JsonAnyGetter.html
英文:
One way you can achieve this is, using @JsonAnyGetter, Try this
public class TestDTO {
@JsonIgnore
List<String> paymentTransactionResponse;
@JsonIgnore
String paymentTransactionResponseError;
public List<String> getPaymentTransactionResponse() {
return paymentTransactionResponse;
}
public void setPaymentTransactionResponse(List<String> paymentTransactionResponse) {
this.paymentTransactionResponse = paymentTransactionResponse;
}
public String getPaymentTransactionResponseError() {
return paymentTransactionResponseError;
}
public void setPaymentTransactionResponseError(String paymentTransactionResponseError) {
this.paymentTransactionResponseError = paymentTransactionResponseError;
}
@JsonAnyGetter
public Map<String, Object> getData(){
Map<String, Object> map = new HashMap<String, Object>();
if(paymentTransactionResponse != null) {
map.put("paymentTransactionResponse", paymentTransactionResponse);
}else {
map.put("paymentTransactionResponse", paymentTransactionResponseError);
}
return map;
}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论