杰克逊,如何使用自定义名称反序列化字段?

时间:2017-04-07 13:36:12

标签: java json jackson

我尝试将REST API中的Jira问题反序列化为对象。多数民众赞成直截了当。我努力的地方是将Jira中的自定义字段映射到属性上。我尝试过使用自定义反序列化程序,但它没有“启动”。

这是REST调用中的Json的样子: (某些部分被剥离)

{
  "expand": "renderedFields,names,schema,...",
  "id": "53899",
  "key": "DPT-12",
  "fields": {
    "issuetype": {
      "id": "10001",
      "name": "Story",
      "subtask": false
    },
    "timespent": null,
    "project": {
      "id": "10823",
      "key": "DPT"
    },
    "fixVersions": [],
    "customfield_10111": null,
    "aggregatetimespent": null,
    "resolution": null,
    "customfield_10112": null,
    "customfield_10700": [
      "entwicklung-w"
    ],
    "customfield_10304": null,
    "resolutiondate": null,
    "lastViewed": "2017-04-04T14:34:19.868+0200",
    "created": "2017-02-02T12:01:31.443+0100",
    "priority": {
      "name": "Schwer",
      "id": "10001"
    },
    "assignee": {
      "displayName": "me :-)"
    },
    "updated": "2017-04-04T14:34:19.710+0200",
    "status": {
      "iconUrl": "https://jira.mobi.ch/",
      "name": "Backlog",
      "statusCategory": {
        "name": "Aufgaben"
      }
    },
    "summary": "Ereignisse in rocket Chat schreiben",
    "creator": {
      "displayName": "me :-)"
    },
    "reporter": {
      "displayName": "me :-)"
    }
  }
}

我的应用程序(“customfield_10700”)中配置了自定义字段名称,我想将其映射到属性上:

private Set<String> deploymentEnvironments;

所以这里是相关的Dto和测试类(在这里剥离了getter和setter)。

测试:

import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Set;

import com.fasterxml.jackson.databind.module.SimpleModule;
import org.junit.Test;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class IssueFieldsWithDeserializerTest {

    @Test
    public void testJiraResponseDeserializer() throws IOException, URISyntaxException {
        // arrange
        String deploymentEnvsKey = "customfield_10700";

        String json = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader().getResource("jira-example-issue-with-customfield-poc.json").toURI())));

        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        SimpleModule module = new SimpleModule();
        module.addDeserializer(Set.class, new CustomFieldDeserializer(deploymentEnvsKey));
        mapper.registerModule(module);


        // act
        IssueResponsePoc issue = mapper.readValue(json, IssueResponsePoc.class);


        // assert
        assertThat("issue is not null", issue, is(not(nullValue())));
        assertThat("fields are not null", issue.getFields(), is(not(nullValue())));
        assertThat("custom field is not null", issue.getFields().getDeploymentEnvironments(), is(not(nullValue())));
        assertThat("custom field is not empty", issue.getFields().getDeploymentEnvironments(), is(not(empty())));
        assertThat("custom field has one value", issue.getFields().getDeploymentEnvironments(), hasSize(1));
    }

}

IssueResponsePoc类:

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonProperty;

public class IssueResponsePoc implements Serializable {

    @JsonProperty private String id;
    @JsonProperty private String key;

    @JsonProperty private IssueFieldsPoc fields;

}

有趣的课程:IssueFieldsPoc

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Set;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import com.fasterxml.jackson.annotation.JsonProperty;

public class IssueFieldsPoc implements Serializable {

    @JsonProperty private String summary;
    @JsonProperty private IssueType issuetype;
    @JsonProperty private IssueUser creator;
    @JsonProperty private Date created;
    @JsonProperty private IssueUser reporter;
    @JsonProperty private IssuePriority priority;
    @JsonProperty private IssueResolution resolution;
    @JsonProperty private List<String> labels;
    @JsonProperty private Date resolutiondate;
    @JsonProperty private IssueUser assignee;
    @JsonProperty private Date updated;
    @JsonProperty private IssueStatus status;

    @JsonDeserialize private Set<String> deploymentEnvironments;
    // @JsonDeserialize(using = CustomFieldDeserializer.class) private Set<String> deploymentEnvironments;

    public Set<String> getDeploymentEnvironments() {
        return deploymentEnvironments;
    }

    public void setDeploymentEnvironments(Set<String> deploymentEnvironments) {
        this.deploymentEnvironments = deploymentEnvironments;
    }
}

我的解串器:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;

public class CustomFieldDeserializer extends StdDeserializer<Set<String>> {

    private final String customFieldName;

    public CustomFieldDeserializer(String customFieldName) {
        super((Class<?>) null);
        this.customFieldName = customFieldName;
    }

    @Override
    public Set<String> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        System.out.println("deserializer started!");
        return null;
    }

    @Override
    public Collection<Object> getKnownPropertyNames() {
        return Collections.singletonList(customFieldName);
    }
}

我尝试注册一个自定义反序列化器,但它没有启动,我怀疑它被忽略,因为jackson无法识别字段名称。添加“getKnownPropertyNames”方法没有帮助。因为我需要将jira自定义字段名称(我从配置中读取)放在某处,我试图将其放入反序列化程序中。使用jackson注释@JsonDeserialize。

我也尝试将它包装到另一个类中,而不是直接使用Set来获得更强的输入。也没有运气。

我也尝试在注释中配置反序列化器,但这需要一个默认的构造函数,我不能再配置jira自定义字段名。

当前解决方案使用@JsonAnySetter注释:

@JsonAnySetter
public void setCustomProperty(String name, Object value) {
    if(StringUtils.startsWith(name, "customfield_")) {
        this.customFields.put(name, value);
    }
}

但我更喜欢在反序列化器中使用该逻辑。

有没有办法帮助jackson何时启动这个动态属性名称的反序列化器(因为它知道属性名称)?

更新: 将模块注册到映射器。 正如答案中所建议的那样,在字段中添加确切的属性名称:

@JsonProperty("customfield_10700")
@JsonDeserialize
private Set<String> deploymentEnvironments;

将允许解串器启动。但如上所述,这是一个可配置的值,我不能直接在映射代码中放置(或者我不想)。

2 个答案:

答案 0 :(得分:0)

好吧,如果我理解正确,你需要将json转换为java对象。

如果您希望该类忽略未知属性,则需要向您的类添加@JsonIgnoreProperties(ignoreUnknown = true),这些类必须忽略(仅IssueResponsePocIssueFieldsPoc)。

@JsonProperty(value = <name_of_property_in_json>)中,您可以在java类中使用字段的任何名称。

如果您通过具有相应注释(@JsonProperty@JsonIgnore等)的java类重复嵌套级别的json,则不需要使用整个反序列化器。

如果您想要处理班级中的未知字段,可以将@JsonAnySetter用于此目的

答案 1 :(得分:0)

我认为您可以通过将@JsonProperty("customfield_10700")设置为字段deploymentEnvironments来解决您的问题,如下所示。在这种情况下,您不需要自定义反序列化器。

public class IssueFieldsPoc implements Serializable {

    @JsonProperty private String summary;
    @JsonProperty private Date created;
    @JsonProperty private List<String> labels;
    @JsonProperty private Date resolutiondate;

    @JsonProperty private Date updated;

    @JsonProperty("customfield_10700")
    private Set<String> deploymentEnvironments;