#401 Code Review of a Comment Fetching Service

Companies:
Innotech
Themes:
SpringSpring CoreCode Review

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

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in