简体   繁体   中英

Jackson JSON field mapping capitalization?

I'm not clear how jackson deals with capitalization in mapping fields. If anyone could help I'd appreciate it.

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

Above is my json, below is my exception...

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"])

Above is my exception, below is my class...

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

... et cetera

Since your setter method is named setMDReqID(…) Jackson assumes the variable is named mDReqID because of the Java naming conventions (variables should start with lower case letters).

If you really want a capital letter use the @JsonProperty annotation on the setter (or - for serialization - on the getter ) like this:

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

You can also do

@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)

on the class to capitalise all property names in the JSON message

Add @JsonProperty on the setter that matches the property name in your received JSON string:

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

Additionally add @JsonProperty annotation to the getter as well for your output to appear in the conventional format:

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

Now you can name your variable whatever you like:

private String mdReqID;

I solve this problem by:

    @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));
    }

output:

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

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

Use JsonNaming Annotation to get all Class Field Names in Proper Case

Use lombok.Data Annotation to automatically make it work without adding getters and setters in your class

import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;

import lombok.Data;

@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class)
@Data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM