迭代 ResponseBody 中的项目并将它们放入 HashMap Spring Boot
Iterate items in ResponseBody and put them in a HashMap Spring Boot
在 Spring 启动的 REST 控制器中,我试图迭代 RequestBody 响应中的值,并将其中一些值放入 POST 端点的 HashMap 中。
我发送的JSON是这样的结构:
{"name":"yogurt","vitaminA":6,"vitaminb12":5}
端点目前看起来是这样的:
@RequestMapping("/create")
public NutrientList createNUtrientList(@RequestBody NutrientList nutrientList) {
Map<String, Double> nutrientMap = new HashMap<String,Double>();
//get nutrient values, need help with this part
for()
//add values to map
NutrientList nl = new NutrientList(nutrientList.getName(), nutrientMap);
//will save to repository
return nl;
}
营养清单 class 如下所示:
public class NutrientList {
@Id
private ObjectId id;
@JsonProperty("name")
private String name;
@JsonProperty("nutrientMap")
Map <String,Double> nutrientMap = new HashMap<String,Double>();
public NutrientList() {}
public NutrientList(String name, Map<String, Double> nutrientMap) {
this.id = new ObjectId();
this.name = name;
this.nutrientMap = nutrientMap;
}
//setters and getters
}
数据在数据库中按单独的营养素存储,不是地图。我看到 NutrientList class 不共享相同的结构,但是有什么方法可以解决这个问题,以便能够在不改变它在数据库中的存储方式的情况下使用地图?
我需要使用地图,因为营养成分很多,我不想为它们设置单独的变量。太感谢了。如果有什么不清楚的地方,请告诉我。
编辑:
我可以交替地将我从数据库中获取数据的 csv 转换为带有地图的 JSON 格式,但我还没有找到可以给我这种灵活性的在线工具。
如果您有有效密钥列表,您可以使用以下内容:
private static final List<String> validKeys = Arrays.asList("vitaminA", "vitaminB" /* ... */);
@RequestMapping("/create")
public NutrientList createNutrientList(@RequestBody Map<String, Object> requestBody) {
Map<String, Double> nutrientMap = new HashMap<>();
for (String nutrient : requestBody.keySet()) {
if (validKeys.contains(nutrient) && requestBody.get(nutrient) instanceof Number) {
Number number = (Number) requestBody.get(nutrient);
nutrientMap.put(nutrient, number.doubleValue());
}
}
String name = (String) requestBody.get("name"); // maybe check if name exists and is really a string
return new NutrientList(name, nutrientMap);
}
如果你想使用Java 8 Stream API你可以尝试:
private static final List<String> validKeys = Arrays.asList("vitaminA", "vitaminB" /* ... */);
@RequestMapping("/create")
public NutrientList createNutrientList(@RequestBody Map<String, Object> requestBody) {
Map<String, Double> nutrientMap = requestBody.entrySet().stream()
.filter(e -> validKeys.contains(e.getKey()))
.filter(e -> e.getValue() instanceof Number)
.collect(Collectors.toMap(Map.Entry::getKey, e -> ((Number) e.getValue()).doubleValue()));
String name = Optional.ofNullable(requestBody.get("name"))
.filter(n -> n instanceof String)
.map(n -> (String) n)
.orElseThrow(IllegalArgumentException::new);
return new NutrientList(name, nutrientMap);
}
希望对您有所帮助。
在 Spring 启动的 REST 控制器中,我试图迭代 RequestBody 响应中的值,并将其中一些值放入 POST 端点的 HashMap 中。
我发送的JSON是这样的结构:
{"name":"yogurt","vitaminA":6,"vitaminb12":5}
端点目前看起来是这样的:
@RequestMapping("/create")
public NutrientList createNUtrientList(@RequestBody NutrientList nutrientList) {
Map<String, Double> nutrientMap = new HashMap<String,Double>();
//get nutrient values, need help with this part
for()
//add values to map
NutrientList nl = new NutrientList(nutrientList.getName(), nutrientMap);
//will save to repository
return nl;
}
营养清单 class 如下所示:
public class NutrientList {
@Id
private ObjectId id;
@JsonProperty("name")
private String name;
@JsonProperty("nutrientMap")
Map <String,Double> nutrientMap = new HashMap<String,Double>();
public NutrientList() {}
public NutrientList(String name, Map<String, Double> nutrientMap) {
this.id = new ObjectId();
this.name = name;
this.nutrientMap = nutrientMap;
}
//setters and getters
}
数据在数据库中按单独的营养素存储,不是地图。我看到 NutrientList class 不共享相同的结构,但是有什么方法可以解决这个问题,以便能够在不改变它在数据库中的存储方式的情况下使用地图?
我需要使用地图,因为营养成分很多,我不想为它们设置单独的变量。太感谢了。如果有什么不清楚的地方,请告诉我。
编辑: 我可以交替地将我从数据库中获取数据的 csv 转换为带有地图的 JSON 格式,但我还没有找到可以给我这种灵活性的在线工具。
如果您有有效密钥列表,您可以使用以下内容:
private static final List<String> validKeys = Arrays.asList("vitaminA", "vitaminB" /* ... */);
@RequestMapping("/create")
public NutrientList createNutrientList(@RequestBody Map<String, Object> requestBody) {
Map<String, Double> nutrientMap = new HashMap<>();
for (String nutrient : requestBody.keySet()) {
if (validKeys.contains(nutrient) && requestBody.get(nutrient) instanceof Number) {
Number number = (Number) requestBody.get(nutrient);
nutrientMap.put(nutrient, number.doubleValue());
}
}
String name = (String) requestBody.get("name"); // maybe check if name exists and is really a string
return new NutrientList(name, nutrientMap);
}
如果你想使用Java 8 Stream API你可以尝试:
private static final List<String> validKeys = Arrays.asList("vitaminA", "vitaminB" /* ... */);
@RequestMapping("/create")
public NutrientList createNutrientList(@RequestBody Map<String, Object> requestBody) {
Map<String, Double> nutrientMap = requestBody.entrySet().stream()
.filter(e -> validKeys.contains(e.getKey()))
.filter(e -> e.getValue() instanceof Number)
.collect(Collectors.toMap(Map.Entry::getKey, e -> ((Number) e.getValue()).doubleValue()));
String name = Optional.ofNullable(requestBody.get("name"))
.filter(n -> n instanceof String)
.map(n -> (String) n)
.orElseThrow(IllegalArgumentException::new);
return new NutrientList(name, nutrientMap);
}
希望对您有所帮助。