I'm facing a challenge with this task. I am trying to transfer a Map from my Spring Boot backend to an Angular application.
Controller
@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;
}
}
Service
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();
}
In Postman I see:
{
"key": "Some Value",
"other-key": "Other Value"
}
I have very limited experience with Angular, but I am eager to learn and complete this task. The map fetched from the backend needs to be stored in localStorage and then displayed in the view based on the key-value pairs. I am struggling with finding the right approach. Can anyone offer some guidance or maybe a sample solution?
Your help is greatly appreciated!