# Jackson Json工具

### Build gradle

```
// build.gradle
// fix Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module
// "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.2'
implementation 'com.fasterxml.jackson.module:jackson-module-parameter-names:2.13.2'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.13.2'
```

### 主要紀錄實際應用的錯誤訊息與解決作法

#### 錯誤一:  欄位對應不正確

```
// Some code
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "balance" (class com.longxiang.game.goodroad.model.GRMemberInfo), not marked as ignorable (2 known properties: "order_id", "account"])
 at [Source: (String)"{"status":"Y","code":"","message":"","data":{"account":"GRCAS005@luk","balance":300,"order_id":"IN#20220928160003#GRCAS005@luk#1409-fa3e2","credit_amount":100,"c_type":"real"}}"; line: 1, column: 84] (through reference chain: com.longxiang.game.goodroad.model.GRBaseRes["data"]->com.longxiang.game.goodroad.model.GRMemberInfo["balance"])
	at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:987)
	at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1974)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1701)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1679)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:330)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:187)
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:324)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:187)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4593)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3548)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3531)
	at com.longxiang.service.CasterTest.main(CasterTest.java:195)
```

問題描述：Json 字串 有多個欄位，但Java 物件的變數欄位並沒有這麼多，導致Mapping時會出現找不到欄位的錯誤，但你又只需要幾個欄位就好，就可以在Class 加上`@JsonIgnoreProperties(ignoreUnknown = true)` 這個註解詳解就自行翻閱他的 source code 了解內容。\\

```
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class GRMemberInfo {

    @JsonProperty("account")
    private String username;

    @JsonProperty("order_id")
    private String orderNo;

}
```
