Spring RestControllerアクションの戻り値の型である巨大なMap<String, Object>
があります。 マップは基本的にString - Stringという文字列と、contextというマップとのペアです。 出力jsonを最初のコンテキストにソートしたいと思います。 返されたオブジェクトにプロパティがある場合、解決策として@JsonPropertyOrder
が見つかりました。残念ながら、それは私のマップで動作していません。Spring RestControllerのJsonプロパティを並べ替えるには
@RestController
public class MyController {
@RequestMapping("/test.json")
@JsonPropertyOrder(value = {"context"}, alphabetic = true)
public Map<String, Object> response() {
Map<String, String> context = new HashMap<>();
context.put("environment", "dev");
Map<String, Object> myMap = new HashMap<>();
myMap.put("context", context);
myMap.putAll(fillMyMap());
return myMap;
}
private Map<String, String> fillMyMap() {
Map<String, String> myMapFillValues = new HashMap<>();
myMapFillValues.put("test", "foo");
myMapFillValues.put("test2", "bar");
return myMapFillValues;
}
}
だから私は、この代わりに、このの
{
"context": {
"environment": "dev"
},
"test": "foo",
"test2": "bar"
}
のような出力たい:
{
"test": "foo",
"test2": "bar",
"context": {
"environment": "dev"
}
}
これは、注文を維持していないため、HashMapでこれを行うことはできません。注文を気にする場合は、LinkedHashMapや注文を管理する他のマップを試すことができます。 linkedHashMapが動作するかどうか確認してください – mlecz
その注釈は文脈の中で項目をアルファベット順に並べるとは言いませんか? – chrylis
@mlecz - Thx、それは私の問題を解決しました。 – Sigee