使用fasterxml.jackson将实体类的驼峰属性变成下划线分割的属性输出。

我们的实体类:public class MessagePack implements java.io.Serializable { private long code; private String transactionId; }当然我们...

我们的实体类:

public class MessagePack implements java.io.Serializable {
	private long code;
	private String transactionId;
}

当然我们省去了set和get方法。

用普通的JSON输出就会变成:

{"code":0,"transactionId":"xxxxxxxx"}

我们可以看到 transactionId 是驼峰形式的 其中I是大写。

但是现在流行的API输出都是用下划线输出的。

那如何将其改成下划线呢。

只要使用下面代码:

ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
String res  = mapper.writeValueAsString(response);

输出就是:

{"code":0,"transaction_id":"xxxxxxxx"}

条评论

请先 登录 后评论
不写代码的码农
三叔

422 篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除