按SpringJava值筛选json

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

我试图通过值过滤传入的json。这是我通过接收来自jiraapi的巨大json生成的当前json。我有几个pojo类,它们处理响应并只获取我现在需要的字段。但是我需要“fromstring”和“tostring”键,它们只包含少数字符串,比如“to do”、“done”、“qa to do”。我尝试使用filterprovider,但不知道如何按值筛选,然后将其添加到getmapping。
这是我控制器类中的当前getmapping,它创建响应并仅为键“fromstring”筛选项[]:

@GetMapping("/")
    public MappingJacksonValue run() throws Exception {
            IssuesList response = rest.getForObject(
                    "https://.../rest/api/2/search?jql=project="+projectId+ " AND status in (done) AND issuetype in (Story)&expand=changelog",
                    IssuesList.class);

              List<Issues> issuesData = response.getIssuesList();

        MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(issuesData);
        FilterProvider filterProvider = new SimpleFilterProvider().addFilter("itemsEntity", SimpleBeanPropertyFilter.filterOutAllExcept("fromString"));
        mappingJacksonValue.setFilters(filterProvider);

        log.info(mappingJacksonValue.toString());
        return mappingJacksonValue;
    }

我的pojo类包含以下项:

@Data
@JsonFilter("itemsEntity")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Items {

    @JsonProperty("fromString")
    private String fromString;
    @JsonProperty("toString")
    private String toString;

    @Override
    public String toString() {
        return "{" +
                "fromString= " + '\'' + fromString+ '\'' +
                ", toString= " + '\'' + toString +  '\'' +
                '}';
    }
}

这是我从得到的响应中生成的当前json,我希望它跳过所有“fromstring”和“tostring”键,这些键不包含以下内容之一:“to do”、“done”、“qa to do”等。如何实现这一点?

[
    {
        "key": "PE-1322",
        "fields": {
            "issuetype": {
                "name": "Story"
            },
            "created": "2020-11-18T09:16:55.816+0000",
            "customfield_10105": 3
        },
        "changelog": {
            "histories": [
                {
                    "created": "2020-12-17T08:57:28.800+0000",
                    "items": [
                        {
                            "fromString": "This issue relates to TD-353"
                        }
                    ]
                },
                {
                    "created": "2020-12-17T08:57:19.233+0000",
                    "items": [
                        {
                            "fromString": null
                        }
                    ]
                }
            ]
        }
    }
]
woobm2wo

woobm2wo1#

您需要编写自己的过滤器,使用符合您需要的比较器。
例如:

filteredList = Arrays.stream(unfilteredList).filter(listItem -> listItem.contains("To Do")).toArray(String[]::new);

在这种情况下,过滤器将过滤掉任何不包含字符串“to do”的内容。

相关问题