1

Now I am using ObjectMapper to convert a Map to POJO in java, this is my code:

@Override
    public RssSubSourceExtDTO getChannelAnalysisInfo() {
        Map<String, Object> analysisInfo = customRssSubSourceMapper.getSourceInfo(new SubSourceRequest());
        final ObjectMapper mapper = new ObjectMapper();
        final RssSubSourceExtDTO pojo = mapper.convertValue(analysisInfo, RssSubSourceExtDTO.class);
        return pojo;
    }

but the result POJO all properties are null. I have tried to tweak the POJO properties name, and tweak the POJO properties as Object type, but still could not convert value from map to POJO. This is my POJO define:

import java.io.Serializable;

/**
 * @author dolphin
 */
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class RssSubSourceExtDTO implements Serializable {
    /**
     * support increment pull channel count
     * rss_sub_source.id
     *
     * @mbggenerated
     */
    private Object incrementPullChannelCount;

    /**
     * do not support increment pull channel count
     * rss_sub_source.created_time
     *
     * @mbggenerated
     */
    private Long fullPullChannelCount;

    /**
     * 
     * rss_sub_source.editor_pick
     *
     * @mbggenerated
     */
    @ApiModelProperty(value = ")")
    private Long editorPickChannelCount;
}

and this is the debug view now:

enter image description here

what should I do to fix it?

Dolphin
  • 29,069
  • 61
  • 260
  • 539

1 Answers1

2

Looking at the analysisInfo map, the field names do not match the instance variable name in your POJO. For instance, the field name is fullPullChannelCount whereas your map has fullpullchannelcount.

Use @JsonProperty to map the property name in the map to the variable in the POJO.

@JsonProperty("fullpullchannelcount")
private Long fullPullChannelCount;

@JsonProperty("editor_pick_channel_count")
private Long editorPickChannelCount;
....

See: When is the @JsonProperty property used and what is it used for?

Or, you can change the property names in serialized data match the instance variable names.

Thiyagu
  • 17,362
  • 5
  • 42
  • 79