Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中jackson将嵌套hashmap映射到嵌套pojo类_Java_Jackson_Pojo_Objectmapper - Fatal编程技术网

java中jackson将嵌套hashmap映射到嵌套pojo类

java中jackson将嵌套hashmap映射到嵌套pojo类,java,jackson,pojo,objectmapper,Java,Jackson,Pojo,Objectmapper,我有一个这样的嵌套java映射 inputMap:{jobId={EndpointReference={ReferenceParameters={ResourceURI=http://schemas.com/wbem/wscim/1/cim-schema/2/Job,SelectorSet={Selector=[JID_502260561923,root/im]},地址=http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous

我有一个这样的嵌套java映射 inputMap:{jobId={EndpointReference={ReferenceParameters={ResourceURI=http://schemas.com/wbem/wscim/1/cim-schema/2/Job,SelectorSet={Selector=[JID_502260561923,root/im]},地址=http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous}},returncode=4096,messageId=null,arguments=null,message=null} 我想映射到JavaPOJO,这里是我的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;
                    }
                }
            }
        }
    }
}

响应对象是:

DMResponseMapper.GetSysConfigDMResponseMapper(endpointReferenceMapper=DMResponseMapper.GetSysConfigDMResponseMapper.endpointReferenceMapper(referenceParametersMapper=null,address=null),returnCode=4096,messageId=null,arguments=null,message=null)

谁能告诉我,这里怎么了

调试时,我看到的是: 已将endpointReferenceMapper转换为类型对象

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)

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类,以更紧密地映射到该结构,并且还更改了嵌套的类名。以下是嵌套类及其数据字段的摘要:

//
// 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;
                }
            }
        }
    }
}
生成的
mapper
对象包含测试数据:

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

谢谢。这是一次失误。。我看不清嵌套的细节。你用什么工具来展示课堂布局?你是welcone。在本例中,我使用格式化JSON-这使我能够更清楚地看到嵌套。然后,我设计了类嵌套以遵循该结构(仅类和字段)。这是一个手动步骤,但您可以使用诸如之类的工具来帮助实现这一点。这是一个很好的工具——特别是对于复杂的JSON,但有时我会简化它生成的内容以满足我的特定需求。可能有更好的方法——我不是JSON专家。
//
// 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;
                    }

                }

            }

        }

    }

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