You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot中如何使用Java 8对List内的月份字段排序

问题:Spring Boot中POJO转JSON时月份字段顺序混乱

在Spring Boot项目里,用POJO存储月份数据并存入List后,输出的JSON中月份字段不按一月至六月的顺序排列,而是杂乱无序。


相关代码及数据示例

POJO类

public class SamplePojo {
   private static final long serialVersionUID = 1L;
   private String type;
   private String name;
   private Integer January;
   private Integer February;
   private Integer March;
   private Integer April;
   private Integer May;
   private Integer June;
   private Integer total; // 补充原代码中存在setTotal对应的字段
}

ServiceImpl代码

List<SamplePojo> sp = new ArrayList<SamplePojo>();
SamplePojo tp1 = new SamplePojo();
        
tp1.setType("type");
tp1.setName("name");
tp1.setJanuary(12);
tp1.setFebruary(2);
tp1.setMarch(33);
tp1.setApril(0);
tp1.setMay(0);
tp1.setJune(0);
tp1.setTotal(122);
sp.add(tp1);

未排序的输出JSON

[
    {
        "type": "LLB",
        "name": "Working",
        "total": 0,
        "march": 33,
        "april": 0,
        "may": 0,
        "june": 0,
        "february": 2,
        "january": 12
    },
    {
        "type": "Engineer",
        "name": "Not Working",
        "total": 0,
        "march": 33,
        "april": 0,
        "may": 0,
        "june": 0,
        "february": 2,
        "january": 12
    }
]

期望的排序后JSON

[
    {
        "type": "LLB",
        "name": "Working",
        "january": 11,
        "february": 2,
        "march": 3,
        "april": 3,
        "may": 0,
        "june": 0,
        "total": 0
    },
    {
        "type": "Engineer",
        "name": "Not Working",
        "january": 12,
        "february": 2,
        "march": 33,
        "april": 0,
        "may": 0,
        "june": 0,
        "total": 0
    }
]

解决方案

方法1:用Jackson的@JsonPropertyOrder注解(推荐)

Spring Boot默认用Jackson做JSON序列化,直接在POJO类上添加注解指定字段输出顺序即可:

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder({"type", "name", "january", "february", "march", "april", "may", "june", "total"})
public class SamplePojo {
   private static final long serialVersionUID = 1L;
   private String type;
   private String name;
   private Integer January;
   private Integer February;
   private Integer March;
   private Integer April;
   private Integer May;
   private Integer June;
   private Integer total;

   // 省略getter、setter方法
}

注意:注解里的字段名要和JSON输出的小写名称一致(Jackson默认会把驼峰字段转成小写格式,比如January转成january)。

方法2:全局配置Jackson序列化策略

如果多个POJO都需要按字段定义顺序输出,可以写一个配置类全局设置:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

@Configuration
public class JacksonConfig {
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        // 关闭按字母排序,强制按字段声明顺序输出
        objectMapper.configure(SerializationFeature.ORDER_PROPERTIES_ALPHABETICALLY, false);
        return objectMapper;
    }
}

这种方式会影响所有POJO的JSON输出顺序,适合全局统一需求的场景。

方法3:修改POJO字段命名(可选)

把POJO里的月份字段改成小写(比如january而非January),同时保证字段声明顺序就是你要的输出顺序。不过这种方式需要修改字段名及对应的getter/setter,不如注解方式灵活。


内容的提问来源于stack exchange,提问作者Hanufa Hannan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 17:09:44