58

我不清楚杰克逊如何处理映射领域的大写。如果有人可以提供帮助,我将不胜感激。

{"user":{"username":"user@host.com","password":"pwd","sendercompid":"COMPID","service":{"host":"address","port":6666,"service":"S1","serviceAsString":"s1"}},"MDReqID":"ghost30022","NoRelatedSym":1,"Symbol":["GOOG"],"MarketDepth":"0","NoMDEntryTypes":3,"MDEntryType":["0","1","2"],"SubscriptionRequestType":"1","AggregatedBook":"N"}:

上面是我的json,下面是我的例外......

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "MDReqID" (class com.myco.qa.fixrest.MarketDataRequest), not marked as ignorable (10 known properties: , "mdreqID", "marketDepth", "user", "subscriptionRequestType", "aggregatedBook", "mdentryType", "symbol", "mdupdateType", "noRelatedSym", "noMDEntryTypes"])

上面是我的例外,下面是我的课......

public class MarketDataRequest {
    private User user;
    private String MDReqID;
    private char SubscriptionRequestType;
    private int MarketDepth;
    private int MDUpdateType;
    private char AggregatedBook;
    private int NoMDEntryTypes;
    private ArrayList<Character> MDEntryType;
    private int NoRelatedSym;
    private ArrayList<String> Symbol;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getMDReqID() {
        return MDReqID;
    }

    public void setMDReqID(String MDReqID) {
        this.MDReqID = MDReqID;
    }

    public char getSubscriptionRequestType() {
        return SubscriptionRequestType;
    }

    public void setSubscriptionRequestType(char subscriptionRequestType) {
        SubscriptionRequestType = subscriptionRequestType;
    }

...等等

4

4 回答 4

101

由于您的 setter 方法被命名setMDReqID(…),Jackson 假定变量是mDReqID因为 Java命名约定而命名的(变量应以小写字母开头)。

如果您真的想要大写字母,请在setter上使用@JsonProperty注释(或 - 对于序列化 - 在getter上),如下所示:

@JsonProperty("MDReqID")
public void setMDReqID(String MDReqID) {
    this.MDReqID = MDReqID;
}
于 2013-03-08T21:02:05.390 回答
40

你也可以做

@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)

在类上将 JSON 消息中的所有属性名称大写

于 2019-01-31T12:54:43.957 回答
16

添加@JsonProperty与接收到的 JSON 字符串中的属性名称匹配的设置器:

@JsonProperty("MDReqID")
public void setMDReqID(String MDReqID) {
    this.MDReqID = MDReqID;
}

此外,还要向getter添加@JsonProperty注释,以便您的输出以常规格式显示:

@JsonProperty("mDReqID")
public String getMDReqID() {
    return MDReqID;
}

现在您可以随意命名变量:

private String mdReqID;
于 2016-12-11T17:12:49.963 回答
0

我面临同样的问题,尝试后UpperCamelCaseStrategy仍然出现此错误,该策略将我的字段pContent设置为 ObjectMapper 属性Pcontent,因为不想为每个字段添加@JsonProperty,gson最后只需使用

于 2020-09-12T03:44:37.913 回答