简体   繁体   中英

Jackson: need a another method to be a setter

public class UrlTemplate extends AbstractBeanDefinition implements Serializable {

    private List<HeaderField> header = new ArrayList<>();

    @Tag("header")
    public List<HeaderField> getHeader() {
        return this.header;
    }

    @Node("header/header-field")
    public void setHeader(HeaderField headerField) {
        this.header.add(headerField);
    }

    public void setHeader(List<HeaderField> header) {
        this.header = header;
    }
}

Tag and Node annotation are used by other library, and I can't change method setHeader(HeaderField headerField) .

I define a valid setter setHeader(List<HeaderField> header) for serializing, but when I try to serialize UrlTemplate to String and deserialize String to UrlTemplate , throw exception:

com.fasterxml.jackson.databind.JsonMappingException: 
Conflicting setter definitions for property "header": 
com.enniu.crawler.encreeper.core.domain.config.search.UrlTemplate#setHeader(1 params) vs 
com.enniu.crawler.encreeper.core.domain.config.search.UrlTemplate#setHeader(1 params)

I think I may declare setHeader(List<HeaderField> header) to be the setter that Jackson take to deserialize, but I have no idea about how to do it with the Jackson.

Could any one give me some hint?

Try using JsonProperty annotation from jackson

@JsonProperty("header")
private Object headerJson;

public Object getHeaderJson() {
    return status;
}

public void setHeaderJson(Object headerJson) {
    this.headerJson= headerJson;
}

headerJson will be mapped to json header so it should resolve the conflict issue.

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