需求:完成响应对象驼峰->蛇形的转换,包括响应对应中Map类型的key也需要转换。
准备工作
com.google.guava
guava
31.0.1-jre
@Data
public class User {
private String nameInfo;
private String ageInfo;
private Account account;
@JsonSerialize(using = MapNamingStrategySerialize.class)
private Map extraMap;
@JsonSerialize(using = MapNamingStrategySerialize.class)
private Map cache;
@Data
public static class Account {
private Long accountId;
@JsonProperty("name_4_user")
private String name4User;
}
public static User builder() {
User user = new User();
user.setNameInfo("coder");
user.setAgeInfo("28");
Account account = new Account();
account.setAccountId(1001L);
account.setName4User("liming");
user.setAccount(account);
Map extra = new HashMap<>();
extra.put("id4User", "123");
extra.put("userAge", 23);
extra.put("myPrice", 12.345);
extra.put("uId", 1200L);
extra.put("account", account);
user.setExtraMap(extra);
Map cache = new HashMap<>();
cache.put("id4Cache", "123");
cache.put("name4Cache", "456");
user.setCache(cache);
return user;
}
}
1. 【存在问题】方式一
public class Test1 {
public static void main(String[] args) throws JsonProcessingException {
userStrategy();
}
/**
* 使用策略来进行实现
*/
private static void userStrategy() throws JsonProcessingException {
User user = User.builder();
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategy.SNAKE_CASE);
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user);
System.out.println(json);
}
}
这种无法转换Map中的key。
2. 【可以实现】方式二:JsonSerializer
public class MapNamingStrategySerialize extends JsonSerializer
在Map属性上使用@JsonSerialize(using = MapNamingStrategySerialize.class)
即可实现。