JacksonJSON字段Map是否大写?

x759pob2  于 2022-11-08  发布在  其他
关注(0)|答案(5)|浏览(160)

我不清楚Jackson是如何处理Map字段中的大写的。如果有人能帮助我,我将不胜感激。

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

等等

eit6fx6z

eit6fx6z1#

由于setter方法命名为setMDReqID(…),Jackson假定该变量命名为mDReqID,这是因为Java * 命名约定 *(变量应以小写字母开头).
如果您确实需要大写字母,请在 setter 上使用@JsonProperty注解(或者,对于序列化,请在 getter 上使用),如下所示:

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

ecbunoof2#

你也可以做

@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)

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

mqkwyuun

mqkwyuun3#

在setter上添加与接收到的JSON字符串中的属性名称相匹配的@JsonProperty

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

此外,将@JsonProperty注解添加到 getter 中,以使输出以 * 常规 * 格式显示:

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

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

private String mdReqID;
xggvc2p6

xggvc2p64#

我解决这个问题的方法是:

@Getter
    @Setter
    static class UserInfo {
        //@JsonProperty("UUID")
        private String UUID = "11";
        private String UserName = "22";
        private String userName = "33";
        private String user_Name = "44";
        private String user_name = "55";
        private String User_name = "66";
        private boolean HasDeleted=true;
        private boolean hasDeleted=true;
        private boolean has_Deleted=true;
        private boolean has_deleted=true;
        private boolean HAS_DELETED=true;
    }

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
        objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

        String s = objectMapper.writeValueAsString(new UserInfo());
        System.out.println(s);

        UserInfo userInfo = objectMapper.readValue(s, UserInfo.class);
        System.out.println(objectMapper.writeValueAsString(userInfo));
    }

输出:

{"UUID":"11","UserName":"22","userName":"33","user_Name":"44","user_name":"55","User_name":"66","HasDeleted":true,"hasDeleted":true,"has_Deleted":true,"has_deleted":true,"HAS_DELETED":true}
fbcarpbf

fbcarpbf5#

我遇到了同样的问题,在尝试了UpperCamelCaseStrategy之后,仍然出现了这个错误,该策略使我的字段pContent变为ObjectMapper属性Pcontent,因为不想为每个字段添加@JsonProperty,最后简单地使用gson代替

相关问题