我们的实体类:
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"}
了