forked from cho-log/spring-learning-test
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTodoClientWithRestTemplate.java
More file actions
30 lines (24 loc) · 973 Bytes
/
TodoClientWithRestTemplate.java
File metadata and controls
30 lines (24 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package cholog;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
public class TodoClientWithRestTemplate {
private final RestTemplate restTemplate;
public TodoClientWithRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public Todo getTodoById(Long id) {
try{
String Url = "http://jsonplaceholder.typicode.com/todos/1";
ResponseEntity<Todo> result
= restTemplate.getForEntity(Url+"/{id}", Todo.class);
return result.getBody();
} catch (HttpClientErrorException e){
if (e.getStatusCode() == HttpStatus.NOT_FOUND){
throw new TodoException.NotFound(id);
}
throw new TodoException("Todo with id: " + id + "not found");
}
}
}