Change field case with an ObjectMapper

前端 未结 4 831
伪装坚强ぢ
伪装坚强ぢ 2020-12-30 02:24

I think I need to create a specialist ObjectMapper and cannot find any sample code to start the process.

The creator of the JSON is using .Net

4条回答
  •  时光说笑
    2020-12-30 02:36

    Your first issue can be addressed very simply with the @JsonProperty annotation:

    // java-side class
    public class Facet
    {
        @JsonProperty("Name")
        public String name;
    
        @JsonProperty("Value")
        public String value;
    }
    

    Now the ObjectMapper will match up the differently-cased field names. If you don't want to add annotations into your classes, you can create a Mix-in class to stand in for your Facet:

    public class FacetMixIn
    {
        @JsonProperty("Name")
        public String name;
    
        @JsonProperty("Value")
        public String value;
    }
    
    objectMapper.getDeserializationConfig().addMixInAnnotations(Facet.class, FacetMixIn.class);
    

    This will achieve the same thing, without requiring additional annotations in your Facet class.

提交回复
热议问题