65

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

shaz
  • 2,317
  • 4
  • 27
  • 37

6 Answers6

112

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;
}
informatik01
  • 16,038
  • 10
  • 74
  • 104
nutlike
  • 4,835
  • 1
  • 25
  • 33
  • You wrote "mDReqID " but jackson needs "10 known properties: , mdreqID". All lower why? Im facing same problem, JsonProperty fix it, but why all chars are lower not only first as you mentioned? – Michal Joštiak Nov 14 '18 at 14:26
  • Is there a way to deserialize object correctly, when there is no access to class (it is a dependency)? – WeGa Jan 24 '22 at 13:09
51

You can also do

@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)

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

Marc Enschede
  • 828
  • 10
  • 16
  • 3
    we don't want to do on all properties only on 1 – cyril Dec 27 '19 at 09:43
  • In case that it doesn't work for you just out of the box, remember about setting JacksonAnnotationIntrospector() in your ObjectMapper. In my case I needed to set a pair of introspectors - my custom one and this jackson one. – Borowik Feb 12 '20 at 13:11
  • 2
    try `@JsonNaming(PropertyNamingStrategy.PascalCaseStrategy.class)` if you can't find `UpperCamelCaseStrategy`, it's from `com.fasterxml.jackson.databind.PropertyNamingStrategy` – min Mar 27 '20 at 08:32
  • 3
    Documentation says: `PropertyNamingStrategy.UpperCamelCaseStrategy.class` is deprecated. "Since 2.12 use `PropertyNamingStrategies.UpperCamelCaseStrategy` instead(see: https://github.com/FasterXML/jackson-databind/issues/2715 for reason for deprecation)" – rineez May 18 '22 at 11:28
20

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;
Gayan Weerakutti
  • 11,904
  • 2
  • 71
  • 68
1

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}
light
  • 113
  • 4
0

I face the same problem , after have try UpperCamelCaseStrategy but still this error occurred , the strategy made my field pContent to ObjectMapper property Pcontent, as not want to add @JsonProperty for every field, simply use gson instead at last

towith
  • 109
  • 1
  • 3
0

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
Sujay U N
  • 4,974
  • 11
  • 52
  • 88