2016-06-27 121 views
0
{ 
    "customer": { 
    "ssoId":"B56789", 
    "brand":"123-reg", 
    "forename":"John", 
    "surname":"Doe", 
    "title":"Mr", 
    "companyName":"Unilever", 
    "primaryEmail":"[email protected]", 
    "currency":"$", 
    "language":"English", 
    "vatNumber":"D4531234", 
    "vatCode":"12B6", 
    "ipAddress":"127.0.0.1" 
    } 
} 

這就是我的JSON文件我試圖使用Apache駱駝解組POJO,但它一直拋出該錯誤!當我添加@JsonIngoreProperties時,它會返回一個空對象而不填充POJO。解析Json到Java

@Data 
@JsonInclude(NON_EMPTY) 
public final class Customer{ 
    @JsonProperty(value = "ssoId", required = true) 
    private String ssoId; 

    @JsonProperty(value = "brand", required = true) 
    private String brand; 

    @JsonProperty(value = "forename", required = true) 
    private String forename; 
    //...... 
} 
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: 
Unrecognized field "customer" (class com.heg.esb.model.Customer), not 
marked as ignorable (17 known properties: "addresses", "phones", 
"brand", "companyName", "ssoId", "ipAddress", "currency", "vatNumber", 
"vatCode", "title", "primaryEmail", "surname", "lastVerificationDate", 
"forename", "lastModifiedDate", "createdDate", "language"]) 
at [Source: [email protected]; line: 2, column: 16] (through reference chain: com.heg.esb.model.Customer["customer"]) 
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:51) 
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:839) 
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1045) 
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1352) 
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1330) 
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:264) 
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125) 
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736) 
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2796) 
at org.apache.camel.component.jackson.JacksonDataFormat.unmarshal(JacksonDataFormat.java:173) 
at org.apache.camel.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:69) 
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) 
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:460) 
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190) 
at org.apache.camel.processor.Pipeline.process(Pipeline.java:121) 
at org.apache.camel.processor.Pipeline.process(Pipeline.java:83) 
at org.apache.camel.processor.ChoiceProcessor.process(ChoiceProcessor.java:117) 
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) 
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:460) 
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190) 
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190) 
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109) 
at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:87) 
at org.apache.camel.component.jms.EndpointMessageListener.onMessage(EndpointMessageListener.java:112) 
+0

看起來像'Customer'類中有'customer'字段,沒有使用@JsonProperty或@JsonIgnore註釋? – Wilson

+0

這並不意味着它是一個屬性,但我需要在我的Jsonpath中使用駱駝藍圖來識別POJO。 – Sammy65

+0

哦!我只注意到它被標記爲重複。那麼,這不是我在發佈之前添加的鏈接。我在問如何將上面的JSON文件轉換爲POJO,因爲JSON在花括號之前抱怨外面的「客戶」,因爲它需要一個包裝類來理解如何處理文件。我用這個網站來解決我的挑戰「http://www.jsonschema2pojo.org/」 – Sammy65

回答

0

您必須對POJO公共setter方法你想擁有的傑克遜填充每個私有字段。

另外,如果與字段名稱相同,則不需要在@JsonProperty註釋中指定「值」。

+0

lombok的@Data自動提供getter和setter。沒有這個,我不能包含「required」參數。 – Sammy65