@JsonProperty没有按预期工作 [英] @JsonProperty not working as expected

查看:260
本文介绍了@JsonProperty没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Spring RestTemplate使用restful webservice时出现以下异常

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

com.fasterxml.jackson.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.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:79)
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:555)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:708)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1159)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:315)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:449)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:295)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2094)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readInternal(MappingJackson2HttpMessageConverter.java:123)
... 54 more

响应参数是

{

  "responseBaseParameters":  

{

  "responseCode": "32",

  "responseText": "Invalid Request",

  "incommTxnId": null,

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

 }

}

我的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

推荐答案

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

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

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

此包中的JsonProperty必须不是org.codehaus ...

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

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

总结:jersey客户端1,jackson json属性和删除所有其他jaxrc libs

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

Obs:我使用了jackson,因为已经提供了容器,但是,如果它适用于你,你可以测试另一种组合。

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

这篇关于@JsonProperty没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆