Skip to content

Commit e1aba6f

Browse files
author
Frank
committed
studying resttemplate
1 parent e0316d2 commit e1aba6f

2 files changed

Lines changed: 34 additions & 18 deletions

File tree

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

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ public void deleteEmployeeByName(@PathVariable(value = "name") String name) {
106106
}
107107

108108
@RequestMapping(value = "/employee/{name}", method = RequestMethod.PUT)
109-
public void updateEmployee(@PathVariable(value = "name") String name, @RequestBody Address address) {
109+
public void updateEmployee(@PathVariable(value = "name") String name, @RequestBody Address address,
110+
@RequestHeader HttpHeaders headers) {
111+
log.info("headers : {}", headers);
110112
log.info("name : {} address {}", name, address);
111113
}
112114

@@ -136,20 +138,8 @@ public Employee getEmployeeTimeout(@PathVariable Long id) throws InterruptedExce
136138
return new Employee();
137139
}
138140

139-
// @RequestMapping(value = "/exchange/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
140-
// public ResponseEntity<Employee> exchangeData(@PathVariable(value = "id") Integer id) {
141-
// Address address = new Address("Dhananjaypur", "Varanasi", "UP");
142-
// Employee Employee = new Employee(id, "Mahesh", address);
143-
// return new ResponseEntity<Employee>(Employee, HttpStatus.OK);
144-
// }
145-
146-
// @RequestMapping(value = "/fetch/{id}", method = RequestMethod.HEAD)
147-
// public ResponseEntity<Void> fetch(@PathVariable(value = "id") Integer id) {
148-
// System.out.println("Id:" + id);
149-
// HttpHeaders headers = new HttpHeaders();
150-
// headers.setContentType(MediaType.APPLICATION_JSON);
151-
// return new ResponseEntity<Void>(headers, HttpStatus.NO_CONTENT);
152-
// }
153-
//
154-
141+
@RequestMapping(value = "/employee/{name}", method = RequestMethod.PATCH)
142+
public void partialUpdateEmployee(@PathVariable(value = "name") String name, @RequestBody Address address) {
143+
log.info("name : {} address {}", name, address);
144+
}
155145
}

springboot-resttemplate/src/test/java/com/advenoh/controller/EmployeeControllerTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.advenoh.model.Address;
44
import com.advenoh.model.Employee;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
56
import lombok.extern.slf4j.Slf4j;
67
import org.junit.Test;
78
import org.junit.runner.RunWith;
@@ -17,6 +18,7 @@
1718
import org.springframework.test.context.junit4.SpringRunner;
1819
import org.springframework.util.LinkedMultiValueMap;
1920
import org.springframework.util.MultiValueMap;
21+
import org.springframework.web.client.RequestCallback;
2022
import org.springframework.web.client.ResourceAccessException;
2123
import org.springframework.web.client.RestTemplate;
2224

@@ -176,14 +178,38 @@ public void test_timeout() {
176178
.isInstanceOf(ResourceAccessException.class);
177179
}
178180

181+
//PATCH
179182
@Test
180183
public void test_patchForObject() {
184+
final RestTemplate patchRestTemplate = new RestTemplate(getRequestFactory());
181185

186+
Address address = Address.builder()
187+
.city("Columbus")
188+
.country("US")
189+
.build();
190+
191+
patchRestTemplate.patchForObject(BASE_URL + "/employee/{name}", address, Address.class, "frank");
182192
}
183193

184194
@Test
185195
public void testExecute() {
186-
196+
Address address = Address.builder()
197+
.city("Columbus")
198+
.country("US")
199+
.build();
200+
restTemplate.execute(BASE_URL + "/employee/{name}", HttpMethod.PUT, requestCallback(address), clientHttpResponse -> null, "frank");
201+
}
202+
203+
RequestCallback requestCallback(final Address address) {
204+
return clientHttpRequest -> {
205+
log.info("address : {}", address);
206+
ObjectMapper mapper = new ObjectMapper();
207+
mapper.writeValue(clientHttpRequest.getBody(), address);
208+
clientHttpRequest.getHeaders().add(
209+
HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
210+
clientHttpRequest.getHeaders().add(
211+
HttpHeaders.AUTHORIZATION, "Basic " + "testpasswd");
212+
};
187213
}
188214

189215
/**

0 commit comments

Comments
 (0)