【发布时间】:2021-04-18 10:51:18
【问题描述】:
我被困在任务上。我需要将 Map 从 Spring Boot 后端发送到 Angular 应用程序。
控制器
@GetMapping("/dict")
public Map<String, String> getAll(){
return dictionaryService.getAll();
}
@Entity
public class Entity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "key")
private String key;
@Column(name = "value")
private String value;
public class Model {
private Map<String, String> dict;
public Map<String, String> getDict() {
return dict;
}
public Model setDict(Map<String, String> dict) {
this.dict = dict;
return this;
}
}
服务
public Map<String, String> getAll() {
var model = new DictionaryModel()
.setDictionary(StreamSupport.stream(dictionaryRepository.findAll().spliterator(), false)
.collect(Collectors.toMap(DictionaryEntity::getKey, DictionaryEntity::getValue)));
return model.getDictionary();
}
在邮递员中我有:
{
"key": "Some Value",
"other-key": "Other Value",
}
我与 Angular 几乎没有共同之处,但我需要这样做。从后端下载的地图必须保存在localStorage中,然后从视图中显示的key和value取的localstorage下载。我不知道该怎么做。我的解决方案,即使在这里我也不坚持,但没有奏效。正如我所说,我对 Angular 了解不多。我请求你的理解。有人可以帮帮我吗?显示解决方案或示例解决方案的方法。
您好!
【问题讨论】:
-
您需要从控制器发送地图,对吗?尝试在 responseEntity 中发送它
-
对,但不一定是模型对象。更多的是关于如何在 Angular 中映射类和服务以正确读取它
标签: java angular typescript spring-boot local-storage