Java @JsonProperty 未按预期工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21125173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:46:41  来源:igfitidea点击:

@JsonProperty not working as expected

javaspringrestHymanson

提问by Ayodeji

I get the following exception when i consume a restful webservice using Spring RestTemplate

当我使用 Spring RestTemplate 使用一个安静的网络服务时,我得到以下异常

com.fasterxml.Hymanson.databind.exc.UnrecognizedPropertyException: Unrecognized field "IMP-SourceTxnId" (class com.model.ResponseBaseParameters) not marked as ignorable (4 known properties: , "sourceTxnId", "incommTxnId", "responseCode", "responseText"])

at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@2f2ddd7c; line: 1, column: 130] (through reference chain: com.incomm.ife.model.rogers.RogersTransactionResponse["responseBaseParameters"]->com.incomm.ife.model.rogers.ResponseBaseParameters["IMP-SourceTxnId"])
at com.fasterxml.Hymanson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:79)
at com.fasterxml.Hymanson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:555)
at com.fasterxml.Hymanson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:708)
at com.fasterxml.Hymanson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1159)
at com.fasterxml.Hymanson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:315)
at com.fasterxml.Hymanson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.Hymanson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:449)
at com.fasterxml.Hymanson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98)
at com.fasterxml.Hymanson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:295)
at com.fasterxml.Hymanson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.Hymanson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.Hymanson.databind.ObjectMapper.readValue(ObjectMapper.java:2094)
at org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter.readInternal(MappingHymanson2HttpMessageConverter.java:123)
... 54 more

The response parameter is

响应参数是

{

  "responseBaseParameters":  

{

  "responseCode": "32",

  "responseText": "Invalid Request",

  "incommTxnId": null,

  "IMP-SourceTxnId": "551932ba-6af4-44f9-ab98-db5bc96e962b"

 }

}

and my POJO class is

我的 POJO 课程是

public class ResponseBaseParameters {

private String responseCode;

private String responseText;
private String incommTxnId;
@JsonProperty("IMP-SourceTxnId")
private String sourceTxnId;

public String getResponseCode() {
    return responseCode;
}

public void setResponseCode(String responseCode) {
    this.responseCode = responseCode;
}

public String getResponseText() {
    return responseText;
}

public void setResponseText(String responseText) {
    this.responseText = responseText;
}

public String getIncommTxnId() {
    return incommTxnId;
}

public void setIncommTxnId(String incommTxnId) {
    this.incommTxnId = incommTxnId;
}

public String getSourceTxnId() {
    return sourceTxnId;
}

public void setSourceTxnId(String sourceTxnId) {
    this.sourceTxnId = sourceTxnId;
}

}

Please any insight as to why i am getting this error. Thanks

请了解为什么我会收到此错误。谢谢

回答by Josiel Novaes

There are many implementations of "REST API" and they conflict with each other. After a lot of time, I resolved it with this configuration:

“REST API”有很多实现,它们相互冲突。很多时间后,我用这个配置解决了它:

import com.fasterxml.Hymanson.annotation.JsonProperty;
...
@JsonProperty("cep")
private String cep;

Must have that JsonProperty with this package not org.codehaus...

必须有这个包的 JsonProperty 而不是 org.codehaus ......

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.19</version>
    <scope>test</scope>
</dependency>

In summary: jersey client 1, Hymanson json property and remove all others jaxrc libs.

总结:jersey client 1, Hymanson json property 并删除所有其他 jaxrc libs

Obs:. I used Hymanson because the container already provided, but, if it works for you, you could test another combinations.

奥布斯:。我使用 Hymanson 是因为容器已经提供,但是,如果它适合您,您可以测试其他组合。