common dto for two incoming REST json

左心房为你撑大大i 提交于 2020-01-04 05:28:50

问题


I want to create a common dto like as shown below for receiving the incoming Manager and Staff details from a REST service

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
}

The problem is that for for Manager the item field will be a List where as for Staff it will be a string, so I have created two field for item, one for receiving String and another for List, but its not working and I am getting Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token.

The incoming json details is like as shown below

Manager incoming json

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

Staff incoming json

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

Can anyone please tell me some solution for this


回答1:


You could create a customer deserializer. If the node for the "item" field is an array, then deserialize it as an array, otherwise deserialize it as a String. For example

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;
    }
}

I actually added three cases for "item". It could be a JSON array as multiple Items, a JSON object (which is what you have in your post) as a single Item, or a String for the staff. If it's a JSON object, I just create the single Item and added it to a List

Here is a complete test

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);
    }
}



回答2:


You can make Manager and Staff extend an abstract class Employee, and then use the 'designation' JSON property to distinguish them:

// 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() {
    }
}

Tests:

@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));
}


来源:https://stackoverflow.com/questions/33198270/common-dto-for-two-incoming-rest-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!