方法1: 类加上注解@JsonPropertyOrder,里面按需要指定的顺序写上字段
@JsonPropertyOrder({"firstname", "lastname", "birthday"})
方法2:成员变量加上注解@JsonProperty,通过value指定顺序(数字越小越靠前)
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Nc {
/**
* 播报方式
*/
@JsonProperty(index = 2)
private String channel;
/**
* 播报内容
*/
@JsonProperty(index = 1)
private String ct;
/**
* 消息编码
*/
@JsonProperty(index = 3)
private String charset;
}
输出结果(因为charset没有值,为null,而且设置了 @JsonInclude(JsonInclude.Include.NON_NULL), 所以没有显示该字段):
{
"nc": {
"ct": "12345",
"channel": "1"
}
}
参考资料: