使用JsonProperty注解解决Json解析首字母为大写字段匹配不上的问题

采用@RequestBody注解接收application/json格式的数据,然后数据封装在一个Modle中,假如Model中存在首字母大写的字段,

此时会报错:

2019-04-30 16:58:29.770 [http-bio-8080-exec-4] DEBUG o.s.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor:140 - Reading [class com.xxx.action.GpsUpData] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@28c5acbe]
2019-04-30 16:58:29.787 [http-bio-8080-exec-4] DEBUG o.s.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver:132 - Resolving exception from handler [public java.util.Map com.xxx.action.ConstructionGpsReportAction.getData(com.xxx.action.GpsUpData)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "A1" (class com.xxx.action.GpsUpData), not marked as ignorable (35 known properties: "ios", "mac", "state", "A3", "val_1", "val_2", "val_3", "val_4", "val_5", "val_6", "val_7", "val_8", "val_9", "lat", "lng", "height", "dif_data", "data", "sat_num", "a1", "a2", "a3", "isSyc", "set_type", "type", "conId", "time", "A2", "Gy", "seq", "gx", "gy", "gz", "Gx", "Gz"])
 at [Source: org.apache.catalina.connector.CoyoteInputStream@2c2a81ef; line: 1, column: 346] (through reference chain: com.xxx.action.GpsUpData["A1"]); nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "A1" (class com.xxx.action.GpsUpData), not marked as ignorable (35 known properties: "ios", "mac", "state", "A3", "val_1", "val_2", "val_3", "val_4", "val_5", "val_6", "val_7", "val_8", "val_9", "lat", "lng", "height", "dif_data", "data", "sat_num", "a1", "a2", "a3", "isSyc", "set_type", "type", "conId", "time", "A2", "Gy", "seq", "gx", "gy", "gz", "Gx", "Gz"])
 at [Source: org.apache.catalina.connector.CoyoteInputStream@2c2a81ef; line: 1, column: 346] (through reference chain: com.xxx.action.GpsUpData["A1"])
2019-04-30 16:58:29.792 [http-bio-8080-exec-4] DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver:132 - Resolving exception from handler [public java.util.Map com.vito.dcs.paver.construction.action.ConstructionGpsReportAction.getData(com.xxx.action.GpsUpData)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "A1" (class com.xxx.action.GpsUpData), not marked as ignorable (35 known properties: "ios", "mac", "state", "A3", "val_1", "val_2", "val_3", "val_4", "val_5", "val_6", "val_7", "val_8", "val_9", "lat", "lng", "height", "dif_data", "data", "sat_num", "a1", "a2", "a3", "isSyc", "set_type", "type", "conId", "time", "A2", "Gy", "seq", "gx", "gy", "gz", "Gx", "Gz"])
 at [Source: org.apache.catalina.connector.CoyoteInputStream@2c2a81ef; line: 1, column: 346] (through reference chain: com.xxx.action.GpsUpData["A1"]); nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "A1" (class com.xxx.action.GpsUpData), not marked as ignorable (35 known properties: "ios", "mac", "state", "A3", "val_1", "val_2", "val_3", "val_4", "val_5", "val_6", "val_7", "val_8", "val_9", "lat", "lng", "height", "dif_data", "data", "sat_num", "a1", "a2", "a3", "isSyc", "set_type", "type", "conId", "time", "A2", "Gy", "seq", "gx", "gy", "gz", "Gx", "Gz"])
 at [Source: org.apache.catalina.connector.CoyoteInputStream@2c2a81ef; line: 1, column: 346] (through reference chain: com.xxx.action.GpsUpData["A1"])
2019-04-30 16:58:29.792 [http-bio-8080-exec-4] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver:132 - Resolving exception from handler [public java.util.Map com.xxx.action.ConstructionGpsReportAction.getData(com.xxx.action.GpsUpData)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "A1" (class com.xxx.action.GpsUpData), not marked as ignorable (35 known properties: "ios", "mac", "state", "A3", "val_1", "val_2", "val_3", "val_4", "val_5", "val_6", "val_7", "val_8", "val_9", "lat", "lng", "height", "dif_data", "data", "sat_num", "a1", "a2", "a3", "isSyc", "set_type", "type", "conId", "time", "A2", "Gy", "seq", "gx", "gy", "gz", "Gx", "Gz"])
 at [Source: org.apache.catalina.connector.CoyoteInputStream@2c2a81ef; line: 1, column: 346] (through reference chain: com.xxx.action.GpsUpData["A1"]); nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "A1" (class com.xxx.action.GpsUpData), not marked as ignorable (35 known properties: "ios", "mac", "state", "A3", "val_1", "val_2", "val_3", "val_4", "val_5", "val_6", "val_7", "val_8", "val_9", "lat", "lng", "height", "dif_data", "data", "sat_num", "a1", "a2", "a3", "isSyc", "set_type", "type", "conId", "time", "A2", "Gy", "seq", "gx", "gy", "gz", "Gx", "Gz"])
 at [Source: org.apache.catalina.connector.CoyoteInputStream@2c2a81ef; line: 1, column: 346] (through reference chain: com.xxx.action.GpsUpData["A1"])

不能识别的字段。jackson解析时默认将字段名解析为首字母小写,此时匹配不上,报400。

此时在字段上面加上@JsonProperty注解。

@JsonProperty("A1")
private Double A1;

以下是@JsonProperty注解的说明:

com.fasterxml.jackson.annotation.JsonProperty
标记注释,可用于将非静态方法定义为逻辑属性的“setter”或“getter”(取决于其签名),或将使用的非静态对象字段(序列化、反序列化)作为逻辑属性。
默认值("")表示字段名用作属性名而不做任何修改,但可以将其指定为非空值以指定不同的名称。属性名引用外部使用的名称,作为JSON对象中的字段名。

注意:封装的Model类必须实现java.io.Serializable接口。

你可能感兴趣的:(Java)