Skip to content

Commit

Permalink
add make getresources postconstruct and add getter to map
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander14121 committed May 31, 2024
1 parent 2a387b1 commit adb4617
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public ResourceController(ResourceService resourceService) {

@GetMapping
public ResponseEntity<Map<String, List<String>>> getResources() {
return ResponseEntity.ok(resourceService.getResources());
return ResponseEntity.ok(resourceService.getResultMap());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.PostConstruct;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
Expand All @@ -18,15 +20,18 @@ public class ResourceService {

private final RestTemplate restTemplate = new RestTemplate();

public Map<String, List<String>> getResources() {
@Getter
private Map<String, List<String>> resultMap = new HashMap<>();

@PostConstruct
public void getResources() {
ObjectMapper objectMapper = new ObjectMapper();

String response = restTemplate.getForObject(url, String.class);

try {
JsonNode rootNode = objectMapper.readTree(response);

Map<String, List<String>> resultMap = new HashMap<>();

JsonNode entriesNode = rootNode.path("_embedded").path("_entries");

Expand All @@ -42,10 +47,8 @@ public Map<String, List<String>> getResources() {

resultMap.computeIfAbsent(key, k -> new ArrayList<>()).add(value);
}
return resultMap;
} catch (IOException e) {
e.printStackTrace();
}
return Collections.emptyMap();
}
}

0 comments on commit adb4617

Please sign in to comment.