我目前正在将 Rest 服务从 RestExpress 迁移到 Jersey 框架,我必须在其中具有与 RestExpress 相同的输出。
public class AnnouncementDTO {
private String id;
private String title;
private String details;
private String postedBy;
private String permanent;
private String dismissible;
private String startDate;
private String endDate;
}
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(announcementDTO );
输出:
{
"id" : null,
"title" : "<font size=\"3\" color=\"red\">This is some text!</font>",
"details" : "<p>fhmdhd</p>",
"postedBy" : "Portal, Administrator",
"permanent" : null,
"dismissible" : null,
"startDate" : "Jul 19, 2014, 04:44 AM IST",
"endDate" : null,
"read" : null
}
我的要求是将属性名称格式化为 postedBy 到 posted_by。因此预期结果如下。
{
"title":"<font size=\"3\" color=\"red\">This is some text!</font>",
"details":"<p>fhmdhd</p>",
"posted_by":"Portal, Administrator",
"start_date":"Jul 19, 2014, 04:44 AM ET"
}
最佳答案
@JsonProperty("posted_by")
private String postedBy;
关于java - 如何将Java对象转换为Json格式化属性名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33386342/