#401 Код-ревью сервиса получения комментариев
Сделать код-ревью:
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": "Иванов Иван Иванович",
* "comment": "qqqqq"
* },
* {
* "fullName": "Петров Петр Петрович",
* "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();
}
}
Разбор вопроса
Войди, чтобы начать диалог
Ментор ведёт диалог по текущему вопросу и учитывает предыдущие сообщения.
Войти
Доступно с Premium
Это лишь одно из решений, доступных с Premium. А ещё:
- 250+ продвинутых лекций
- 1300+ практических заданий
- 500+ решений задач с реальных собеседований
- Именные сертификаты Java Hub
- Расширенный доступ к AI-ментору