Jackson JSON字段如何映射大小写?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (119)

{"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;
    }

...等等

提问于
用户回答回答于

由于setter方法被命名为setMDReqID(…)Jackson,因为Java命名约定(变量应以小写字母开头),所以该变量被命名为mDReqID。如果你真的想要一个大写字母,那么在setter上使用@JsonProperty注解(或 - 对于序列化 - 在getter上)如下所示:@JsonProperty("MDReqID")

用户回答回答于

添加@JsonProperty与收到的Json字符串中的属性名称相匹配的setter。

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

此外@JsonProperty,还要为getter 添加注释以使输出以传统格式显示。

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

无论你喜欢什么,你都可以为变量命名。

private String mdReqID;

扫码关注云+社区