I've some Spring Boot applications (as microservices) each one with a Repository like:
@RepositoryRestResource(collectionResourceRel = "cart", path = "cart")
@CrossOrigin
public interface CartRepository extends PagingAndSortingRepository<Cart, Long> {
When I save a "Cart" entity, a @RepositoryEventHandler class sends the entity as a json string to an Apache Kafka topic:
@HandleAfterCreate
public void handleAfterCreate(final Cart cart) throws JSONException, JsonProcessingException {
final JSONObject json = new JSONObject(mapper.writeValueAsString(cart));
json.put("eventType", "saved");
sender.send(json.toString());
}
Finally, another Spring Boot application consumes the Kafka's topic.
Here, I get some related information by calling RestTemplate.getForObject(...)
on other microserivces.
String templateURL="http://localhost:6060/api/cart/{id}/articles";
Map<String, Long> parameters = new HashMap<>();
parameters.put("id", cartId);
String result = restTemplate.getForObject(templateURL, String.class, parameters);
JSONObject obj = new JSONObject(result);
The obj
contains something like:
{"_embedded":{"articles":[{"id":1},{"id":2}]},"_links":{"self":{"href":"http://localhost:7074/api/cart/1/articles"}}}
But I can't find a way to get all the articles from that json.
I tried with:
Object articles = obj.get("articles");
But it doesn't work. I've already seen other answer like this or similar, but I can solve my problem.
So, how can I get a JSONArray from a RepositoryRestResource by RestTemplate?
Thanks