John is working in Software Company as a developer, he has developed a web application following client-server architecture and he wants to implement Spring REST API on client application for getting the list of the studentsl from the database. Which of the following code snippet he will use to get the desired result?@Overridepublic List<Student> getStudent() {ResponseEntity<List<Student>> responseEntity =restTemplate.exchange(studentRestUrl, HttpMethod.GET,new ParameterizedTypeReference<List<Student>>() {});AList<Student> students = responseEntity.getBody();} return students;
时间: 2024-03-21 10:41:33 浏览: 68
基于spring的REST服务
5星 · 资源好评率100%
The code snippet that John will use to get the list of students from the database using Spring REST API on the client application would be:
```
@Override
public List<Student> getStudent() {
ResponseEntity<List<Student>> responseEntity = restTemplate.exchange(
studentRestUrl,
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<Student>>() {}
);
List<Student> students = responseEntity.getBody();
return students;
}
```
In this code, John is using the Spring RestTemplate to send an HTTP GET request to the server-side REST API endpoint at the `studentRestUrl`. The `exchange()` method is used to send the request and receive the response, with the HTTP method type as `GET` and the `ParameterizedTypeReference` specifying the expected response type as a list of `Student` objects. The response is then extracted using the `getBody()` method of the `ResponseEntity` object and returned as a list of `Student` objects.
阅读全文