两个传入 REST 的公共 dto json

common dto for two incoming REST json

我想创建一个如下所示的公共 dto,用于从 REST 服务接收传入的经理和员工详细信息

public class Employee {

    @JsonProperty("name")
    public String name;

    @JsonProperty("designation")
    public String designation;

    @JsonProperty("item")
    public String item;

    @JsonProperty("item")
    public List<Item> items;

    //setters and getters
}

问题是,对于经理来说,项目字段将是一个列表,而对于员工来说,它将是一个字符串,所以我为项目创建了两个字段,一个用于接收字符串,另一个用于列表,但它不是工作,我得到 Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token.

传入json详情如下图

经理来人json

{
  "name": "Rohit",
  "designation": "Manager",
  "item": {"name": "ABC", "desc": "1234"}
}

工作人员来访json

{
  "name": "Manu",
  "designation": "Staff",
  "item": "abc"
}

谁能告诉我一些解决方案

您可以创建一个客户解串器。如果 "item" 字段的节点是数组,则将其反序列化为数组,否则将其反序列化为字符串。例如

public static class EmployeeDeserializer extends JsonDeserializer<Employee> {

    @Override
    public Employee deserialize(JsonParser jp,
            DeserializationContext dc)
            throws IOException, JsonProcessingException {
        Employee emp = new Employee();
        JsonNode root = jp.getCodec().readTree(jp);
        emp.name = root.get("name").asText();
        emp.designation = root.get("designation").asText();
        JsonNode itemNode = root.get("item");
        if (itemNode.isArray()) {
            ArrayNode itemsNode = (ArrayNode) itemNode;
            List<Item> items = new ArrayList<>();
            for (JsonNode iNode : itemsNode) {
                Item item = new Item();
                item.name = iNode.get("name").asText();
                item.desc = iNode.get("desc").asText();
                items.add(item);
            }
            emp.items = items;
        } else if (itemNode.isObject()) {
            List<Item> items = new ArrayList<>();
            Item item = new Item();
            item.name = itemNode.get("name").asText();
            item.desc = itemNode.get("desc").asText();
            items.add(item);
            emp.items = items;
        } else {
            String item = root.get("item").asText();
            emp.item = item;
        }
        return emp;
    }
}

我实际上为"item"添加了三个案例。它可以是作为多个项目的 JSON 数组,作为单个项目的 JSON 对象(这是您在 post 中拥有的对象),或者是员工的字符串。如果它是一个 JSON 对象,我只创建单个 Item 并将其添加到 List

这是一个完整的测试

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ArrayNode;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.BeforeClass;

public class EmployeeTest {

    @JsonDeserialize(using = EmployeeDeserializer.class)
    public static class Employee {

        public String name;
        public String designation;
        public String item;
        public List<Item> items;
    }

    public static class Item {

        public String name;
        public String desc;

        @Override
        public String toString() {
            return "Item{" + "name=" + name + ", desc=" + desc + '}';
        }
    }

    public static class EmployeeDeserializer extends JsonDeserializer<Employee> {

        @Override
        public Employee deserialize(JsonParser jp,
                DeserializationContext dc)
                throws IOException, JsonProcessingException {
            Employee emp = new Employee();
            JsonNode root = jp.getCodec().readTree(jp);
            emp.name = root.get("name").asText();
            emp.designation = root.get("designation").asText();
            JsonNode itemNode = root.get("item");
            if (itemNode.isArray()) {
                ArrayNode itemsNode = (ArrayNode) itemNode;
                List<Item> items = new ArrayList<>();
                for (JsonNode iNode : itemsNode) {
                    Item item = new Item();
                    item.name = iNode.get("name").asText();
                    item.desc = iNode.get("desc").asText();
                    items.add(item);
                }
                emp.items = items;
            } else if (itemNode.isObject()) {
                List<Item> items = new ArrayList<>();
                Item item = new Item();
                item.name = itemNode.get("name").asText();
                item.desc = itemNode.get("desc").asText();
                items.add(item);
                emp.items = items;
            } else {
                String item = root.get("item").asText();
                emp.item = item;
            }
            return emp;
        }
    }

    private static ObjectMapper mapper;

    @BeforeClass
    public static void setUpMapper() {
        mapper = new ObjectMapper();
        //SimpleModule module = new SimpleModule();
        //module.addDeserializer(Employee.class, new EmployeeDeserializer());
        //mapper.registerModule(module);
    }

    @Test
    public void should_deserialize_manager_list_ok() throws Exception {
        final String mgrJson
                = "{\n"
                + "  \"name\": \"Rohit\",\n"
                + "  \"designation\": \"Manager\",\n"
                + "  \"item\": [{\"name\": \"ABC\", \"desc\": \"1234\"}]\n"
                + "}";
        Employee mgr = mapper.readValue(mgrJson, Employee.class);
        assertEquals("Rohit", mgr.name);
        assertEquals("Manager", mgr.designation);
        assertNull(mgr.item);
        assertEquals(1, mgr.items.size());
        assertEquals("ABC", mgr.items.get(0).name);
        assertEquals("1234", mgr.items.get(0).desc);
    }

    @Test
    public void should_deserialize_staff_string_ok() throws Exception {

        final String staffJson
                = "{\n"
                + "  \"name\": \"Manu\",\n"
                + "  \"designation\": \"Staff\",\n"
                + "  \"item\": \"abc\"\n"
                + "}";
        Employee staff = mapper.readValue(staffJson, Employee.class);
        assertEquals("Manu", staff.name);
        assertEquals("Staff", staff.designation);
        assertEquals("abc", staff.item);
        assertNull(staff.items);
    }

    @Test
    public void should_deserialize_single_item_ok() throws Exception {
        final String mgrJson
                = "{\n"
                + "  \"name\": \"Rohit\",\n"
                + "  \"designation\": \"Manager\",\n"
                + "  \"item\": {\"name\": \"ABC\", \"desc\": \"1234\"}\n"
                + "}";
        Employee mgr = mapper.readValue(mgrJson, Employee.class);
        assertEquals("Rohit", mgr.name);
        assertEquals("Manager", mgr.designation);
        assertNull(mgr.item);
        assertEquals(1, mgr.items.size());
        assertEquals("ABC", mgr.items.get(0).name);
        assertEquals("1234", mgr.items.get(0).desc);
    }
}

你可以让Manager和Staff扩展一个抽象的classEmployee,然后用'designation'JSON属性来区分他们:

// note hashCode(), equals() and toString() methods left out for brevity!

@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "designation")
@JsonSubTypes({ @JsonSubTypes.Type(Staff.class), @JsonSubTypes.Type(Manager.class) })
public static abstract class Employee {
    @JsonProperty("name")
    public String name;

    public Employee(String name) {
        this.name = name;
    }

    public Employee() {
    }
}

@JsonTypeName("Manager")
public static final class Manager extends Employee {
    @JsonProperty("item")
    public List<Item> items;

    public Manager(String name, List<Item> items) {
        super(name);
        this.items = items;
    }

    public Manager() {
    }

    public static final class Item {
        public final String name;
        public final String desc;

        public Item(@JsonProperty("name") String name, @JsonProperty("desc") String desc) {
            this.name = name;
            this.desc = desc;
        }
    }
}

@JsonTypeName("Staff")
public static final class Staff extends Employee {
    @JsonProperty("item")
    public String item;

    public Staff(String name) {
        super(name);
        this.item = item;
    }

    public Staff() {
    }
}

测试:

@Test
public void polymorphic_deserialization_of_manager() throws Exception {
    ObjectMapper mapper = new ObjectMapper().enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES)
            .enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES)
            .enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
            .enable(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
    String json = "{ name: 'Rohit', designation: 'Manager', item: { name: 'ABC', desc: '1234' } }";
    Employee employee = new Manager("Rohit", ImmutableList.of(new Manager.Item("ABC", "1234")));
    assertThat(mapper.readValue(json, Employee.class), equalTo(employee));
    assertThat(mapper.writeValueAsString(employee), equivalentTo(json));
}

@Test
public void polymorphic_deserialization_of_staff() throws Exception {
    ObjectMapper mapper = new ObjectMapper().enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES).enable(
            JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
    String json = "{ name: 'Manu', designation: 'Staff', item: 'abc' }";
    Employee employee = new Staff("Manu", "abc");
    assertThat(mapper.readValue(json, Employee.class), equalTo(employee));
    assertThat(mapper.writeValueAsString(employee), equivalentTo(json));
}