spring—可以将jackson配置为反序列化json键(而不是值)吗?

nnt7mjpx  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(482)

我正在尝试修剪json key 为了避免json请求中出现空格。
json对象看起来像是带空格的(检查“eq”)

{
    "page": 0,
    "size": 25,
    "and":{
        "eq ": [
            {
                "field":"id",
                "value": "60536"
            }
        ]
    }
}

我找到了很多解决方案 SimpleModuleJsonDeserializer 但一般来说,都是在价值方面起作用的。如何修剪键本身,然后将其正确地转换为javapojo?

public class SearchInDTO implements InDTO {

    private Integer pageNo;
    private Integer pageSize;
    private ANDFilter andFilter;

    @JsonProperty("page")
    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    @JsonProperty("size")
    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    @JsonProperty("and")
    public ANDFilter getAndFilter() {
        return andFilter;
    }

    public void setAndFilter(ANDFilter andFilter) {
        this.andFilter = andFilter;
    }

    public static class EQFilter extends FieldValue
    {
        @JsonProperty("field")
        public String getFieldName() {
            return super.getFieldName();
        }

        @JsonProperty("value")
        public String getValue() {
            return super.getValue();
        }

        @Override
        public String toString() {
            final StringBuilder sb = new StringBuilder("EQFilter{");
            sb.append(super.toString());
            sb.append('}');
            return sb.toString();
        }
    }

    public static class FieldValue
    {
        private String fieldName;
        private String value;

        @JsonProperty("field")
        public String getFieldName() {
            return fieldName;
        }

        public void setFieldName(String fieldName) {
            this.fieldName = fieldName;
        }

        @JsonProperty("value")
        public String getValue() {
            if(value == null) return value;
            return value.toLowerCase();
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

    public static class ANDFilter {

        private List<EQFilter> eqFilters = new ArrayList<>();

        @JsonProperty("eq")
        public List<EQFilter> getEqFilters() {
            return eqFilters;
        }

        public void setEqFilters(List<EQFilter> eqFilters) {
            this.eqFilters = eqFilters;
        }
    }
}
xu3bshqb

xu3bshqb1#

使用自定义jsonparser实现的解决方案:

public class MyJsonParser extends JsonParserDelegate {

    public MyJsonParser(JsonParser parser) {
        super(parser);
    }

    @Override
    public String getCurrentName() throws IOException {
        return super.getCurrentName().trim();
    }
}
public class MyJsonParserFactory extends MappingJsonFactory {

    @Override
    protected JsonParser _createParser(InputStream in, IOContext ctxt) throws IOException {
        return new MyJsonParser(super._createParser(in, ctxt));
    }

    @Override
    protected JsonParser _createParser(Reader r, IOContext ctxt) throws IOException {
        return new MyJsonParser(super._createParser(r, ctxt));
    }

    @Override
    protected JsonParser _createParser(char[] data, int offset, int len, IOContext ctxt, boolean recyclable) throws IOException {
        return new MyJsonParser(super._createParser(data, offset, len, ctxt, recyclable));
    }

    @Override
    protected JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException {
        return new MyJsonParser(super._createParser(data, offset, len, ctxt));
    }

    @Override
    protected JsonParser _createParser(DataInput input, IOContext ctxt) throws IOException {
        return new MyJsonParser(super._createParser(input, ctxt));
    }
}
@Component
public class MyJackson2ObjectMapperBuilderCustomizer implements Jackson2ObjectMapperBuilderCustomizer {

    @Override
    public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
        jacksonObjectMapperBuilder.factory(new MyJsonParserFactory());
    }
}

相关问题