两个传入REST json的常见dto [英] common dto for two incoming REST json
问题描述
我想创建一个如下所示的公共dto,用于从REST服务接收传入的Manager和Staff详细信息
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
}
问题是对于管理器,项目字段将是一个列表,其中对于员工,它将是一个字符串,所以我为项目创建了两个字段,一个用于接收字符串,另一个用于列表,但它不起作用,我得到无法从VALUE_STRING标记中反序列化java.util.ArrayList的实例
。
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
.
传入的json详细信息如下所示
The incoming json details is like as shown below
经理传入的json
{
"name": "Rohit",
"designation": "Manager",
"item": {"name": "ABC", "desc": "1234"}
}
员工来电json
{
"name": "Manu",
"designation": "Staff",
"item": "abc"
}
任何人都可以告诉我一些解决方案
Can anyone please tell me some solution for this
推荐答案
你可以创建一个铜stomer deserializer。如果item
字段的节点是数组,则将其反序列化为数组,否则将其反序列化为String。例如
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;
}
}
我实际为 项目
。它可以是一个JSON数组作为多个Items,一个JSON对象(这是你在帖子中所拥有的)作为单个Item,或者是一个字符串供员工使用。如果它是一个JSON对象,我只创建单个 Item
并将其添加到 List
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
这是一个完整的测试
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);
}
}
这篇关于两个传入REST json的常见dto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!