@Getter
@Setter
static class User {
private String userName;
private int userAge;
// getters and setters
}
public static void main(String[] args) throws Exception {
User user = new User();
user.setUserName("John");
user.setUserAge(30);
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
String json = mapper.writeValueAsString(user);
System.out.println(json);
}
结果:
{"user_name":"John","user_age":30}