英文:
jackson mapping nested hashmap to nested pojo class in java
问题
我有一个类似这样的嵌套的Java映射
inputMap: {jobId={EndpointReference={ReferenceParameters={ResourceURI=http://schemas.com/wbem/wscim/1/cim-schema/2/Job, SelectorSet={Selector=[JID_502260561923, root/im]}}, Address=http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous}}, returncode=4096, messageId=null, arguments=null, message=null}
我想将其映射到Java POJO,这是我的POJO类。
@Getter
@Setter
@ToString
public class DMResponseMapper {
@Getter
@Setter
@ToString
public static class GetSysConfigDMResponseMapper {
@JsonProperty("jobId")
private EndpointReferenceMapper endpointReferenceMapper;
private Integer returnCode;
private String messageId;
private String arguments;
private String message;
@Getter
@Setter
@ToString
public static class EndpointReferenceMapper {
@JsonProperty("ReferenceParameters")
private ReferenceParametersMapper referenceParametersMapper;
@JsonProperty("Address")
private String address;
@Getter
@Setter
@ToString
public static class ReferenceParametersMapper {
@JsonProperty("ResourceURI")
private String resourceURI;
@JsonProperty("SelectorSet")
private SelectorSetMapper selectorSetMapper;
@Getter
@Setter
@ToString
public static class SelectorSetMapper {
@JsonProperty("Selector")
private List<String> selector;
}
}
}
}
}
}
但objectMapper.convertValue(inputMap, GetSysConfigDMResponseMapper.class)
不映射嵌套类,只映射顶级字段。
我的objectMapper
像这样实例化:
static {
objectMapper = new ObjectMapper();
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
响应对象是:
DMResponseMapper.GetSysConfigDMResponseMapper(endpointReferenceMapper=DMResponseMapper.GetSysConfigDMResponseMapper.EndpointReferenceMapper(referenceParametersMapper=null, address=null), returnCode=4096, messageId=null, arguments=null, message=null)
有人可以建议,这里有什么问题吗?
在调试时,我看到的是:
将endpointReferenceMapper
转换为Object
类型。
DMResponseMapper.GetSysConfigDMResponseMapper(endpointReferenceMapper={EndpointReference={ReferenceParameters={ResourceURI=http://schemas.com/wbem/wscim/1/cim-schema/2/Job, SelectorSet={Selector=[JID_502318722705, root/dcim]}}, Address=http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous}}, returnCode=4096, messageId=null, arguments=null, message=null)
英文:
I have a nested java map like this
inputMap: {jobId={EndpointReference={ReferenceParameters={ResourceURI=http://schemas.com/wbem/wscim/1/cim-schema/2/Job, SelectorSet={Selector=[JID_502260561923, root/im]}}, Address=http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous}}, returncode=4096, messageId=null, arguments=null, message=null}
which I want to map to java pojo and here is my pojo classes.
@Getter
@Setter
@ToString
public class DMResponseMapper {
@Getter
@Setter
@ToString
public static class GetSysConfigDMResponseMapper {
@JsonProperty("jobId")
private EndpointReferenceMapper endpointReferenceMapper;
private Integer returnCode;
private String messageId;
private String arguments;
private String message;
@Getter
@Setter
@ToString
public static class EndpointReferenceMapper {
@JsonProperty("ReferenceParameters")
private ReferenceParametersMapper referenceParametersMapper;
@JsonProperty("Address")
private String address;
@Getter
@Setter
@ToString
public static class ReferenceParametersMapper {
@JsonProperty("ResourceURI")
private String resourceURI;
@JsonProperty("SelectorSet")
private SelectorSetMapper selectorSetMapper;
@Getter
@Setter
@ToString
public static class SelectorSetMapper {
@JsonProperty("Selector")
private List<String> selector;
}
}
}
}
}
but objectMapper.convertValue(inputMap, GetSysConfigDMResponseMapper.class) is NOT mapping the nested classes.. just the top level fields.
My objectMapper is instantiated like this:
static {
objectMapper = new ObjectMapper();
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
Response Object is :
DMResponseMapper.GetSysConfigDMResponseMapper(endpointReferenceMapper=DMResponseMapper.GetSysConfigDMResponseMapper.EndpointReferenceMapper(referenceParametersMapper=null, address=null), returnCode=4096, messageId=null, arguments=null, message=null)
Can anyone please suggest, what is wrong here?
Upon debugging this is what I see:
Converted endpointReferenceMapper to type Object.
DMResponseMapper.GetSysConfigDMResponseMapper(endpointReferenceMapper={EndpointReference={ReferenceParameters={ResourceURI=http://schemas.com/wbem/wscim/1/cim-schema/2/Job, SelectorSet={Selector=[JID_502318722705, root/dcim]}}, Address=http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous}}, returnCode=4096, messageId=null, arguments=null, message=null)
答案1
得分: 1
DMResponseMapper
pojo 需要更紧密地遵循源数据的结构。
根据问题中提供的信息,您的源 Map
对象具有以下结构:
inputMap:
{
jobId={
EndpointReference={
ReferenceParameters={
ResourceURI=http://schemas.com/wbem/wscim/1/cim-schema/2/Job,
SelectorSet={
Selector=[JID_502260561923, root/im]
}
},
Address=http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
}
},
returncode=4096,
messageId=null,
arguments=null,
message=null
}
因此,我调整了您的 DMResponseMapper
pojo 类,使其更紧密地映射到该结构,并且我还更改了嵌套类的名称。以下是嵌套类及其字段的摘要:
//
// 不是实际的类 - 只是结构概览!
//
class DMResponseMapper {
private JobId jobId;
private Integer returncode;
private Object messageId;
private Object arguments;
private Object message;
class JobId {
private EndpointReference endpointReference;
class EndpointReference {
private ReferenceParameters referenceParameters;
private String address;
class ReferenceParameters {
private String resourceURI;
private SelectorSet selectorSet;
class SelectorSet {
private List<String> selector = null;
}
}
}
}
}
在添加了注解和 getter/setter 后,得到了以下类:
//
// 这是基于上述结构的实际类。
//
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
public class DMResponseMapper {
@JsonProperty("jobId")
private JobId jobId;
@JsonProperty("returncode")
private Integer returncode;
@JsonProperty("messageId")
private Object messageId;
@JsonProperty("arguments")
private Object arguments;
@JsonProperty("message")
private Object message;
@JsonProperty("jobId")
public JobId getJobId() {
return jobId;
}
@JsonProperty("jobId")
public void setJobId(JobId jobId) {
this.jobId = jobId;
}
@JsonProperty("returncode")
public Integer getReturncode() {
return returncode;
}
@JsonProperty("returncode")
public void setReturncode(Integer returncode) {
this.returncode = returncode;
}
@JsonProperty("messageId")
public Object getMessageId() {
return messageId;
}
@JsonProperty("messageId")
public void setMessageId(Object messageId) {
this.messageId = messageId;
}
@JsonProperty("arguments")
public Object getArguments() {
return arguments;
}
@JsonProperty("arguments")
public void setArguments(Object arguments) {
this.arguments = arguments;
}
@JsonProperty("message")
public Object getMessage() {
return message;
}
@JsonProperty("message")
public void setMessage(Object message) {
this.message = message;
}
public static class JobId {
@JsonProperty("EndpointReference")
private EndpointReference endpointReference;
@JsonProperty("EndpointReference")
public EndpointReference getEndpointReference() {
return endpointReference;
}
@JsonProperty("EndpointReference")
public void setEndpointReference(EndpointReference endpointReference) {
this.endpointReference = endpointReference;
}
public static class EndpointReference {
@JsonProperty("ReferenceParameters")
private ReferenceParameters referenceParameters;
@JsonProperty("Address")
private String address;
@JsonProperty("ReferenceParameters")
public ReferenceParameters getReferenceParameters() {
return referenceParameters;
}
@JsonProperty("ReferenceParameters")
public void setReferenceParameters(ReferenceParameters referenceParameters) {
this.referenceParameters = referenceParameters;
}
@JsonProperty("Address")
public String getAddress() {
return address;
}
@JsonProperty("Address")
public void setAddress(String address) {
this.address = address;
}
public static class ReferenceParameters {
@JsonProperty("ResourceURI")
private String resourceURI;
@JsonProperty("SelectorSet")
private SelectorSet selectorSet;
@JsonProperty("ResourceURI")
public String getResourceURI() {
return resourceURI;
}
@JsonProperty("ResourceURI")
public void setResourceURI(String resourceURI) {
this.resourceURI = resourceURI;
}
@JsonProperty("SelectorSet")
public SelectorSet getSelectorSet() {
return selectorSet;
}
@JsonProperty("SelectorSet")
public void setSelectorSet(SelectorSet selectorSet) {
this.selectorSet = selectorSet;
}
public static class SelectorSet {
@JsonProperty("Selector")
private List<String> selector = null;
@JsonProperty("Selector")
public List<String> getSelector() {
return selector;
}
@JsonProperty("Selector")
public void setSelector(List<String> selector) {
this.selector = selector;
}
}
}
}
}
}
这是如何调用的:
首先,一些测试数据:
List<String> selector = new ArrayList();
selector.add("JID_502260561923");
selector.add("root/im");
Map<String, Object> selectorSet = new HashMap();
selectorSet.put("Selector", selector);
String resourceURI = "http://schemas.com/wbem/wscim/1/cim-schema/2/Job";
Map<String, Object> referenceParameters = new HashMap();
referenceParameters.put("ResourceURI", resourceURI);
referenceParameters.put("SelectorSet", selectorSet);
String address = "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous";
Map<String, Object> endpointReference = new HashMap();
endpointReference.put("ReferenceParameters", referenceParameters);
endpointReference.put("Address", address);
Map<String, Object> jobId = new HashMap();
jobId.put("EndpointReference", endpointReference);
Map<String, Object> inputMap = new HashMap();
inputMap.put("jobId", jobId);
inputMap.put("returncode", 4096);
inputMap.put("messageId", "foo");
inputMap.put("arguments", "bar");
inputMap.put("message", "baz");
然后是执行映射的代码:
ObjectMapper objectMapper = new ObjectMapper();
DMResponseMapper mapper = objectMapper.convertValue(inputMap, DMResponseMapper.class);
生成的 mapper
对象包含了测试数据。
英文:
The DMResponseMapper
pojo needs to follow the structure of your source data more closely.
Your source Map
object has the following structure, based on the info in the question:
inputMap:
{
jobId={
EndpointReference={
ReferenceParameters={
ResourceURI=http://schemas.com/wbem/wscim/1/cim-schema/2/Job,
SelectorSet={
Selector=[JID_502260561923, root/im]
}
},
Address=http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
}
},
returncode=4096,
messageId=null,
arguments=null,
message=null
}
So, I adapted your DMResponseMapper
pojo class to more closely map to that structure - and I changed the nested class names as well. Here is a summary of the nested classes with their fields for your data:
//
// NOT the actual class - just an overview of the structure!
//
class DMResponseMapper {
private JobId jobId;
private Integer returncode;
private Object messageId;
private Object arguments;
private Object message;
class JobId {
private EndpointReference endpointReference;
class EndpointReference {
private ReferenceParameters referenceParameters;
private String address;
class ReferenceParameters {
private String resourceURI;
private SelectorSet selectorSet;
class SelectorSet {
private List<String> selector = null;
}
}
}
}
}
This gave me the following, when fleshed out with annotations and getters/setters:
//
// Here is the actual class, based on the above structure.
//
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
public class DMResponseMapper {
@JsonProperty("jobId")
private JobId jobId;
@JsonProperty("returncode")
private Integer returncode;
@JsonProperty("messageId")
private Object messageId;
@JsonProperty("arguments")
private Object arguments;
@JsonProperty("message")
private Object message;
@JsonProperty("jobId")
public JobId getJobId() {
return jobId;
}
@JsonProperty("jobId")
public void setJobId(JobId jobId) {
this.jobId = jobId;
}
@JsonProperty("returncode")
public Integer getReturncode() {
return returncode;
}
@JsonProperty("returncode")
public void setReturncode(Integer returncode) {
this.returncode = returncode;
}
@JsonProperty("messageId")
public Object getMessageId() {
return messageId;
}
@JsonProperty("messageId")
public void setMessageId(Object messageId) {
this.messageId = messageId;
}
@JsonProperty("arguments")
public Object getArguments() {
return arguments;
}
@JsonProperty("arguments")
public void setArguments(Object arguments) {
this.arguments = arguments;
}
@JsonProperty("message")
public Object getMessage() {
return message;
}
@JsonProperty("message")
public void setMessage(Object message) {
this.message = message;
}
public static class JobId {
@JsonProperty("EndpointReference")
private EndpointReference endpointReference;
@JsonProperty("EndpointReference")
public EndpointReference getEndpointReference() {
return endpointReference;
}
@JsonProperty("EndpointReference")
public void setEndpointReference(EndpointReference endpointReference) {
this.endpointReference = endpointReference;
}
public static class EndpointReference {
@JsonProperty("ReferenceParameters")
private ReferenceParameters referenceParameters;
@JsonProperty("Address")
private String address;
@JsonProperty("ReferenceParameters")
public ReferenceParameters getReferenceParameters() {
return referenceParameters;
}
@JsonProperty("ReferenceParameters")
public void setReferenceParameters(ReferenceParameters referenceParameters) {
this.referenceParameters = referenceParameters;
}
@JsonProperty("Address")
public String getAddress() {
return address;
}
@JsonProperty("Address")
public void setAddress(String address) {
this.address = address;
}
public static class ReferenceParameters {
@JsonProperty("ResourceURI")
private String resourceURI;
@JsonProperty("SelectorSet")
private SelectorSet selectorSet;
@JsonProperty("ResourceURI")
public String getResourceURI() {
return resourceURI;
}
@JsonProperty("ResourceURI")
public void setResourceURI(String resourceURI) {
this.resourceURI = resourceURI;
}
@JsonProperty("SelectorSet")
public SelectorSet getSelectorSet() {
return selectorSet;
}
@JsonProperty("SelectorSet")
public void setSelectorSet(SelectorSet selectorSet) {
this.selectorSet = selectorSet;
}
public static class SelectorSet {
@JsonProperty("Selector")
private List<String> selector = null;
@JsonProperty("Selector")
public List<String> getSelector() {
return selector;
}
@JsonProperty("Selector")
public void setSelector(List<String> selector) {
this.selector = selector;
}
}
}
}
}
}
This is invoked as follows:
First, some test data:
List<String> selector = new ArrayList();
selector.add("JID_502260561923");
selector.add("root/im");
Map<String, Object> selectorSet = new HashMap();
selectorSet.put("Selector", selector);
String resourceURI = "http://schemas.com/wbem/wscim/1/cim-schema/2/Job";
Map<String, Object> referenceParameters = new HashMap();
referenceParameters.put("ResourceURI", resourceURI);
referenceParameters.put("SelectorSet", selectorSet);
String address = "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous";
Map<String, Object> endpointReference = new HashMap();
endpointReference.put("ReferenceParameters", referenceParameters);
endpointReference.put("Address", address);
Map<String, Object> jobId = new HashMap();
jobId.put("EndpointReference", endpointReference);
Map<String, Object> inputMap = new HashMap();
inputMap.put("jobId", jobId);
inputMap.put("returncode", 4096);
inputMap.put("messageId", "foo");
inputMap.put("arguments", "bar");
inputMap.put("message", "baz");
Note I replaced your null
values with strings, for testing and demonstration.
Then the code to perform the mapping:
ObjectMapper objectMapper = new ObjectMapper();
DMResponseMapper mapper = objectMapper.convertValue(inputMap, DMResponseMapper.class);
The resulting mapper
object contains the test data:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论