spring 解決返回對象指定的的屬性順序問題


說明:@JsonPropertyOrder  勇於fastjson 對對象字段實現排序使用
1.@JsonPropertyOrder(value = {"name", "id"})

明確制定字段排序順序  name ==》 id

實體類屬性
public class WorkingPlace implements Serializable {
    private static final long serialVersionUID = 163375549891371085L;
    /**
     * 主鍵
     */
    private Long id;
    /**
     * 部門名稱
     */
    private String name;
}

 

請求返回順序(測試發現是按照實體類從上到下的屬性返回的)

{
  "id": 4,
  "name": "xxx"
}

現在要求返回順序變化、name在id 的上面可以設置:

@JsonPropertyOrder(value = {"name", "id"})
public class WorkingPlace implements Serializable {
    private static final long serialVersionUID = 163375549891371085L;
    /**
     * 主鍵
     */
    private Long id;
    /**
     * 部門名稱
     */
    private String name;
}

 

2、@JsonPropertyOrder(alphabetic = true) 

使用字段的字母排序

可以使用到類,屬性比如map<String,String

 

實體類中的屬性
@JsonPropertyOrder(alphabetic = true)
    private Map<String, String> fieldDetails;

    public Map<String, String> getFieldDetails() {
        return fieldDetails;
    }

    public void setFieldDetails(Map<String, String> fieldDetails) {
        this.fieldDetails = fieldDetails;
    }

使用時設置
xxx.setFieldDetails(ImmutableMap.of("name", "name", "id", "id"));

輸出順序:
name ==> id

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM