英文:
Read date as an array integer from json and map with POJO as an integer
问题
以下是翻译好的内容:
从 REST API 获取 JSON 数据,并希望将数据存储在 POJO(普通 Java 对象)的列表中。以下是相应的代码:
public List<MyObject> mapper(){
String myObjectData = restClient.getAllOriginal("myObject");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
List<MyObject> resultDto = null;
try
{
Map<String, List<MyObject>> root = mapper.readValue(jsonString, new TypeReference<Map<String, List<MyObject>>>() {});
List<MyObject> objects = root.get("myObject");
}
catch (JsonParseException e)
{
e.printStackTrace();
}
catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return resultDto;
}
从 REST API 获取字符串的方法:
public String getAllOriginal(String resourcePath) {
// Objects.requireNonNull(this.baseUri, "target cannot be null");
return this.client
.target("http://comtsrvc.ny.qa.flx.nimbus.gs.com:3802/v2/")
.path(resourcePath)
.request(MediaType.APPLICATION_JSON_TYPE)
.cookie("mySSO", getCookie())
.get()
.readEntity(String.class);
}
以下是我的 JSON 数据:
{
"myObject" : [ {
"key" : {
"srcSys" : "REPO_1",
"srcSysRef" : "20200909_1911_1"
},
"productData" : {
"id" : null,
"number" : null,
"isn" : null,
"productId" : null,
"productAdditionalData" : {
"assetClassTree" : "UNCLASSIFIED",
"description" : "UNCLASSIFIED",
"productTypeData" : {
"productType" : "UNCLASSIFIED",
"productGroup" : "UNCLASSIFIED"
}
}
},
"state" : "OPEN",
"type" : "01"
}, {
"key" : {
"srcSys" : "REPO_2",
"srcSysRef" : "20200403_3892_1"
},
"productData" : {
"id" : "1",
"number" : "11",
"isn" : "null",
"productId" : 1234,
"productAdditionalData" : {
"assetClassTree" : "xyz",
"description" : "abc",
"productTypeData" : {
"productType" : "UNCLASSIFIED",
"productGroup" : "UNCLASSIFIED"
}
}
},
"state" : "OPEN",
"startDate" : [2020, 9, 22],
"endDate" : [2020, 9, 24],
"tradAcctType" : "01"
} ]
}
鉴于我不能修改现有的 POJO(除非必要),因为这需要很多修改。我对如何从数组中访问日期并将其映射到具有整数数据类型的 POJO(其中 startDate 具有整数数据类型)感到困惑。
英文:
I'm getting json from rest api and I want to store the data in list of POJO. Below is the codefor the same:
public List<myObject> mapper(){
String myObjectData= restClient.getAllOriginal("myObject");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
List<CommitmentPojo> resultDto = null;
try
{
Map<String, List<MyObject>> root = mapper.readValue(jsonString, new TypeReference<Map<String, List<MyObject>>>() {});
List<MyObject> objects = root.get("myObject");
}
catch (JsonParseException e)
{
e.printStackTrace();
}
catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return resultDto;
}
Method which gets the string from rest api:
public String getAllOriginal(String resourcePath) {
// Objects.requireNonNull(this.baseUri, "target cannot be null");
return this.client
.target("http://comtsrvc.ny.qa.flx.nimbus.gs.com:3802/v2/")
.path(resourcePath)
.request(MediaType.APPLICATION_JSON_TYPE)
.cookie("mySSO", getCookie())
.get()
.readEntity(String.class);
}
Below is my json:
{
"myObject" : [ {
"key" : {
"srcSys" : "REPO_1",
"srcSysRef" : "20200909_1911_1"
},
"productData" : {
"id" : null,
"number" : null,
"isn" : null,
"productId" : null,
"productAdditionalData" : {
"assetClassTree" : "UNCLASSIFIED",
"description" : "UNCLASSIFIED",
"productTypeData" : {
"productType" : "UNCLASSIFIED",
"productGroup" : "UNCLASSIFIED"
}
}
},
"state" : "OPEN",
"type" : "01"
}, {
"key" : {
"srcSys" : "REPO_2",
"srcSysRef" : "20200403_3892_1"
},
"productData" : {
"id" : "1",
"number" : "11",
"isn" : "null",
"productId" : 1234,
"productAdditionalData" : {
"assetClassTree" : "xyz",
"description" : "abc",
"productTypeData" : {
"productType" : "UNCLASSIFIED",
"productGroup" : "UNCLASSIFIED"
}
}
},
"state" : "OPEN",
"startDate" : [2020, 9, 22],
"endDate" : [2020, 9, 24],
"tradAcctType" : "01"
} ]
}
Provided that I cannot modify the existing POJO(unless and until required) as it will need a lot of modifications.
I'm confused how to access date from the array and map it to POJO where startDate has integer data type.
答案1
得分: 2
以下是翻译好的部分:
import java.util.List;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Test {
private String state;
private int startDate;
private int endDate;
@JsonCreator
public Test(@JsonProperty("startDate") List<Integer> startDate,
@JsonProperty("endDate") List<Integer> endDate) {
StringBuilder stringBuilder = new StringBuilder();
for (Integer value : startDate) {
stringBuilder.append(value);
}
this.startDate = Integer.parseInt(stringBuilder.toString());
stringBuilder.setLength(0);
for (Integer value : endDate) {
stringBuilder.append(value);
}
this.endDate = Integer.parseInt(stringBuilder.toString());
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public int getStartDate() {
return startDate;
}
public void setStartDate(int startDate) {
this.startDate = startDate;
}
public int getEndDate() {
return endDate;
}
public void setEndDate(int endDate) {
this.endDate = endDate;
}
}
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) {
String json = "{\"state\" : \"OPEN\", \"startDate\" : [2020, 9, 22], \"endDate\" : [2020, 9, 24]}";
ObjectMapper mapper = new ObjectMapper();
try {
Test test = mapper.readValue(json, Test.class);
System.out.println(test.getStartDate() + " " + test.getEndDate()
+ " " + test.getState());
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
public class CustomTest {
private String state;
private int startDate;
private int endDate;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public int getStartDate() {
return startDate;
}
public void setStartDate(int startDate) {
this.startDate = startDate;
}
public int getEndDate() {
return endDate;
}
public void setEndDate(int endDate) {
this.endDate = endDate;
}
}
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
public class CustomTestDeserializer extends StdDeserializer<CustomTest> {
private static final long serialVersionUID = 1L;
public CustomTestDeserializer() {
this(null);
}
protected CustomTestDeserializer(Class<?> vc) {
super(vc);
}
@Override
public CustomTest deserialize(JsonParser jp, DeserializationContext dCtxt)
throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
String state = node.get("state").asText();
ObjectMapper mapper = new ObjectMapper();
List<Integer> startDateValue = mapper.convertValue(
node.get("startDate"), List.class);
StringBuilder stringBuilder = new StringBuilder();
for (Integer value : startDateValue) {
stringBuilder.append(value);
}
int startDate = Integer.parseInt(stringBuilder.toString());
List<Integer> endDateValue = mapper.convertValue(node.get("endDate"),
List.class);
stringBuilder.setLength(0);
for (Integer value : endDateValue) {
stringBuilder.append(value);
}
int endDate = Integer.parseInt(stringBuilder.toString());
CustomTest customTest = new CustomTest();
customTest.setEndDate(endDate);
customTest.setStartDate(startDate);
customTest.setState(state);
return customTest;
}
}
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
public class CustomTestClient {
public static void main(String[] args) {
String json = "{\"state\" : \"OPEN\", \"startDate\" : [2020, 9, 22], \"endDate\" : [2020, 9, 24]}";
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(CustomTest.class, new CustomTestDeserializer());
mapper.registerModule(module);
try {
CustomTest customTest = mapper.readValue(json, CustomTest.class);
System.out.println(customTest.getStartDate() + " "
+ customTest.getEndDate() + " " + customTest.getState());
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
注意:请确保适当实现空值处理。假设您不想更改 POJO,因此,而不是在 bean 类级别使用 @JsonDeserialize 注解,需要在客户端代码中注册此自定义反序列化程序。
英文:
Is it possible for you to define a constructor annotated with @JsonCreator in POJO and then parse the List<Integer> for date fields in json?
Something similar to this.
Test.java
import java.util.List;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Test {
private String state;
private int startDate;
private int endDate;
@JsonCreator
public Test(@JsonProperty("startDate") List<Integer> startDate,
@JsonProperty("endDate") List<Integer> endDate) {
StringBuilder stringBuilder = new StringBuilder();
for (Integer value : startDate) {
stringBuilder.append(value);
}
this.startDate = Integer.parseInt(stringBuilder.toString());
stringBuilder.setLength(0);
for (Integer value : endDate) {
stringBuilder.append(value);
}
this.endDate = Integer.parseInt(stringBuilder.toString());
}
/**
* @return the state
*/
public String getState() {
return state;
}
/**
* @param state
* the state to set
*/
public void setState(String state) {
this.state = state;
}
/**
* @return the startDate
*/
public int getStartDate() {
return startDate;
}
/**
* @param startDate
* the startDate to set
*/
public void setStartDate(int startDate) {
this.startDate = startDate;
}
/**
* @return the endDate
*/
public int getEndDate() {
return endDate;
}
/**
* @param endDate
* the endDate to set
*/
public void setEndDate(int endDate) {
this.endDate = endDate;
}
}
Main.java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) {
String json = "{\"state\" : \"OPEN\", \"startDate\" : [2020, 9, 22], \"endDate\" : [2020, 9, 24]}";
ObjectMapper mapper = new ObjectMapper();
try {
Test test = mapper.readValue(json, Test.class);
System.out.println(test.getStartDate() + " " + test.getEndDate()
+ " " + test.getState());
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
Output:
2020922 2020924 OPEN
If POJO class update is not possible then there is another alternative way to implement a custom deserializer. Where you can implement similar conversion logic.
Custom Deserialization approach
CustomTest.java
public class CustomTest {
private String state;
private int startDate;
private int endDate;
/**
* @return the state
*/
public String getState() {
return state;
}
/**
* @param state
* the state to set
*/
public void setState(String state) {
this.state = state;
}
/**
* @return the startDate
*/
public int getStartDate() {
return startDate;
}
/**
* @param startDate
* the startDate to set
*/
public void setStartDate(int startDate) {
this.startDate = startDate;
}
/**
* @return the endDate
*/
public int getEndDate() {
return endDate;
}
/**
* @param endDate
* the endDate to set
*/
public void setEndDate(int endDate) {
this.endDate = endDate;
}
}
CustomTestDeserializer.java
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
public class CustomTestDeserializer extends StdDeserializer<CustomTest> {
/**
*
*/
private static final long serialVersionUID = 1L;
public CustomTestDeserializer() {
this(null);
}
protected CustomTestDeserializer(Class<?> vc) {
super(vc);
}
@Override
public CustomTest deserialize(JsonParser jp, DeserializationContext dCtxt)
throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
String state = node.get("state").asText();
ObjectMapper mapper = new ObjectMapper();
List<Integer> startDateValue = mapper.convertValue(
node.get("startDate"), List.class);
StringBuilder stringBuilder = new StringBuilder();
for (Integer value : startDateValue) {
stringBuilder.append(value);
}
int startDate = Integer.parseInt(stringBuilder.toString());
List<Integer> endDateValue = mapper.convertValue(node.get("endDate"),
List.class);
stringBuilder.setLength(0);
for (Integer value : endDateValue) {
stringBuilder.append(value);
}
int endDate = Integer.parseInt(stringBuilder.toString());
CustomTest customTest = new CustomTest();
customTest.setEndDate(endDate);
customTest.setStartDate(startDate);
customTest.setState(state);
return customTest;
}
}
CustomTestClient.java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
public class CustomTestClient {
public static void main(String[] args) {
String json = "{\"state\" : \"OPEN\", \"startDate\" : [2020, 9, 22], \"endDate\" : [2020, 9, 24]}";
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(CustomTest.class, new CustomTestDeserializer());
mapper.registerModule(module);
try {
CustomTest customTest = mapper.readValue(json, CustomTest.class);
System.out.println(customTest.getStartDate() + " "
+ customTest.getEndDate() + " " + customTest.getState());
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
Output:
2020922 2020924 OPEN
NOTE: Please remember to implement null handling properly.
Assuming you don't want to change the POJO hence instead of @JsonDeserialize annotation at bean class level, need to register this custom deserializer at client code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论