Skip to content

Commit 19bbcd9

Browse files
author
Frank
committed
studying resttemplate
1 parent 669c9cb commit 19bbcd9

3 files changed

Lines changed: 115 additions & 82 deletions

File tree

spring-resttemplate/src/test/java/com/concretepage/controller/RestTemplateTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ public void testHeadForHeaders() {
7575
System.out.println(httpHeaders.getContentType());
7676
}
7777

78-
7978
@Test
8079
public void testPostForObject() {
8180
String url = "http://localhost:8080/data/saveinfo/{id}/{name}";

springboot-resttemplate/src/main/java/com/advenoh/controller/EmployeeController.java

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
import org.springframework.web.bind.annotation.RequestMapping;
1515
import org.springframework.web.bind.annotation.RequestMethod;
1616
import org.springframework.web.bind.annotation.RestController;
17+
import org.springframework.web.util.UriComponentsBuilder;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
1721

1822
@Slf4j
1923
@RestController
20-
@RequestMapping("/employee")
2124
public class EmployeeController {
2225

23-
@RequestMapping(method = RequestMethod.GET, value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
26+
@RequestMapping(method = RequestMethod.GET, value = "/employee/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
2427
public Employee getEmployee(@PathVariable Long id) {
2528
log.info("id: {}", id);
2629

@@ -38,7 +41,7 @@ public Employee getEmployee(@PathVariable Long id) {
3841
return employee;
3942
}
4043

41-
@RequestMapping(method = RequestMethod.GET, value = "/{name}/{country}", produces = MediaType.APPLICATION_JSON_VALUE)
44+
@RequestMapping(method = RequestMethod.GET, value = "/employee/{name}/{country}", produces = MediaType.APPLICATION_JSON_VALUE)
4245
public Employee getEmployeeByNameAndCountry(@PathVariable String name, @PathVariable String country) {
4346
log.info("name: {} country: {}", name, country);
4447

@@ -53,6 +56,46 @@ public Employee getEmployeeByNameAndCountry(@PathVariable String name, @PathVari
5356
return employee;
5457
}
5558

59+
@RequestMapping(method = RequestMethod.GET, value = "/employees", produces = MediaType.APPLICATION_JSON_VALUE)
60+
public List<Employee> getAllEmployees() {
61+
List<Employee> lists = new ArrayList();
62+
63+
lists.add(Employee.builder()
64+
.name("frank1")
65+
.address(Address.builder()
66+
.country("US")
67+
.build())
68+
.build());
69+
70+
lists.add(Employee.builder()
71+
.name("frank2")
72+
.address(Address.builder()
73+
.country("US")
74+
.build())
75+
.build());
76+
77+
log.info("lists: {}", lists);
78+
return lists;
79+
}
80+
81+
@RequestMapping(value = "/employee", method = RequestMethod.POST)
82+
public ResponseEntity<String> saveEmployee(@RequestBody Employee employee) {
83+
log.info("employee: {}", employee);
84+
return new ResponseEntity(HttpStatus.CREATED);
85+
}
86+
87+
// @RequestMapping(value = "/location/{id}/{name}", method = RequestMethod.POST)
88+
// public ResponseEntity<Void> locationURI(@PathVariable(value = "id") Integer id,
89+
// @PathVariable(value = "name") String name, @RequestBody Address address,
90+
// UriComponentsBuilder builder) {
91+
// System.out.println("Id:" + id + " Name:" + name);
92+
// System.out.println("Village:" + address.getVillage() + " District:" + address.getDistrict());
93+
// HttpHeaders headers = new HttpHeaders();
94+
// headers.setLocation(builder.path("/location/{id}/{name}").buildAndExpand(id, name).toUri());
95+
// log.info("headers: {}", headers);
96+
// return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
97+
// }
98+
5699
@RequestMapping(method = RequestMethod.GET, value = "/exchange/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
57100
public ResponseEntity<Employee> getEmployeeByExchangeMethod(@PathVariable Long id, @RequestBody String body, @RequestHeader HttpHeaders headers) {
58101
log.info("body: {} headers: {}", body, headers);
@@ -68,16 +111,16 @@ public ResponseEntity<Employee> getEmployeeByExchangeMethod(@PathVariable Long i
68111
}
69112

70113
// @RequestMapping(value = "/exchange/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
71-
// public ResponseEntity<Person> exchangeData(@PathVariable(value = "id") Integer id) {
114+
// public ResponseEntity<Employee> exchangeData(@PathVariable(value = "id") Integer id) {
72115
// Address address = new Address("Dhananjaypur", "Varanasi", "UP");
73-
// Person person = new Person(id, "Mahesh", address);
74-
// return new ResponseEntity<Person>(person, HttpStatus.OK);
116+
// Employee Employee = new Employee(id, "Mahesh", address);
117+
// return new ResponseEntity<Employee>(Employee, HttpStatus.OK);
75118
// }
76119
//
77120
// @RequestMapping(value = "/delete/{name}/{village}", method = RequestMethod.DELETE)
78121
// public void deleteData(@PathVariable(value = "name") String name,
79122
// @PathVariable(value = "village") String village) {
80-
// System.out.println("Delete person with name:" + name + " and village:" + village);
123+
// System.out.println("Delete Employee with name:" + name + " and village:" + village);
81124
// }
82125
//
83126
// @RequestMapping(value = "/fetch/{id}", method = RequestMethod.HEAD)
@@ -95,23 +138,5 @@ public ResponseEntity<Employee> getEmployeeByExchangeMethod(@PathVariable Long i
95138
// System.out.println("District:" + address.getDistrict());
96139
// System.out.println("Village:" + address.getVillage());
97140
// }
98-
//
99-
// @RequestMapping(value = "/saveinfo/{id}/{name}", method = RequestMethod.POST)
100-
// public ResponseEntity<Person> saveInfo(@PathVariable(value = "id") Integer id,
101-
// @PathVariable(value = "name") String name, @RequestBody Address address) {
102-
// Person person = new Person(id, name, address);
103-
// return new ResponseEntity<Person>(person, HttpStatus.CREATED);
104-
// }
105-
//
106-
// @RequestMapping(value = "/location/{id}/{name}", method = RequestMethod.POST)
107-
// public ResponseEntity<Void> locationURI(@PathVariable(value = "id") Integer id,
108-
// @PathVariable(value = "name") String name, @RequestBody Address address,
109-
// UriComponentsBuilder builder) {
110-
// System.out.println("Id:" + id + " Name:" + name);
111-
// System.out.println("Village:" + address.getVillage() + " District:" + address.getDistrict());
112-
// HttpHeaders headers = new HttpHeaders();
113-
// headers.setLocation(builder.path("/location/{id}/{name}").buildAndExpand(id, name).toUri());
114-
// log.info("headers: {}", headers);
115-
// return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
116-
// }
141+
117142
}
Lines changed: 64 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.advenoh.controller;
22

3+
import com.advenoh.model.Address;
34
import com.advenoh.model.Employee;
45
import lombok.extern.slf4j.Slf4j;
56
import org.junit.Test;
67
import org.junit.runner.RunWith;
78
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.core.ParameterizedTypeReference;
810
import org.springframework.http.HttpEntity;
911
import org.springframework.http.HttpHeaders;
1012
import org.springframework.http.HttpMethod;
@@ -15,11 +17,15 @@
1517
import org.springframework.util.MultiValueMap;
1618
import org.springframework.web.client.RestTemplate;
1719

20+
import java.util.HashMap;
21+
import java.util.List;
22+
import java.util.Map;
23+
1824
@Slf4j
1925
@RunWith(SpringRunner.class)
2026
@SpringBootTest
2127
public class EmployeeControllerTest {
22-
final String BASE_URL = "http://localhost:8080/employee";
28+
final String BASE_URL = "http://localhost:8080";
2329

2430
RestTemplate restTemplate = new RestTemplate();
2531

@@ -35,17 +41,19 @@ public class EmployeeControllerTest {
3541
* put() : 주어진 URL 주소로 HTTP PUT 메서드를 실행한다.
3642
* execute():
3743
*/
44+
45+
//GET
3846
@Test
3947
public void test_getForObject() {
40-
Employee employee = restTemplate.getForObject(BASE_URL + "/{id}", Employee.class, 25);
48+
Employee employee = restTemplate.getForObject(BASE_URL + "/employee/{id}", Employee.class, 25);
4149
log.info("employee: {}", employee);
4250
}
4351

4452
@Test
4553
public void test_getForEntity() {
46-
ResponseEntity<Employee> empEntity = restTemplate.getForEntity(BASE_URL + "/{id}", Employee.class, 25);
47-
log.info("statusCode: {}", empEntity.getStatusCode());
48-
log.info("getBody: {}", empEntity.getBody());
54+
ResponseEntity<String> responseEntity = restTemplate.getForEntity(BASE_URL + "/employee/{id}", String.class, 25);
55+
log.info("statusCode: {}", responseEntity.getStatusCode());
56+
log.info("getBody: {}", responseEntity.getBody());
4957
}
5058

5159
@Test
@@ -54,62 +62,21 @@ public void test_getForEntity() {
5462
params.add("name", "Frank Oh");
5563
params.add("country", "US");
5664

57-
ResponseEntity<Employee> empEntity = restTemplate.getForEntity(BASE_URL + "/{name}/{country}", Employee.class, params);
58-
log.info("statusCode: {}", empEntity.getStatusCode());
59-
log.info("getBody: {}", empEntity.getBody());
60-
}
61-
62-
@Test
63-
public void test_get_lists_of_objects() {
64-
//https://www.baeldung.com/spring-rest-template-list
65+
ResponseEntity<Employee> responseEntity = restTemplate.getForEntity(BASE_URL + "/employee/{name}/{country}", Employee.class, params);
66+
log.info("statusCode: {}", responseEntity.getStatusCode());
67+
log.info("getBody: {}", responseEntity.getBody());
6568
}
6669

70+
//POST
6771
@Test
68-
public void test_exchange() {
69-
HttpHeaders headers = new HttpHeaders();
70-
headers.setContentType(MediaType.APPLICATION_JSON);
71-
HttpEntity<String> entity = new HttpEntity<>("Hello World!", headers);
72-
log.info("entity: {}", entity);
72+
public void testPostForObject() {
73+
Employee newEmployee = Employee.builder().name("Frank").address(Address.builder().country("US").build()).build();
7374

74-
ResponseEntity<Employee> empEntity = restTemplate.exchange(BASE_URL + "/exchange/{id}", HttpMethod.GET, entity, Employee.class, 50);
75-
log.info("empEntity: {}", empEntity);
75+
// final HttpEntity<Employee> request = new HttpEntity<>(newEmployee);
76+
Employee employee = restTemplate.postForObject(BASE_URL, newEmployee, Employee.class, 23);
77+
log.info("employee: {}", employee);
7678
}
7779

78-
// @Test
79-
// public void testExchange() {
80-
// String uri = "http://localhost:8080/data/exchange/{id}";
81-
// HttpHeaders headers = new HttpHeaders();
82-
// headers.setContentType(MediaType.APPLICATION_JSON);
83-
// HttpEntity<String> entity = new HttpEntity<>("Hello World!", headers);
84-
// log.info("entity: {}", entity);
85-
//
86-
// ResponseEntity<Person> personEntity = restTemplate.exchange(uri, HttpMethod.GET, entity, Person.class, 100);
87-
// System.out.println("ID:" + personEntity.getBody().getId());
88-
// System.out.println("Name:" + personEntity.getBody().getName());
89-
// System.out.println("Village:" + personEntity.getBody().getAddress().getVillage());
90-
// }
91-
//
92-
// @Test
93-
// public void testHeadForHeaders() {
94-
// String url = "http://localhost:8080/data/fetch/{id}";
95-
// HttpHeaders httpHeaders = restTemplate.headForHeaders(url, 100);
96-
// System.out.println(httpHeaders.getDate());
97-
// System.out.println(httpHeaders.getContentType());
98-
// }
99-
//
100-
//
101-
// @Test
102-
// public void testPostForObject() {
103-
// String url = "http://localhost:8080/data/saveinfo/{id}/{name}";
104-
// Map<String, String> map = new HashMap<>();
105-
// map.put("id", "111");
106-
// map.put("name", "Shyam");
107-
// Address address = new Address("Dhananjaypur", "Varanasi", "UP");
108-
// Person person = restTemplate.postForObject(url, address, Person.class, map);
109-
// System.out.println(person.getName());
110-
// System.out.println(person.getAddress().getVillage());
111-
// }
112-
//
11380
// @Test
11481
// public void testPostForEntity() {
11582
// String url = "http://localhost:8080/data/saveinfo/{id}/{name}";
@@ -131,6 +98,29 @@ public void test_exchange() {
13198
// System.out.println(uri.getPath());
13299
// }
133100
//
101+
102+
// @Test
103+
// public void testExchange() {
104+
// String uri = "http://localhost:8080/data/exchange/{id}";
105+
// HttpHeaders headers = new HttpHeaders();
106+
// headers.setContentType(MediaType.APPLICATION_JSON);
107+
// HttpEntity<String> entity = new HttpEntity<>("Hello World!", headers);
108+
// log.info("entity: {}", entity);
109+
//
110+
// ResponseEntity<Person> personEntity = restTemplate.exchange(uri, HttpMethod.GET, entity, Person.class, 100);
111+
// System.out.println("ID:" + personEntity.getBody().getId());
112+
// System.out.println("Name:" + personEntity.getBody().getName());
113+
// System.out.println("Village:" + personEntity.getBody().getAddress().getVillage());
114+
// }
115+
//
116+
// @Test
117+
// public void testHeadForHeaders() {
118+
// String url = "http://localhost:8080/data/fetch/{id}";
119+
// HttpHeaders httpHeaders = restTemplate.headForHeaders(url, 100);
120+
// System.out.println(httpHeaders.getDate());
121+
// System.out.println(httpHeaders.getContentType());
122+
// }
123+
134124
// @Test
135125
// public void testDelete() {
136126
// String url = "http://localhost:8080/data/delete/{name}/{village}";
@@ -149,4 +139,23 @@ public void test_exchange() {
149139
// Address address = new Address("Dhananjaypur", "Varanasi", "UP");
150140
// restTemplate.put(url, address, map);
151141
// }
142+
143+
@Test
144+
public void test_exchange() {
145+
HttpHeaders headers = new HttpHeaders();
146+
headers.setContentType(MediaType.APPLICATION_JSON);
147+
HttpEntity<String> entity = new HttpEntity<>("Hello World!", headers);
148+
log.info("entity: {}", entity);
149+
150+
ResponseEntity<Employee> empEntity = restTemplate.exchange(BASE_URL + "/exchange/{id}", HttpMethod.GET, entity, Employee.class, 50);
151+
log.info("empEntity: {}", empEntity);
152+
}
153+
154+
@Test
155+
public void test_get_lists_of_objects() {
156+
ResponseEntity<List<Employee>> responseEntity = restTemplate
157+
.exchange(BASE_URL + "/employees", HttpMethod.GET, null, new ParameterizedTypeReference<List<Employee>>() {
158+
});
159+
log.info("responseEntity: {}", responseEntity);
160+
}
152161
}

0 commit comments

Comments
 (0)