UnderScore or CamelCase
- Json String could be underscore(access_token) or camel(accessToken) form
Mapping Json to Model(noramlly camelCase), need to specify Name Strategy if json property in a underscore format, otherwise will lost value(null)
Jackson
- default mapping by name
for signle property, use annotation
@JsonProperty(value = "access_token") private String accessToken;
for globe setting, set ObejctMapper.PropertyNamingStrategy
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
- in practice, create a util class to custom jackson objectmapper, config its features, like serializer/deserializer, time format, name strategy, etc.
- Ref Doc: https://github.com/FasterXML/jackson-databind/wiki
Gson
annotation way
@SerializedName(value = "access_token") private String accessToken;
globe way, setFieldNamingPolicy
Gson gson = new GsonBuilder() .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .create();
- also could use util class
Note
- Spring integrated JacksonJson as message coverter, so could use globe way to do once/everywhere.
- Gson normally used on special case, as some modern api not compitiable with it, as swagger, so cloud declear it in anywhere in-need, more domain design style.
本文由 Ivan Dong 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Jun 13, 2023 at 09:21 am