#401 Code Review of a Comment Fetching Service
Perform a code review:
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
@Service
public class NewService {
private static ArrayList<String> comments = new ArrayList<>();
private static final int commentsCount = 5;
public List<String> getComments() {
comments = new ArrayList<>();
String response = call();
try {
log.debug("getComments: " + response);
for (int i = 0; i < commentsCount; i++) {
comments.add(new JSONObject(response).getJSONArray(i).getString("comment"));
}
} catch (Exception e) {
e.printStackTrace();
}
return comments;
}
/**
* Example JSON:
*
* [
* {
* "fullName": "Ivan Ivanov",
* "comment": "qqqqq"
* },
* {
* "fullName": "Petr Petrov",
* "comment": "wwwww"
* }
* ]
*
*/
private String call() {
ResponseEntity<String> responseEntity = new RestTemplate().exchange(
"http://localhost:9103/api/comments",
HttpMethod.GET,
new HttpEntity<>(new HttpHeaders()),
String.class
);
return responseEntity.getBody();
}
}
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor